全局异常拦截器

全局异常处理器

package com.yuanda.erp9.syn.exception;

import com.yuanda.erp9.syn.entity.CmsOtherWarnLogEntity;
import com.yuanda.erp9.syn.service.erp9.CmsOtherWarnLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @Description: 全局的的异常拦截器(拦截所有的控制器)
 */
@RestControllerAdvice
@Order(-1)
@Slf4j
public class GlobalExceptionHandler {


    @Resource
    private CmsOtherWarnLogService cmsOtherWarnLogService;


    /**
     * @Description: 目标服务器错误
     * @Params:
     * @Return:
     * @Author: Mr.myq
     * @Date: 2023/1/410:21
     */
    @ExceptionHandler(TargetServerException.class)
    public void targetServerException(TargetServerException e) {
        log.info("全局异常拦截器=》{}",e.toString());
        String message = e.getMessage();
        CmsOtherWarnLogEntity entity = new CmsOtherWarnLogEntity();
        entity.setSupplier(e.getSupplier());
        entity.setFileName(e.getFileName());
        entity.setWarnLog(message);
        entity.setCreateTime(LocalDateTime.now().toString());
        cmsOtherWarnLogService.addOtherWarnLogEntity(entity);
    }


    /**
     * @Description: 服务准备阶段错误
     * @Params:
     * @Return:
     * @Author: Mr.myq
     * @Date: 2023/1/410:21
     */
    @ExceptionHandler(ServerPrepareException.class)
    public void serverPrepareException(ServerPrepareException e) {
        log.info("全局异常拦截器=》{}",e.toString());
        String message = e.getMessage();
        CmsOtherWarnLogEntity entity = new CmsOtherWarnLogEntity();
        entity.setSupplier(e.getSupplier());
        entity.setFileName(e.getFileName());
        entity.setWarnLog(message);
        entity.setCreateTime(LocalDateTime.now().toString());
        cmsOtherWarnLogService.addOtherWarnLogEntity(entity);
    }


    /**
     * @Description: 程序未知异常
     * @Params:
     * @Return:
     * @Author: Mr.myq
     * @Date: 2023/2/2314:46
     */
    @ExceptionHandler(Exception.class)
    public void undefinedException(Exception e) {
        log.info("全局异常拦截器=》{}",e.toString());
        CmsOtherWarnLogEntity entity = new CmsOtherWarnLogEntity();
        entity.setSupplier(9999);
        entity.setFileName("未知");
        entity.setWarnLog("异常信息:{" + e + "}, 异常错误抛出点: {" + e.getStackTrace()[0] + "}");
        entity.setCreateTime(LocalDateTime.now().toString());
        cmsOtherWarnLogService.addOtherWarnLogEntity(entity);
    }


}

自定义异常

package com.yuanda.erp9.syn.exception;

import lombok.Getter;
import lombok.Setter;

/**
 * @ClassName ServerPrepareException
 * @Description 准备阶段异常(下载文件、读取excel、处理邮件等异常)
 * @Date 2023/2/7
 * @Author myq
 */
@Getter
@Setter
public class ServerPrepareException extends RuntimeException {

    private Integer supplier;
    private String errMsg;
    private String fileName;

    /**
     * @Description: 默认构造器
     * @Params:
     * @Return:
     * @Author: Mr.myq
     * @Date: 2023/1/410:52
     */
    public ServerPrepareException(String message) {
        super(message);
        this.errMsg = message;
    }


    /**
     * @Description: 带供应商ID的构造器
     * @Params:
     * @Return:
     * @Author: Mr.myq
     * @Date: 2023/1/410:52
     */
    public ServerPrepareException(String message, Integer supplier, String fileName) {
        super(message);
        this.errMsg = message;
        this.supplier = supplier;
        this.fileName = fileName;
    }
}
package com.yuanda.erp9.syn.exception;

import lombok.Getter;
import lombok.Setter;

/**
 * @ClassName TargetServerException
 * @Description 目标服务器错误(连接拒绝、连接超时、连接重置等异常)
 * @Date 2023/1/4
 * @Author myq
 */
@Getter
@Setter
public class TargetServerException extends RuntimeException {

    private Integer supplier;
    private String errMsg;
    private String fileName;

    /**
     * @Description: 默认构造器
     * @Params:
     * @Return:
     * @Author: Mr.myq
     * @Date: 2023/1/410:52
     */
    public TargetServerException(String message){
        super(message);
        this.errMsg = message;
    }


    /**
     * @Description: 带供应商ID的构造器
     * @Params:
     * @Return:
     * @Author: Mr.myq
     * @Date: 2023/1/410:52
     */
    public TargetServerException(String message,Integer supplier,String fileName){
        super(message);
        this.errMsg = message;
        this.supplier = supplier;
        this.fileName = fileName;
    }

}

常见的一些错误

错误一: 出现异常没有被拦截

全局异常处理器里面没有配置Exception拦截器, 只配置俩个自定义异常类并extends RuntimeException,而程序抛出却是Exception异常。

错误二:程序抛出的是TargetServerException,但是被Exception拦截

  public void service2() {
        try {
            System.out.println(1 / 0);
        } catch (Exception e) {
            throw new TargetServerException("出现错误");
        }
    }


    public void service1() {
        try {
            service2();
        } finally {
            // 最终拦截Exception
            System.out.println(1 / 0);
        }
    }    
    
    public void service1() {
        try {
            service2();
        } catch (Exception e){
           throw new ServerPrepareException("最终异常类型");
        }
    }
案例1:
程序可能是controller-> service1-> service2 -> dao , service2抛出TargetServerException,
但是serice1进行tray catch{抛出Exception异常。。。异常会一直向上抛出,返回最终的异常类,才能被全局异常处理器拦截住最终的类型,然后执行代码块}
案例2:
程序可能是controller-> service1-> service2 -> dao , service2抛出TargetServerException,
但是serice1没有try catch{},但是有finally{这里面的代码出现异常并抛出Exception异常},全局异常处理器只会拦截到Exception,然后执行代码块

错误三: 全局异常处理器拦截代码中出现的异常不会被全局异常处理器再次处理


    /**
     * @Description: 目标服务器错误
     * @Params:
     * @Return:
     * @Author: Mr.myq
     * @Date: 2023/1/410:21
     */
    @ExceptionHandler(TargetServerException.class)
    public void targetServerException(TargetServerException e) {
        log.info("全局异常拦截器=》{}",e.toString());
        String message = e.getMessage();
        CmsOtherWarnLogEntity entity = new CmsOtherWarnLogEntity();
        entity.setSupplier(e.getSupplier());
        entity.setFileName(e.getFileName());
        entity.setWarnLog(message);
        entity.setCreateTime(LocalDateTime.now().toString());
        // 这里出现错误不会再次被拦截
        cmsOtherWarnLogService.addOtherWarnLogEntity(entity);
    }


经过一下学习可以放心大胆的使用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心有城府,腹有良谋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值