From ba90f52c674f6f7b4ba549ee07266ffcefda9bd0 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 16 Mar 2024 20:58:19 +0100 Subject: [PATCH] feat: Implemented problem-details for `ChatRoomInactiveException` --- .../api/ChatBackendControllerAdvice.java | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java index 90b639ff..6911ba18 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java +++ b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java @@ -1,9 +1,6 @@ package de.juplo.kafka.chat.backend.api; -import de.juplo.kafka.chat.backend.domain.exceptions.InvalidUsernameException; -import de.juplo.kafka.chat.backend.domain.exceptions.MessageMutationException; -import de.juplo.kafka.chat.backend.domain.exceptions.ShardNotOwnedException; -import de.juplo.kafka.chat.backend.domain.exceptions.UnknownChatroomException; +import de.juplo.kafka.chat.backend.domain.exceptions.*; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.ProblemDetail; @@ -150,4 +147,31 @@ public class ChatBackendControllerAdvice return problem; } + + @ExceptionHandler(ChatRoomInactiveException.class) + public final ProblemDetail handleException( + ChatRoomInactiveException e, + ServerWebExchange exchange, + UriComponentsBuilder uriComponentsBuilder) + { + final HttpStatus status = HttpStatus.SERVICE_UNAVAILABLE; + ProblemDetail problem = ProblemDetail.forStatus(status); + + problem.setProperty("timestamp", new Date()); + + problem.setProperty("requestId", exchange.getRequest().getId()); + + problem.setType(uriComponentsBuilder.replacePath(contextPath).path("/problem/chatroom-inactive").build().toUri()); + + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(status.getReasonPhrase()); + stringBuilder.append(" - Chat-Room not active"); + problem.setTitle(stringBuilder.toString()); + + problem.setDetail(e.getMessage()); + + problem.setProperty("chatroom", e.getChatRoomId()); + + return problem; + } } -- 2.20.1