liurunyu
2024-06-11 914bc07f2ff447f916b736da84d766cda8c6f67b
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
package com.dy.common.webUtil;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
 
/**
 * 全局异常处理,处理在Controller中抛出的异常,在Controller之前(如Fileter)发生的错误或异常由GlErrorCtrl处理
 * 此类是基于拦截器和实现指定接口(GlExceptionHandler和GlExceptionHandlerImpl)
 * 另一种实现方式是基于注解切面实现(GlExceptionAspect)
 */
@ControllerAdvice
@ResponseBody
@Slf4j
public class GlExceptionHandler {
 
    @ExceptionHandler
    public BaseResponse GlExceptionHandler(GlException e){
        log.error("捕获到GlException:{}", e.getMsg(), e);
        return BaseResponseUtils.buildException(e.getMsg());
    }
 
    public BaseResponse exceptionHandler(Throwable t) {
        log.error("捕获到异常:{}",t.getMessage(),t);
        return BaseResponseUtils.buildException("在Controller中或后产生异常:" + t.getMessage());
    }
}