Handle errors better
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0) (push) Successful in 59s

This commit is contained in:
Braydon 2024-04-07 01:09:10 -04:00
parent 46349a1b47
commit 14ddce24e8

@ -7,6 +7,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.resource.NoResourceFoundException;
/**
* Advice for handling raised exceptions.
@ -23,9 +24,13 @@ public final class ExceptionControllerAdvice {
*/
@ExceptionHandler(Exception.class)
public ResponseEntity<?> handleException(@NonNull Exception ex) {
HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR; // Get the HTTP status
boolean hasResponseStatus = ex.getClass().isAnnotationPresent(ResponseStatus.class);
if (hasResponseStatus) { // Get from the @ResponseStatus annotation
HttpStatus status = null; // Get the HTTP status
if (ex instanceof NoResourceFoundException) { // Not found
status = HttpStatus.NOT_FOUND;
} else if (ex instanceof UnsupportedOperationException) { // Not implemented
status = HttpStatus.NOT_IMPLEMENTED;
}
if (ex.getClass().isAnnotationPresent(ResponseStatus.class)) { // Get from the @ResponseStatus annotation
status = ex.getClass().getAnnotation(ResponseStatus.class).value();
}
String message = ex.getLocalizedMessage(); // Get the error message
@ -33,9 +38,12 @@ public final class ExceptionControllerAdvice {
message = "An internal error has occurred.";
}
// Print the stack trace if no response status is present
if (!hasResponseStatus) {
if (status == null) {
ex.printStackTrace();
}
if (status == null) { // Fallback to 500
status = HttpStatus.INTERNAL_SERVER_ERROR;
}
return new ResponseEntity<>(new ErrorResponse(status, message), status);
}
}