Last active
July 14, 2021 17:43
-
-
Save erdemaydin/c840a9365c81de14870cee4f5d7f87a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @ControllerAdvice | |
| public class TaskApiExceptionHandler { | |
| private static Logger log = Logger.getLogger("TaskApiExceptionHandler"); | |
| @ExceptionHandler(InvalidInputException.class) | |
| @ResponseBody ResponseEntity<TaskApiError> invalidInputException(InvalidInputException exception) { | |
| return generateErrorResponse(TaskApiErrorType.INVALID_INPUT_ERROR, HttpStatus.BAD_REQUEST, exception); | |
| } | |
| @ExceptionHandler(RecordNotFoundException.class) | |
| @ResponseBody ResponseEntity<TaskApiError> recordNotFoundException(RecordNotFoundException exception) { | |
| return generateErrorResponse(TaskApiErrorType.RECORD_NOT_FOUND_ERROR, HttpStatus.NOT_FOUND, exception); | |
| } | |
| @ExceptionHandler({InvalidSessionException.class, UnauthorizedSessionException.class, ExpiredSessionException.class}) | |
| @ResponseBody ResponseEntity<TaskApiError> sessionExceptions(RuntimeException exception) { | |
| return generateErrorResponse(TaskApiErrorType.SESSION_ERROR, HttpStatus.NOT_ACCEPTABLE, exception); | |
| } | |
| @ExceptionHandler(Throwable.class) | |
| @ResponseBody ResponseEntity<TaskApiError> unhandledExceptions(Throwable exception) { | |
| return generateErrorResponse(TaskApiErrorType.UNHANDLED_ERROR, HttpStatus.FORBIDDEN, exception); | |
| } | |
| private ResponseEntity<TaskApiError> generateErrorResponse(TaskApiErrorType errorType, HttpStatus httpStatus, Throwable exception) { | |
| generateErrorLog(exception); | |
| return new ResponseEntity<>( | |
| new TaskApiError(errorType, exception.getMessage()), | |
| httpStatus); | |
| } | |
| private void generateErrorLog(Throwable exception) { | |
| log.error("ErrorMessage:" + exception.getMessage() + ", ErrorDesc:" + exception.getCause()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment