Skip to content

Instantly share code, notes, and snippets.

@Kwonkyu
Last active January 28, 2022 09:44
Show Gist options
  • Select an option

  • Save Kwonkyu/d3a3dbe73a28f5d7cbf48664d6cfcdb0 to your computer and use it in GitHub Desktop.

Select an option

Save Kwonkyu/d3a3dbe73a28f5d7cbf48664d6cfcdb0 to your computer and use it in GitHub Desktop.

Revisions

  1. Kwonkyu renamed this gist Jan 28, 2022. 1 changed file with 0 additions and 0 deletions.
  2. Kwonkyu revised this gist Jan 28, 2022. 1 changed file with 46 additions and 48 deletions.
    94 changes: 46 additions & 48 deletions global exception handlers
    Original 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({
    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({
    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({
    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)",
    @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()
    @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 " +
    String errorMessage = "Request parameter errors on " +
    exception.getFieldErrorCount() +
    " fields. Check result fields for details.";
    return ApiResponse.fail(fieldErrorDetails, errorMessage);
    }
    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");
    }
    @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");
    }
  3. Kwonkyu revised this gist Jan 28, 2022. 1 changed file with 24 additions and 3 deletions.
    27 changes: 24 additions & 3 deletions global exception handlers
    Original file line number Diff line number Diff line change
    @@ -17,14 +17,35 @@
    @ExceptionHandler({
    IllegalArgumentException.class,
    IllegalStateException.class,
    HttpRequestMethodNotSupportedException.class,
    MethodArgumentNotValidException.class,
    MethodArgumentTypeMismatchException.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
  4. Kwonkyu created this gist Jan 28, 2022.
    4 changes: 4 additions & 0 deletions FieldErrorDetail.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    public class FieldErrorDetail {
    private String rejectedField;
    private String cause;
    }
    38 changes: 38 additions & 0 deletions global exception handlers
    Original 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");
    }