에러메시지 및 예외 처리
예외 처리
/** * GlobalControllerAdvice */
@Slf4j
public class GlobalControllerAdvice {
@ExceptionHandler(Exception.class)
protected ResponseEntity<Object> handleException(Exception e, HttpServletRequest request) {
RequestUtils.setAttribute(RequestLoggingFilter.REQUEST_LOG_LEVEL, this.attributeError);
log.error("", e);
String code = String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value());
String message = e.getMessage();
int httpStatus = Integer.parseInt(code);
ErrorCode errorCode = ErrorCode.builder()
.code(code)
.message(message)
.httpStatus(httpStatus)
.build();
return handleExceptionInternal(errorCode);
}
@ExceptionHandler(BindException.class)
protected ResponseEntity<Object> handleBindException(BindException e, HttpServletRequest request) {
RequestUtils.setAttribute(RequestLoggingFilter.REQUEST_LOG_LEVEL, this.attributeError);
log.warn("", e);
String code = CommonAppError.BINDING_ERROR.getCode();
String message = getBindingErrorMessage(e.getBindingResult());
String httpStatusCode = String.valueOf(HttpStatus.BAD_REQUEST.value());
int httpStatus = getBadRequestHttpStatusCode(httpStatusCode);
ErrorCode errorCode = ErrorCode.builder()
.code(code)
.message(message)
.httpStatus(httpStatus)
.build();
return handleExceptionInternal(e, errorCode);
}
// ...... 그외 등등 소스코드가 길어서 생략함.
}예외 반환 응답값
Exception 종류
반환응답값
비고
프로젝트별 Code값 목록
Code
HttpStatus
설명 (ex)
에러메시지 처리
MessageResolver.class
마지막 업데이트