This commit is contained in:
@ -1,33 +0,0 @@
|
||||
package cc.fascinated.exception;
|
||||
|
||||
import cc.fascinated.model.response.Response;
|
||||
import io.micrometer.common.lang.NonNull;
|
||||
import org.springframework.http.HttpStatus;
|
||||
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;
|
||||
|
||||
@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
|
||||
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
|
||||
if (message == null) { // Fallback
|
||||
message = "An internal error has occurred.";
|
||||
}
|
||||
ex.printStackTrace(); // Print the stack trace
|
||||
return new Response(status, message).toResponseEntity(); // Return the error response
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user