exception : custom HandlerExceptionResolver
This commit is contained in:
@@ -3,6 +3,7 @@ package hello.exception;
|
||||
import hello.exception.filter.LogFilter;
|
||||
import hello.exception.interceptor.LogInterceptor;
|
||||
import hello.exception.resolver.MyHandlerExceptionResolver;
|
||||
import hello.exception.resolver.UserHandlerExceptionResolver;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -28,6 +29,7 @@ public class WebConfig implements WebMvcConfigurer {
|
||||
@Override
|
||||
public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
|
||||
resolvers.add(new MyHandlerExceptionResolver());
|
||||
resolvers.add(new UserHandlerExceptionResolver());
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package hello.exception.api;
|
||||
|
||||
import hello.exception.exception.UserException;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -16,9 +17,13 @@ public class ApiExceptionController {
|
||||
|
||||
if (id.equals("ex")) {
|
||||
throw new RuntimeException("잘못된 사용자");
|
||||
} else if (id.equals("bad")) {
|
||||
}
|
||||
if (id.equals("bad")) {
|
||||
throw new IllegalArgumentException("잘못된 입력 값");
|
||||
}
|
||||
if (id.equals("user-ex")) {
|
||||
throw new UserException("사용자 오류");
|
||||
}
|
||||
|
||||
return new MemberDto(id, "hello " + id);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package hello.exception.exception;
|
||||
|
||||
public class UserException extends RuntimeException{
|
||||
|
||||
public UserException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UserException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public UserException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public UserException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
protected UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package hello.exception.resolver;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import hello.exception.exception.UserException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.servlet.HandlerExceptionResolver;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
public class UserHandlerExceptionResolver implements HandlerExceptionResolver {
|
||||
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
|
||||
|
||||
try {
|
||||
if (ex instanceof UserException) {
|
||||
log.info("UserException resolver to 400");
|
||||
String acceptHeader = request.getHeader("accept");
|
||||
|
||||
if ("application/json".equals(acceptHeader)) {
|
||||
Map<String, Object> errorResult = new HashMap<>();
|
||||
errorResult.put("ex", ex.getClass());
|
||||
errorResult.put("message", ex.getMessage());
|
||||
|
||||
String result = objectMapper.writeValueAsString(errorResult);
|
||||
|
||||
response.setContentType("application/json");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.getWriter().write(result);
|
||||
return new ModelAndView();
|
||||
} else {
|
||||
return new ModelAndView("error/500");
|
||||
}
|
||||
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("resolver ex", e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user