refactor: Reworked implementation of the problem-details [RFC-7807]
authorKai Moritz <kai@juplo.de>
Mon, 26 Dec 2022 17:07:57 +0000 (18:07 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 15 Jan 2023 18:32:58 +0000 (19:32 +0100)
src/main/java/de/juplo/kafka/chatroom/api/ChatroomControllerAdvice.java
src/main/java/de/juplo/kafka/chatroom/domain/MessageMutationException.java

index d98abbb..42f7599 100644 (file)
@@ -1,26 +1,55 @@
 package de.juplo.kafka.chatroom.api;
 
 import de.juplo.kafka.chatroom.domain.MessageMutationException;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ProblemDetail;
-import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
-import org.springframework.web.reactive.result.method.annotation.ResponseEntityExceptionHandler;
 import org.springframework.web.server.ServerWebExchange;
-import reactor.core.publisher.Mono;
+import org.springframework.web.util.UriComponentsBuilder;
+
+import java.util.Date;
 
 
 @ControllerAdvice
-public class ChatroomControllerAdvice extends ResponseEntityExceptionHandler
+public class ChatroomControllerAdvice
 {
+  @Value("${server.context-path:/}")
+  String contextPath;
+
   @ExceptionHandler(MessageMutationException.class)
-  public final Mono<ResponseEntity<Object>> handleException(MessageMutationException e, ServerWebExchange exchange)
+  public final ProblemDetail handleException(
+      MessageMutationException e,
+      ServerWebExchange exchange,
+      UriComponentsBuilder uriComponentsBuilder)
   {
     final HttpStatus status = HttpStatus.BAD_REQUEST;
-    ProblemDetail body = ProblemDetail.forStatusAndDetail(status, e.getMessage());
-    body.setProperty("new", e.getToAdd());
-    body.setProperty("existing", e.getExisting());
-    return handleExceptionInternal(e, body, null, status, exchange);
+    ProblemDetail problem = ProblemDetail.forStatus(status);
+
+    problem.setProperty("timestamp", new Date());
+
+    problem.setProperty("requestId", exchange.getRequest().getId());
+
+    problem.setType(uriComponentsBuilder.replacePath(contextPath).path("/problem/message-mutation").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("The existing message with user=");
+    stringBuilder.append(e.getExisting().getUser());
+    stringBuilder.append(" and id=");
+    stringBuilder.append(e.getExisting().getId());
+    stringBuilder.append(" cannot be mutated!");
+    problem.setDetail(stringBuilder.toString());
+
+    problem.setProperty("mutatedMessage", e.getMutated());
+
+    problem.setProperty("existingMessage", e.getExisting());
+
+    return problem;
   }
 }
index ceeea24..db4ebc2 100644 (file)
@@ -6,14 +6,14 @@ import lombok.Getter;
 public class MessageMutationException extends RuntimeException
 {
   @Getter
-  private final Message toAdd;
+  private final Message mutated;
   @Getter
   private final Message existing;
 
-  public MessageMutationException(Message toAdd, Message existing)
+  public MessageMutationException(Message mutated, Message existing)
   {
     super("Messages are imutable!");
-    this.toAdd = toAdd;
+    this.mutated = mutated;
     this.existing = existing;
   }
 }