From: Kai Moritz Date: Mon, 26 Dec 2022 17:07:57 +0000 (+0100) Subject: refactor: Reworked implementation of the problem-details [RFC-7807] X-Git-Tag: wip~99 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=cf4d6ca13853c0cc464b5e35cf099c9f9cf0adc9;p=demos%2Fkafka%2Fchat refactor: Reworked implementation of the problem-details [RFC-7807] --- diff --git a/src/main/java/de/juplo/kafka/chatroom/api/ChatroomControllerAdvice.java b/src/main/java/de/juplo/kafka/chatroom/api/ChatroomControllerAdvice.java index d98abbbb..42f7599e 100644 --- a/src/main/java/de/juplo/kafka/chatroom/api/ChatroomControllerAdvice.java +++ b/src/main/java/de/juplo/kafka/chatroom/api/ChatroomControllerAdvice.java @@ -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> 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; } } diff --git a/src/main/java/de/juplo/kafka/chatroom/domain/MessageMutationException.java b/src/main/java/de/juplo/kafka/chatroom/domain/MessageMutationException.java index ceeea240..db4ebc24 100644 --- a/src/main/java/de/juplo/kafka/chatroom/domain/MessageMutationException.java +++ b/src/main/java/de/juplo/kafka/chatroom/domain/MessageMutationException.java @@ -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; } }