Last active
January 28, 2022 09:44
-
-
Save Kwonkyu/d3a3dbe73a28f5d7cbf48664d6cfcdb0 to your computer and use it in GitHub Desktop.
Revisions
-
Kwonkyu renamed this gist
Jan 28, 2022 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Kwonkyu revised this gist
Jan 28, 2022 . 1 changed file with 46 additions and 48 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,59 +1,57 @@ @ExceptionHandler({ AuthenticationException.class }) @ResponseStatus(HttpStatus.UNAUTHORIZED) public ApiResponse<Object> handleAuthentication(AuthenticationException exception) { return ApiResponse.fail(exception.getMessage()); } @ExceptionHandler({ AccessDeniedException.class }) @ResponseStatus(HttpStatus.FORBIDDEN) public ApiResponse<Object> handleAccessDenied(AccessDeniedException exception) { return ApiResponse.fail(exception.getMessage()); } @ExceptionHandler({ IllegalArgumentException.class, IllegalStateException.class, HttpRequestMethodNotSupportedException.class }) @ResponseStatus(HttpStatus.BAD_REQUEST) public ApiResponse<Object> handleIllegals(RuntimeException ex) { return ApiResponse.fail(ex.getMessage()); } @ExceptionHandler(MethodArgumentTypeMismatchException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public ApiResponse<Object> methodArgumentTypeMismatch(MethodArgumentTypeMismatchException exception) { return ApiResponse.fail( String.format("Request parameter type mismatch on '%s'(%s)", exception.getName(), exception.getParameter().getParameter().getParameterizedType().getTypeName())); } @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public ApiResponse<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException exception) { List<FieldErrorDetail> fieldErrorDetails = exception.getFieldErrors().stream() .map(fieldError -> new FieldErrorDetail(fieldError.getField(), fieldError.getDefaultMessage())) .collect(Collectors.toList()); String errorMessage = "Request parameter errors on " + exception.getFieldErrorCount() + " fields. Check result fields for details."; return ApiResponse.fail(fieldErrorDetails, errorMessage); } @ExceptionHandler({ RuntimeException.class, Exception.class }) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public ApiResponse<Object> handleNotHandled(Exception ex) { log.error("unhandled exception {}: {}", ex.getClass().getName(), ex.getMessage()); ex.printStackTrace(); return ApiResponse.fail("critical error: please check server logs"); } -
Kwonkyu revised this gist
Jan 28, 2022 . 1 changed file with 24 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -17,14 +17,35 @@ @ExceptionHandler({ IllegalArgumentException.class, IllegalStateException.class, HttpRequestMethodNotSupportedException.class }) @ResponseStatus(HttpStatus.BAD_REQUEST) public ApiResponse<Object> handleIllegals(RuntimeException ex) { return ApiResponse.fail(ex.getMessage()); } @ExceptionHandler(MethodArgumentTypeMismatchException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public ApiResponse<Object> methodArgumentTypeMismatch(MethodArgumentTypeMismatchException exception) { return ApiResponse.fail( String.format("Request parameter type mismatch on '%s'(%s)", exception.getName(), exception.getParameter().getParameter().getParameterizedType().getTypeName())); // check get parameter result. } @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public ApiResponse<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException exception) { List<FieldErrorDetail> fieldErrorDetails = exception.getFieldErrors().stream() .map(fieldError -> new FieldErrorDetail(fieldError.getField(), fieldError.getDefaultMessage())) .collect(Collectors.toList()); String errorMessage = "Request parameter errors on " + exception.getFieldErrorCount() + " fields. Check result fields for details."; return ApiResponse.fail(fieldErrorDetails, errorMessage); } @ExceptionHandler({ RuntimeException.class, Exception.class -
Kwonkyu created this gist
Jan 28, 2022 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ public class FieldErrorDetail { private String rejectedField; private String cause; } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ @ExceptionHandler({ AuthenticationException.class }) @ResponseStatus(HttpStatus.UNAUTHORIZED) public ApiResponse<Object> handleAuthentication(AuthenticationException exception) { return ApiResponse.fail(exception.getMessage()); } @ExceptionHandler({ AccessDeniedException.class }) @ResponseStatus(HttpStatus.FORBIDDEN) public ApiResponse<Object> handleAccessDenied(AccessDeniedException exception) { return ApiResponse.fail(exception.getMessage()); } @ExceptionHandler({ IllegalArgumentException.class, IllegalStateException.class, HttpRequestMethodNotSupportedException.class, MethodArgumentNotValidException.class, MethodArgumentTypeMismatchException.class }) @ResponseStatus(HttpStatus.BAD_REQUEST) public ApiResponse<Object> handleIllegals(RuntimeException ex) { return ApiResponse.fail(ex.getMessage()); } @ExceptionHandler({ RuntimeException.class, Exception.class }) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public ApiResponse<Object> handleNotHandled(Exception ex) { log.error("unhandled exception {}: {}", ex.getClass() .getName(), ex.getMessage()); ex.printStackTrace(); return ApiResponse.fail("critical error: please check server logs"); }