티스토리 뷰
dev_공부일지/spring boot + intelliJ
예외 자동변환하는 DefaultHandlerExceptionResolver
dev_0hoon 2023. 12. 8. 20:26
만약 localhost:8090/api/response-handler-ex?data=111 라는 url에서 data가 Interger 라면
localhost:8090/api/response-handler-ex?data=qqq 라고 넣으면 typeMissMatch 오류가 날 것이다.
그럼 서버에서 오류가 나기 때문에 원래는 500이 나야하지만, 사용자가 잘못 넣은 값이기 때문에 400으로 처리하는 것이 맞을 것이다.
하지만 부트는 타입미스매치를 자동으로 400으로 처리해 준다.
아래의 디폴트핸들러익셉션리졸버가 그것을 가능하게 해준다
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package org.springframework.web.servlet.mvc.support;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.lang.Nullable;
import org.springframework.validation.BindException;
import org.springframework.web.ErrorResponse;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;
public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionResolver {
public static final String PAGE_NOT_FOUND_LOG_CATEGORY = "org.springframework.web.servlet.PageNotFound";
protected static final Log pageNotFoundLogger = LogFactory.getLog("org.springframework.web.servlet.PageNotFound");
public DefaultHandlerExceptionResolver() {
this.setOrder(Integer.MAX_VALUE);
this.setWarnLogCategory(this.getClass().getName());
}
@Nullable
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {
try {
if (ex instanceof ErrorResponse errorResponse) {
ModelAndView mav = null;
if (ex instanceof HttpRequestMethodNotSupportedException theEx) {
mav = this.handleHttpRequestMethodNotSupported(theEx, request, response, handler);
} else if (ex instanceof HttpMediaTypeNotSupportedException theEx) {
mav = this.handleHttpMediaTypeNotSupported(theEx, request, response, handler);
} else if (ex instanceof HttpMediaTypeNotAcceptableException theEx) {
mav = this.handleHttpMediaTypeNotAcceptable(theEx, request, response, handler);
} else if (ex instanceof MissingPathVariableException theEx) {
mav = this.handleMissingPathVariable(theEx, request, response, handler);
} else if (ex instanceof MissingServletRequestParameterException theEx) {
mav = this.handleMissingServletRequestParameter(theEx, request, response, handler);
} else if (ex instanceof MissingServletRequestPartException theEx) {
mav = this.handleMissingServletRequestPartException(theEx, request, response, handler);
} else if (ex instanceof ServletRequestBindingException theEx) {
mav = this.handleServletRequestBindingException(theEx, request, response, handler);
} else if (ex instanceof MethodArgumentNotValidException theEx) {
mav = this.handleMethodArgumentNotValidException(theEx, request, response, handler);
} else if (ex instanceof NoHandlerFoundException theEx) {
mav = this.handleNoHandlerFoundException(theEx, request, response, handler);
} else if (ex instanceof AsyncRequestTimeoutException theEx) {
mav = this.handleAsyncRequestTimeoutException(theEx, request, response, handler);
}
return mav != null ? mav : this.handleErrorResponse(errorResponse, request, response, handler);
}
if (ex instanceof ConversionNotSupportedException theEx) {
return this.handleConversionNotSupported(theEx, request, response, handler);
}
if (ex instanceof TypeMismatchException theEx) {
return this.handleTypeMismatch(theEx, request, response, handler);
}
if (ex instanceof HttpMessageNotReadableException theEx) {
return this.handleHttpMessageNotReadable(theEx, request, response, handler);
}
if (ex instanceof HttpMessageNotWritableException theEx) {
return this.handleHttpMessageNotWritable(theEx, request, response, handler);
}
if (ex instanceof BindException theEx) {
return this.handleBindException(theEx, request, response, handler);
}
} catch (Exception var17) {
if (this.logger.isWarnEnabled()) {
this.logger.warn("Failure while trying to resolve exception [" + ex.getClass().getName() + "]", var17);
}
}
return null;
}
@Nullable
protected ModelAndView handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
return null;
}
@Nullable
protected ModelAndView handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
return null;
}
@Nullable
protected ModelAndView handleHttpMediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
return null;
}
@Nullable
protected ModelAndView handleMissingPathVariable(MissingPathVariableException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
return null;
}
@Nullable
protected ModelAndView handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
return null;
}
@Nullable
protected ModelAndView handleMissingServletRequestPartException(MissingServletRequestPartException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
return null;
}
@Nullable
protected ModelAndView handleServletRequestBindingException(ServletRequestBindingException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
return null;
}
@Nullable
protected ModelAndView handleMethodArgumentNotValidException(MethodArgumentNotValidException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
return null;
}
@Nullable
protected ModelAndView handleNoHandlerFoundException(NoHandlerFoundException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
pageNotFoundLogger.warn(ex.getMessage());
return null;
}
@Nullable
protected ModelAndView handleAsyncRequestTimeoutException(AsyncRequestTimeoutException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
return null;
}
protected ModelAndView handleErrorResponse(ErrorResponse errorResponse, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
if (!response.isCommitted()) {
HttpHeaders headers = errorResponse.getHeaders();
headers.forEach((name, values) -> {
values.forEach((value) -> {
response.addHeader(name, value);
});
});
int status = errorResponse.getStatusCode().value();
String message = errorResponse.getBody().getDetail();
if (message != null) {
response.sendError(status, message);
} else {
response.sendError(status);
}
} else {
this.logger.warn("Ignoring exception, response committed. : " + errorResponse);
}
return new ModelAndView();
}
protected ModelAndView handleConversionNotSupported(ConversionNotSupportedException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
this.sendServerError(ex, request, response);
return new ModelAndView();
}
protected ModelAndView handleTypeMismatch(TypeMismatchException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
response.sendError(400);
return new ModelAndView();
}
protected ModelAndView handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
response.sendError(400);
return new ModelAndView();
}
protected ModelAndView handleHttpMessageNotWritable(HttpMessageNotWritableException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
this.sendServerError(ex, request, response);
return new ModelAndView();
}
/** @deprecated */
@Deprecated(
since = "6.0",
forRemoval = true
)
protected ModelAndView handleBindException(BindException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
response.sendError(400);
return new ModelAndView();
}
protected void sendServerError(Exception ex, HttpServletRequest request, HttpServletResponse response) throws IOException {
request.setAttribute("jakarta.servlet.error.exception", ex);
response.sendError(500);
}
}
'dev_공부일지 > spring boot + intelliJ' 카테고리의 다른 글
@ExceptionHandler Springboot의 자동 예외처리 (0) | 2023.12.13 |
---|---|
예외 자동변환하는 DefaultHandlerExceptionResolver (0) | 2023.12.13 |
HandlerExceptionResolver로 예외처리 깔끔하게 처리하기 (1) | 2023.12.05 |
api 요청 예외처리 일때에도 부트는 자동으로 지원한다 . 아래글과 이어짐 (0) | 2023.12.04 |
api 예외처리는 html이면 안된다. (0) | 2023.12.03 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 백엔드 개발자 역량
- filter
- 리터럴
- 스프링부트
- 컨트
- hypertexttransferprotocol
- JPA
- 스프링공부
- Java
- exception
- 로그인
- 항해플러스
- 인터셉터
- jpa api
- thymleaf
- 향해플러스백엔드
- 백엔드 개발자 공부
- 향해99
- react실행
- BindingResult
- ArgumentResolver
- 항해99
- 향해플러스
- rejectValue
- SpringBoot
- Intercepter
- reject
- HTTP
- React
- 예외처리
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
글 보관함