feat: Added problem-details [RFC-7807] for a message mutation error
[demos/kafka/chat] / src / main / java / de / juplo / kafka / chatroom / api / ChatroomControllerAdvice.java
1 package de.juplo.kafka.chatroom.api;
2
3 import de.juplo.kafka.chatroom.domain.MessageMutationException;
4 import org.springframework.http.HttpStatus;
5 import org.springframework.http.ProblemDetail;
6 import org.springframework.http.ResponseEntity;
7 import org.springframework.web.bind.annotation.ControllerAdvice;
8 import org.springframework.web.bind.annotation.ExceptionHandler;
9 import org.springframework.web.reactive.result.method.annotation.ResponseEntityExceptionHandler;
10 import org.springframework.web.server.ServerWebExchange;
11 import reactor.core.publisher.Mono;
12
13
14 @ControllerAdvice
15 public class ChatroomControllerAdvice extends ResponseEntityExceptionHandler
16 {
17   @ExceptionHandler(MessageMutationException.class)
18   public final Mono<ResponseEntity<Object>> handleException(MessageMutationException e, ServerWebExchange exchange)
19   {
20     final HttpStatus status = HttpStatus.BAD_REQUEST;
21     ProblemDetail body = ProblemDetail.forStatusAndDetail(status, e.getMessage());
22     body.setProperty("new", e.getToAdd());
23     body.setProperty("existing", e.getExisting());
24     return handleExceptionInternal(e, body, null, status, exchange);
25   }
26 }