From d73fd7f88c2508c14b567f20e0bf59a38eb72c69 Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Sat, 6 Apr 2024 14:43:08 -0400 Subject: [PATCH] Don't print exceptions with response statuses --- .../braydon/mc/exception/ExceptionControllerAdvice.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/me/braydon/mc/exception/ExceptionControllerAdvice.java b/src/main/java/me/braydon/mc/exception/ExceptionControllerAdvice.java index 8bc891f..38ebdac 100644 --- a/src/main/java/me/braydon/mc/exception/ExceptionControllerAdvice.java +++ b/src/main/java/me/braydon/mc/exception/ExceptionControllerAdvice.java @@ -24,14 +24,18 @@ public final class ExceptionControllerAdvice { @ExceptionHandler(Exception.class) public ResponseEntity handleException(@NonNull Exception ex) { HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR; // Get the HTTP status - if (ex.getClass().isAnnotationPresent(ResponseStatus.class)) { // Get from the @ResponseStatus annotation + boolean hasResponseStatus = ex.getClass().isAnnotationPresent(ResponseStatus.class); + if (hasResponseStatus) { // Get from the @ResponseStatus annotation status = ex.getClass().getAnnotation(ResponseStatus.class).value(); } String message = ex.getLocalizedMessage(); // Get the error message if (message == null) { // Fallback message = "An internal error has occurred."; } - ex.printStackTrace(); // Print the stack trace + // Print the stack trace if no response status is present + if (!hasResponseStatus) { + ex.printStackTrace(); + } return new ResponseEntity<>(new ErrorResponse(status, message), status); } } \ No newline at end of file