Don't print exceptions with response statuses

This commit is contained in:
Braydon 2024-04-06 14:43:08 -04:00
parent a2804552e0
commit d73fd7f88c

@ -24,14 +24,18 @@ public final class ExceptionControllerAdvice {
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
public ResponseEntity<?> handleException(@NonNull Exception ex) { public ResponseEntity<?> handleException(@NonNull Exception ex) {
HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR; // Get the HTTP status 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(); status = ex.getClass().getAnnotation(ResponseStatus.class).value();
} }
String message = ex.getLocalizedMessage(); // Get the error message String message = ex.getLocalizedMessage(); // Get the error message
if (message == null) { // Fallback if (message == null) { // Fallback
message = "An internal error has occurred."; 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); return new ResponseEntity<>(new ErrorResponse(status, message), status);
} }
} }