Skip to content

Instantly share code, notes, and snippets.

@erdemaydin
Created July 14, 2021 15:19
Show Gist options
  • Select an option

  • Save erdemaydin/5c7ac453310d11934c3ae445faa3c009 to your computer and use it in GitHub Desktop.

Select an option

Save erdemaydin/5c7ac453310d11934c3ae445faa3c009 to your computer and use it in GitHub Desktop.
@ExceptionHandler(Exception.class)
@ResponseBody ResponseEntity<TaskApiError> exceptionHandler(Throwable exception) {
ResponseEntity apiError;
if(exception instanceof InvalidInputException) {
apiError = generateErrorResponse(TaskApiErrorType.INVALID_INPUT_ERROR, HttpStatus.BAD_REQUEST, exception);
} else if (exception instanceof RecordNotFoundException) {
apiError = generateErrorResponse(TaskApiErrorType.RECORD_NOT_FOUND_ERROR, HttpStatus.NOT_FOUND, exception);
} else if (exception instanceof IllegalArgumentException || exception instanceof ValidationException || exception instanceof SessionException) {
apiError = generateErrorResponse(TaskApiErrorType.ILLEGAL_ARGUMENTS_ERROR, HttpStatus.NOT_ACCEPTABLE, exception);
} else {
apiError = generateErrorResponse(TaskApiErrorType.UNHANDLED_ERROR, HttpStatus.FORBIDDEN, exception);
}
return apiError;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment