Skip to content

Commit

Permalink
feat: 스프링 발생 예외 로깅에 메세지 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
nayonsoso committed Aug 8, 2024
1 parent d49579a commit 94dc84f
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,19 @@ public ProblemDetail handleHttpRequestMethodNotSupportedException(Exception ex)
}
@ExceptionHandler(HttpMediaTypeException.class)
public ProblemDetail handleHttpMediaTypeException(Exception ex) {
logSpringException(ex);
return ProblemDetail.forStatusAndDetail(HttpStatus.UNSUPPORTED_MEDIA_TYPE, "잘못된 media type 입니다.");
}

@ExceptionHandler({MissingRequestValueException.class, MissingServletRequestPartException.class})
public ProblemDetail handleMissingRequestException(Exception ex) {
logSpringException(ex);
return ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "필수 요청 데이터가 누락되었습니다.");
}

@ExceptionHandler({ServletRequestBindingException.class, HttpMessageNotReadableException.class})
public ProblemDetail handleServletRequestBindingException(Exception ex) {
logSpringException(ex);
return ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "요청을 읽을 수 없습니다.");
}

Expand All @@ -80,16 +83,19 @@ public ProblemDetail handleServletRequestBindingException(Exception ex) {
TypeMismatchException.class, HandlerMethodValidationException.class
})
public ProblemDetail handleRequestFormatException(Exception ex) {
logSpringException(ex);
return ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "요청의 형식이 잘못되었습니다.");
}

@ExceptionHandler({NoHandlerFoundException.class, NoResourceFoundException.class})
public ProblemDetail handleNoHandlerFoundException(Exception ex) {
logSpringException(ex);
return ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, "잘못된 경로의 요청입니다.");
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ProblemDetail handleMethodArgumentNotValid(MethodArgumentNotValidException ex) {
logSpringException(ex);
List<FieldErrorResponse> fieldErrors = ex.getBindingResult()
.getFieldErrors()
.stream()
Expand Down Expand Up @@ -119,6 +125,6 @@ private void logInitialServerError(Exception ex) {
}

private void logSpringException(Exception ex) {
log.info("Spring error is occurred - {}", ex.getClass().getSimpleName());
log.info("Spring error is occurred - {}: {}", ex.getClass().getSimpleName(), ex.getLocalizedMessage());
}
}

0 comments on commit 94dc84f

Please sign in to comment.