fix: Fixed generated problem-details for mutated messages
[demos/kafka/chat] / src / main / java / de / juplo / kafka / chat / backend / api / ChatBackendControllerAdvice.java
index fb9c0f2..6ff108e 100644 (file)
@@ -18,6 +18,36 @@ public class ChatBackendControllerAdvice
   @Value("${server.context-path:/}")
   String contextPath;
 
+  @ExceptionHandler(UnknownChatroomException.class)
+  public final ProblemDetail handleException(
+      UnknownChatroomException e,
+      ServerWebExchange exchange,
+      UriComponentsBuilder uriComponentsBuilder)
+  {
+    final HttpStatus status = HttpStatus.NOT_FOUND;
+    ProblemDetail problem = ProblemDetail.forStatus(status);
+
+    problem.setProperty("timestamp", new Date());
+
+    problem.setProperty("requestId", exchange.getRequest().getId());
+
+    problem.setType(uriComponentsBuilder.replacePath(contextPath).path("/problem/unknown-chatroom").build().toUri());
+    StringBuilder stringBuilder = new StringBuilder();
+    stringBuilder.append(status.getReasonPhrase());
+    stringBuilder.append(" - ");
+    stringBuilder.append(e.getMessage());
+    problem.setTitle(stringBuilder.toString());
+
+    stringBuilder.setLength(0);
+    stringBuilder.append("Chatroom unknown: ");
+    stringBuilder.append(e.getChatroomId());
+    problem.setDetail(stringBuilder.toString());
+
+    problem.setProperty("chatroomId", e.getChatroomId());
+
+    return problem;
+  }
+
   @ExceptionHandler(MessageMutationException.class)
   public final ProblemDetail handleException(
       MessageMutationException e,
@@ -46,9 +76,9 @@ public class ChatBackendControllerAdvice
     stringBuilder.append(" cannot be mutated!");
     problem.setDetail(stringBuilder.toString());
 
-    problem.setProperty("mutatedMessage", e.getMutated());
+    problem.setProperty("mutatedMessage", MessageTo.from(e.getMutated()));
 
-    problem.setProperty("existingMessage", e.getExisting());
+    problem.setProperty("existingMessage", MessageTo.from(e.getExisting()));
 
     return problem;
   }