From a2804552e0a9763e71a31bbcb8aaaf99d42be5f4 Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Sat, 6 Apr 2024 14:42:16 -0400 Subject: [PATCH] Custom exceptions --- .../mc/exception/ExceptionControllerAdvice.java | 9 ++++++++- .../mc/exception/impl/BadRequestException.java | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/main/java/me/braydon/mc/exception/impl/BadRequestException.java diff --git a/src/main/java/me/braydon/mc/exception/ExceptionControllerAdvice.java b/src/main/java/me/braydon/mc/exception/ExceptionControllerAdvice.java index d38b0c3..8bc891f 100644 --- a/src/main/java/me/braydon/mc/exception/ExceptionControllerAdvice.java +++ b/src/main/java/me/braydon/mc/exception/ExceptionControllerAdvice.java @@ -1,7 +1,7 @@ package me.braydon.mc.exception; import lombok.NonNull; -import me.braydon.mc.model.response.ErrorResponse; +import me.braydon.mc.model.ErrorResponse; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; @@ -15,6 +15,12 @@ import org.springframework.web.bind.annotation.ResponseStatus; */ @ControllerAdvice public final class ExceptionControllerAdvice { + /** + * Handle a raised exception. + * + * @param ex the raised exception + * @return the error response + */ @ExceptionHandler(Exception.class) public ResponseEntity handleException(@NonNull Exception ex) { HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR; // Get the HTTP status @@ -25,6 +31,7 @@ public final class ExceptionControllerAdvice { if (message == null) { // Fallback message = "An internal error has occurred."; } + ex.printStackTrace(); // Print the stack trace return new ResponseEntity<>(new ErrorResponse(status, message), status); } } \ No newline at end of file diff --git a/src/main/java/me/braydon/mc/exception/impl/BadRequestException.java b/src/main/java/me/braydon/mc/exception/impl/BadRequestException.java new file mode 100644 index 0000000..77edc22 --- /dev/null +++ b/src/main/java/me/braydon/mc/exception/impl/BadRequestException.java @@ -0,0 +1,15 @@ +package me.braydon.mc.exception.impl; + +import lombok.experimental.StandardException; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +/** + * This exception is raised + * when a bad request is made. + * + * @author Braydon + */ +@StandardException +@ResponseStatus(HttpStatus.BAD_REQUEST) +public final class BadRequestException extends RuntimeException { } \ No newline at end of file