Repository: provectus/kafka-ui
Issue: Schema registry: delete request doesn't return a response
Labels: type/bug
scope/backend
status/accepted
status/confirmed

**Describe the bug**
Schema registry: delete request doesn't return a response

**Set up**
docker-compose -f kafka-ui.yaml up

**Steps to Reproduce**
Steps to reproduce the behavior:

1. Create a schema
2. Delete a schema

**Expected behavior**
Backend returns a response. In fact, it's pending until timeout happens.

Issue comments:
fixed within https://github.com/provectus/kafka-ui/pull/1360
It's not

Pull request review comments:
@jonasgeiregat 
Mono[Void] cant be mapped, you should use thenReturn(..) or then() on Mono[Void]. 
In general I think its better to make all schemaRegistryService.deleteXXX methods return Mono[Void] and do .thenReturn(ResponseEntity.ok().build()); in SchemasController
use then() instead  of .flatMap(r -> Mono.empty()); Also apply this return type change to all deleteXXX methods 
Thanks for pointing this out. I improved the PR according to your input.
I also believe the mapping to a ResponseEntity should be done in the controller and definitely not forward the ResponseEntity from a previous Rest call.
lets remove this one and extend SchemaFailedToDeleteException from CustomBaseException. I don't think we need another hierarchy level for this

