From d5da6d9568f9101ae20561645c4d3c9df0d1495e Mon Sep 17 00:00:00 2001 From: Higunio320 Date: Tue, 7 May 2024 19:58:15 +0200 Subject: [PATCH 1/2] Added abstract exception handler --- .../exceptions/AbstractExceptionHandler.java | 36 +++++++++++++++++++ .../data/AbstractExceptionResponse.java | 9 +++++ 2 files changed, 45 insertions(+) create mode 100644 corn-backend/src/main/java/dev/corn/cornbackend/utils/exceptions/AbstractExceptionHandler.java create mode 100644 corn-backend/src/main/java/dev/corn/cornbackend/utils/exceptions/data/AbstractExceptionResponse.java diff --git a/corn-backend/src/main/java/dev/corn/cornbackend/utils/exceptions/AbstractExceptionHandler.java b/corn-backend/src/main/java/dev/corn/cornbackend/utils/exceptions/AbstractExceptionHandler.java new file mode 100644 index 00000000..f67e5e25 --- /dev/null +++ b/corn-backend/src/main/java/dev/corn/cornbackend/utils/exceptions/AbstractExceptionHandler.java @@ -0,0 +1,36 @@ +package dev.corn.cornbackend.utils.exceptions; + +import dev.corn.cornbackend.utils.exceptions.data.AbstractExceptionResponse; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import java.time.LocalDateTime; + +@RestControllerAdvice +@Slf4j +public class AbstractExceptionHandler { + + private static final String EXCEPTION_OCCURRED_MESSAGE = "Exception occurred with status: {} and response: {}"; + + @ExceptionHandler(AbstractException.class) + public final ResponseEntity handleAbstractException(AbstractException exception) { + AbstractExceptionResponse response = buildExceptionResponse(exception); + + HttpStatus status = HttpStatus.valueOf(exception.getStatusCode().value()); + + log.error(EXCEPTION_OCCURRED_MESSAGE, status.value(), response); + + return ResponseEntity.status(status).body(response); + } + + private AbstractExceptionResponse buildExceptionResponse(Exception exception) { + return AbstractExceptionResponse.builder() + .exceptionMessage(exception.getMessage()) + .exceptionName(exception.getClass().getSimpleName()) + .timeStamp(LocalDateTime.now()) + .build(); + } +} diff --git a/corn-backend/src/main/java/dev/corn/cornbackend/utils/exceptions/data/AbstractExceptionResponse.java b/corn-backend/src/main/java/dev/corn/cornbackend/utils/exceptions/data/AbstractExceptionResponse.java new file mode 100644 index 00000000..264c7935 --- /dev/null +++ b/corn-backend/src/main/java/dev/corn/cornbackend/utils/exceptions/data/AbstractExceptionResponse.java @@ -0,0 +1,9 @@ +package dev.corn.cornbackend.utils.exceptions.data; + +import lombok.Builder; + +import java.time.LocalDateTime; + +@Builder +public record AbstractExceptionResponse(String exceptionName, String exceptionMessage, LocalDateTime timeStamp){ +} From 4fdb32d1b400aba7f03fede39addab6d8b5d6a0d Mon Sep 17 00:00:00 2001 From: Higunio320 Date: Tue, 7 May 2024 20:22:25 +0200 Subject: [PATCH 2/2] Added stack trace logging --- .../cornbackend/utils/exceptions/AbstractExceptionHandler.java | 1 + 1 file changed, 1 insertion(+) diff --git a/corn-backend/src/main/java/dev/corn/cornbackend/utils/exceptions/AbstractExceptionHandler.java b/corn-backend/src/main/java/dev/corn/cornbackend/utils/exceptions/AbstractExceptionHandler.java index f67e5e25..52ad8a8c 100644 --- a/corn-backend/src/main/java/dev/corn/cornbackend/utils/exceptions/AbstractExceptionHandler.java +++ b/corn-backend/src/main/java/dev/corn/cornbackend/utils/exceptions/AbstractExceptionHandler.java @@ -22,6 +22,7 @@ public final ResponseEntity handleAbstractException(A HttpStatus status = HttpStatus.valueOf(exception.getStatusCode().value()); log.error(EXCEPTION_OCCURRED_MESSAGE, status.value(), response); + log.error("", exception); return ResponseEntity.status(status).body(response); }