Custom exceptions

This commit is contained in:
Braydon 2024-04-06 14:42:16 -04:00
parent 6605664857
commit a2804552e0
2 changed files with 23 additions and 1 deletions

@ -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);
}
}

@ -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 { }