Patch that resolved the issue:
diff --git a/kafka-ui-api/src/main/java/com/provectus/kafka/ui/controller/SchemasController.java b/kafka-ui-api/src/main/java/com/provectus/kafka/ui/controller/SchemasController.java
index c9ba613a3dc..e738216b49a 100644
--- a/kafka-ui-api/src/main/java/com/provectus/kafka/ui/controller/SchemasController.java
+++ b/kafka-ui-api/src/main/java/com/provectus/kafka/ui/controller/SchemasController.java
@@ -69,7 +69,8 @@ public Mono<ResponseEntity<Void>> deleteLatestSchema(
   @Override
   public Mono<ResponseEntity<Void>> deleteSchema(
       String clusterName, String subjectName, ServerWebExchange exchange) {
-    return schemaRegistryService.deleteSchemaSubjectEntirely(getCluster(clusterName), subjectName);
+    return schemaRegistryService.deleteSchemaSubjectEntirely(getCluster(clusterName), subjectName)
+            .thenReturn(ResponseEntity.ok().build());
   }
 
   @Override
diff --git a/kafka-ui-api/src/main/java/com/provectus/kafka/ui/exception/ErrorCode.java b/kafka-ui-api/src/main/java/com/provectus/kafka/ui/exception/ErrorCode.java
index 20df819ec9f..38ab56e8920 100644
--- a/kafka-ui-api/src/main/java/com/provectus/kafka/ui/exception/ErrorCode.java
+++ b/kafka-ui-api/src/main/java/com/provectus/kafka/ui/exception/ErrorCode.java
@@ -25,7 +25,8 @@ public enum ErrorCode {
   DIR_NOT_FOUND(4012, HttpStatus.BAD_REQUEST),
   TOPIC_OR_PARTITION_NOT_FOUND(4013, HttpStatus.BAD_REQUEST),
   INVALID_REQUEST(4014, HttpStatus.BAD_REQUEST),
-  RECREATE_TOPIC_TIMEOUT(4015, HttpStatus.REQUEST_TIMEOUT);
+  RECREATE_TOPIC_TIMEOUT(4015, HttpStatus.REQUEST_TIMEOUT),
+  SCHEMA_NOT_DELETED(4015, HttpStatus.INTERNAL_SERVER_ERROR);
 
   static {
     // codes uniqueness check
@@ -33,7 +34,7 @@ public enum ErrorCode {
     for (ErrorCode value : ErrorCode.values()) {
       if (!codes.add(value.code())) {
         LoggerFactory.getLogger(ErrorCode.class)
-            .warn("Multiple {} values refer to code {}", ErrorCode.class, value.code);
+                .warn("Multiple {} values refer to code {}", ErrorCode.class, value.code);
       }
     }
   }
diff --git a/kafka-ui-api/src/main/java/com/provectus/kafka/ui/exception/SchemaFailedToDeleteException.java b/kafka-ui-api/src/main/java/com/provectus/kafka/ui/exception/SchemaFailedToDeleteException.java
new file mode 100644
index 00000000000..c88ee86af76
--- /dev/null
+++ b/kafka-ui-api/src/main/java/com/provectus/kafka/ui/exception/SchemaFailedToDeleteException.java
@@ -0,0 +1,13 @@
+package com.provectus.kafka.ui.exception;
+
+public class SchemaFailedToDeleteException extends CustomBaseException {
+
+  public SchemaFailedToDeleteException(String schemaName) {
+    super(String.format("Unable to delete schema with name %s", schemaName));
+  }
+
+  @Override
+  public ErrorCode getErrorCode() {
+    return ErrorCode.SCHEMA_NOT_DELETED;
+  }
+}
diff --git a/kafka-ui-api/src/main/java/com/provectus/kafka/ui/service/SchemaRegistryService.java b/kafka-ui-api/src/main/java/com/provectus/kafka/ui/service/SchemaRegistryService.java
index 84aba4b81b1..10e0a0a147c 100644
--- a/kafka-ui-api/src/main/java/com/provectus/kafka/ui/service/SchemaRegistryService.java
+++ b/kafka-ui-api/src/main/java/com/provectus/kafka/ui/service/SchemaRegistryService.java
@@ -4,6 +4,7 @@
 import static org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY;
 
 import com.provectus.kafka.ui.exception.DuplicateEntityException;
+import com.provectus.kafka.ui.exception.SchemaFailedToDeleteException;
 import com.provectus.kafka.ui.exception.SchemaNotFoundException;
 import com.provectus.kafka.ui.exception.SchemaTypeIsNotSupportedException;
 import com.provectus.kafka.ui.exception.UnprocessableEntityException;
@@ -32,6 +33,7 @@
 import org.jetbrains.annotations.NotNull;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpMethod;
+import org.springframework.http.HttpStatus;
 import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
@@ -156,16 +158,16 @@ private Mono<ResponseEntity<Void>> deleteSchemaSubject(KafkaCluster cluster, Str
         ).toBodilessEntity();
   }
 
-  public Mono<ResponseEntity<Void>> deleteSchemaSubjectEntirely(KafkaCluster cluster,
+  public Mono<Void> deleteSchemaSubjectEntirely(KafkaCluster cluster,
                                                                 String schemaName) {
     return configuredWebClient(
         cluster,
         HttpMethod.DELETE,
         URL_SUBJECT, schemaName)
         .retrieve()
-        .onStatus(NOT_FOUND::equals,
-            throwIfNotFoundStatus(formatted(NO_SUCH_SCHEMA, schemaName)))
-        .toBodilessEntity();
+        .onStatus(HttpStatus::isError, errorOnSchemaDeleteFailure(schemaName))
+        .toBodilessEntity()
+        .then();
   }
 
   /**
@@ -335,4 +337,13 @@ private WebClient.RequestBodySpec configuredWebClient(InternalSchemaRegistry sch
   private boolean isUnrecognizedFieldSchemaTypeMessage(String errorMessage) {
     return errorMessage.contains(UNRECOGNIZED_FIELD_SCHEMA_TYPE);
   }
+
+  private Function<ClientResponse, Mono<? extends Throwable>> errorOnSchemaDeleteFailure(String schemaName) {
+    return resp -> {
+      if (NOT_FOUND.equals(resp.statusCode())) {
+        return Mono.error(new SchemaNotFoundException(schemaName));
+      }
+      return Mono.error(new SchemaFailedToDeleteException(schemaName));
+    };
+  }
 }


Test patch: