|
@@ -1,73 +0,0 @@
|
|
|
-package cn.reghao.tnb.account.app.config.web.exception;
|
|
|
|
|
-
|
|
|
|
|
-import cn.reghao.jutil.jdk.exception.ExceptionUtil;
|
|
|
|
|
-import cn.reghao.jutil.web.WebResult;
|
|
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
-import org.springframework.http.HttpHeaders;
|
|
|
|
|
-import org.springframework.http.HttpStatus;
|
|
|
|
|
-import org.springframework.http.MediaType;
|
|
|
|
|
-import org.springframework.http.ResponseEntity;
|
|
|
|
|
-import org.springframework.security.core.AuthenticationException;
|
|
|
|
|
-import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
|
|
-import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
|
|
-import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
|
-import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
-
|
|
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
-
|
|
|
|
|
-/**
|
|
|
|
|
- * 全局异常处理类,处理 controller 抛出的异常
|
|
|
|
|
- *
|
|
|
|
|
- * @author reghao
|
|
|
|
|
- * @date 2019/03/21 10:25:49
|
|
|
|
|
- */
|
|
|
|
|
-@Slf4j
|
|
|
|
|
-@ControllerAdvice
|
|
|
|
|
-public class ControllerExceptionHandler {
|
|
|
|
|
- /**
|
|
|
|
|
- * 处理所有 controller 上抛出的异常
|
|
|
|
|
- *
|
|
|
|
|
- * @date 2019-09-28 上午11:01
|
|
|
|
|
- */
|
|
|
|
|
- @ExceptionHandler({Exception.class})
|
|
|
|
|
- @ResponseBody
|
|
|
|
|
- public ResponseEntity<String> error(Exception e, HttpServletRequest request) throws Exception {
|
|
|
|
|
- String uri = request.getRequestURI();
|
|
|
|
|
- String msg = ExceptionUtil.errorMsg(e);
|
|
|
|
|
- log.error("{} 接口抛出异常: {}", uri, msg);
|
|
|
|
|
-
|
|
|
|
|
- String body = WebResult.errorWithMsg(msg);
|
|
|
|
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
- headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
- int status = HttpStatus.OK.value();
|
|
|
|
|
- if (e instanceof AuthenticationException) {
|
|
|
|
|
- //status = HttpStatus.UNAUTHORIZED.value();
|
|
|
|
|
- // 不处理 AuthenticationException 异常, 继续向上抛出, 由 spring-security 提供的 ExceptionTranslationFilter 来处理
|
|
|
|
|
- // ExceptionTranslationFilter 会使用 AuthenticationEntryPoint 来处理认证异常
|
|
|
|
|
- throw e;
|
|
|
|
|
- } else if (e instanceof MethodArgumentNotValidException) {
|
|
|
|
|
- // 参数校验失败
|
|
|
|
|
- MethodArgumentNotValidException exception = (MethodArgumentNotValidException) e;
|
|
|
|
|
- String errMsg = exception.getBindingResult().getAllErrors().stream()
|
|
|
|
|
- .map(objectError -> {
|
|
|
|
|
- String message = objectError.getDefaultMessage();
|
|
|
|
|
- return message + "\n";
|
|
|
|
|
- }).collect(Collectors.joining());
|
|
|
|
|
- body = WebResult.errorWithMsg(errMsg);
|
|
|
|
|
- /*body = exception.getBindingResult().getAllErrors().stream()
|
|
|
|
|
- .map(DefaultMessageSourceResolvable::getDefaultMessage)
|
|
|
|
|
- .collect(Collectors.joining());*/
|
|
|
|
|
- } else if (e instanceof IllegalStateException) {
|
|
|
|
|
- IllegalStateException exception = (IllegalStateException) e;
|
|
|
|
|
- Throwable throwable = exception.getCause();
|
|
|
|
|
- /*if (throwable instanceof AuthException) {
|
|
|
|
|
- status = HttpStatus.UNAUTHORIZED.value();
|
|
|
|
|
- }*/
|
|
|
|
|
-
|
|
|
|
|
- body = WebResult.errorWithMsg(throwable.getMessage());
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return ResponseEntity.status(status).headers(headers).body(body);
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|