|
| 1 | +package com.example.exception; |
| 2 | + |
| 3 | +import cn.hutool.core.util.ObjectUtil; |
| 4 | +import org.slf4j.Logger; |
| 5 | +import org.slf4j.LoggerFactory; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.http.HttpStatus; |
| 8 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
| 9 | +import org.springframework.web.bind.annotation.ResponseStatus; |
| 10 | +import org.springframework.web.bind.annotation.RestControllerAdvice; |
| 11 | +import org.springframework.web.servlet.NoHandlerFoundException; |
| 12 | + |
| 13 | +import javax.servlet.http.HttpServletRequest; |
| 14 | +import java.util.concurrent.ExecutionException; |
| 15 | +import java.util.concurrent.RejectedExecutionException; |
| 16 | + |
| 17 | +/** |
| 18 | + * 全局异常控制处理器 |
| 19 | + * 未找到指定异常处理方法exception,则以全局异常方法globalException兜底 |
| 20 | + * |
| 21 | + * @author wliduo[i@dolyw.com] |
| 22 | + * @date 2021/10/12 15:46 |
| 23 | + */ |
| 24 | +@RestControllerAdvice |
| 25 | +public class ExceptionAdvice { |
| 26 | + |
| 27 | + /** |
| 28 | + * logger |
| 29 | + */ |
| 30 | + private static final Logger logger = LoggerFactory.getLogger(ExceptionAdvice.class); |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private HttpServletRequest request; |
| 34 | + |
| 35 | + /** |
| 36 | + * 捕捉RejectedExecutionException-线程拒绝策略异常 |
| 37 | + * |
| 38 | + * @param e |
| 39 | + * @return java.lang.Object |
| 40 | + * @throws |
| 41 | + * @author wliduo[i@dolyw.com] |
| 42 | + * @date 2021/11/9 13:49 |
| 43 | + */ |
| 44 | + @ResponseStatus(HttpStatus.OK) |
| 45 | + @ExceptionHandler(RejectedExecutionException.class) |
| 46 | + public Object exception(RejectedExecutionException e) { |
| 47 | + // logger.error("{}接口线程服务异常:{}", request.getRequestURI(), ExceptionUtil.stacktraceToOneLineString(e.getCause(), 10000000)); |
| 48 | + logger.error("{}接口线程服务异常:{}", request.getRequestURI(), e); |
| 49 | + return e.getMessage(); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * 捕捉ExecutionException-FutureTask异步线程异常 |
| 54 | + * |
| 55 | + * @param e |
| 56 | + * @return java.lang.Object |
| 57 | + * @throws |
| 58 | + * @author wliduo[i@dolyw.com] |
| 59 | + * @date 2021/11/9 13:46 |
| 60 | + */ |
| 61 | + @ResponseStatus(HttpStatus.OK) |
| 62 | + @ExceptionHandler(ExecutionException.class) |
| 63 | + public Object exception(ExecutionException e) { |
| 64 | + // logger.error("{}接口异步线程服务异常:{}", request.getRequestURI(), ExceptionUtil.stacktraceToOneLineString(e.getCause(), 10000000)); |
| 65 | + logger.error("{}接口异步线程服务异常:{}", request.getRequestURI(), e.getCause()); |
| 66 | + if (ObjectUtil.isNotEmpty(e.getCause().getMessage())) { |
| 67 | + return e.getCause().getMessage(); |
| 68 | + } else { |
| 69 | + return e.getMessage(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * 捕捉404未找到路径异常 |
| 75 | + * |
| 76 | + * @param e |
| 77 | + * @return java.lang.Object |
| 78 | + * @throws |
| 79 | + * @author wliduo[i@dolyw.com] |
| 80 | + * @date 2021/10/12 16:08 |
| 81 | + */ |
| 82 | + @ResponseStatus(HttpStatus.OK) |
| 83 | + @ExceptionHandler(NoHandlerFoundException.class) |
| 84 | + public Object exception(NoHandlerFoundException e) { |
| 85 | + return "404"; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * 捕捉其他所有Exception异常 |
| 90 | + * |
| 91 | + * @param e |
| 92 | + * @return com.pcic.app.common.dto.ResponseMessage |
| 93 | + * @throws |
| 94 | + * @author wliduo[i@dolyw.com] |
| 95 | + * @date 2021/10/12 16:09 |
| 96 | + */ |
| 97 | + @ResponseStatus(HttpStatus.OK) |
| 98 | + @ExceptionHandler(Exception.class) |
| 99 | + public Object globalException(Exception e) { |
| 100 | + // logger.error("{}接口服务异常:{}", request.getRequestURI(), ExceptionUtil.stacktraceToOneLineString(e, 10000000)); |
| 101 | + logger.error("{}接口服务异常:{}", request.getRequestURI(), e); |
| 102 | + return e.getMessage(); |
| 103 | + } |
| 104 | +} |
0 commit comments