From d2198a259345d1d715deb379e4ccf9a064c40e06 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 16 Mar 2024 21:01:11 +0100 Subject: [PATCH] feat: Implemented problem-details for `ChannelNotReadyException` --- .../api/ChatBackendControllerAdvice.java | 28 +++++++++++++++++++ .../kafka/ChannelNotReadyException.java | 6 +++- 2 files changed, 33 insertions(+), 1 deletion(-) 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 6911ba18..95977d8d 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,6 +1,7 @@ package de.juplo.kafka.chat.backend.api; import de.juplo.kafka.chat.backend.domain.exceptions.*; +import de.juplo.kafka.chat.backend.implementation.kafka.ChannelNotReadyException; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.ProblemDetail; @@ -174,4 +175,31 @@ public class ChatBackendControllerAdvice return problem; } + + @ExceptionHandler(ChannelNotReadyException.class) + public final ProblemDetail handleException( + ChannelNotReadyException 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/channel-not-ready").build().toUri()); + + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(status.getReasonPhrase()); + stringBuilder.append(" - Channel not ready"); + problem.setTitle(stringBuilder.toString()); + + problem.setDetail(e.getMessage()); + + problem.setProperty("state", e.getState()); + + return problem; + } } diff --git a/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/ChannelNotReadyException.java b/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/ChannelNotReadyException.java index 0746748a..71c05ebc 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/ChannelNotReadyException.java +++ b/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/ChannelNotReadyException.java @@ -1,13 +1,17 @@ package de.juplo.kafka.chat.backend.implementation.kafka; +import lombok.Getter; + + public class ChannelNotReadyException extends IllegalStateException { + @Getter public final ChannelState state; public ChannelNotReadyException(ChannelState state) { - super("Not ready! Current state: " + state); + super("Channel not ready! Current state: " + state); this.state = state; } } -- 2.20.1