0


Mabatis处理异常屏蔽SQL返回前端全局异常捕获处理

文章目录

Mabatis处理异常屏蔽SQL返回前端全局异常捕获处理

结论

在全局异常处理类中添加MyBatisSystemException即可单独对MyBatis中和数据库操作相关异常操作进行全局处理,同时屏蔽sql内容,只返回文字 “服务错误,请联系系统管理员” 给前端。

@Slf4j@ControllerAdvicepublicclassExceptionHandlerAdvice{/**
     * Sql查询失败在spring的包装下会统一抛出非受检异常,单独捕获,防止sql语句被返回给前端
     */@ResponseBody@ExceptionHandler(MyBatisSystemException.class)publicObjecthandleBindException(HttpServletRequest req,MyBatisSystemException e){String path ="http://"+req.getRemoteAddr()+":"+req.getServerPort()+ req.getRequestURI();
        log.error("访问 "+path +"报错,报错信息为: "+ e.getMessage(), e);returnnewBaseResult<>(CodeEnum.E500,false,"服务错误,请联系系统管理员。");}//拦截所有Exception,展示Error页面@ResponseBody@ExceptionHandler({Exception.class})publicBaseResulterrorHandler(HttpServletRequest req,Exception e){String path ="http://"+req.getRemoteAddr()+":"+req.getServerPort()+ req.getRequestURI();
        log.error("访问 "+path +"报错,报错信息为: "+ e.getMessage(), e);returnnewBaseResult<>(CodeEnum.E500,false, e.getMessage());}}

1 java异常体系

在这里插入图片描述

1.Throwable

所有的异常都是Throwable的直接或者间接子类。Throwable有两个直接子类,Error和Exception。

2.Error

Error是错误,对于所有的编译时期的错误以及系统错误都是通过Error抛出的。这些错误表示故障发生于虚拟机自身、或者发生在虚拟机试图执行应用时,如Java虚拟机运行错误(Virtual MachineError)、类定义错误(NoClassDefFoundError)等。这些错误是不可查的,因为它们在应用程序的控制和处理能力之 外,而且绝大多数是程序运行时不允许出现的状况。对于设计合理的应用程序来说,即使确实发生了错误,本质上也不应该试图去处理它所引起的异常状况。在 Java中,错误通过Error的子类描述。

3.Exception

它规定的异常是程序本身可以处理的异常。异常和错误的区别是,异常是可以被处理的,而错误是没法处理的。

4.Checked Exception【受检异常】

可检查的异常,这是编码时非常常用的,所有checked exception都是需要在代码中处理的。它们的发生是可以预测的,正常的一种情况,可以合理的处理。例如IOException。

5.Unchecked Exception【非受检异常】

RuntimeException及其子类都是unchecked exception。比如NPE空指针异常,除数为0的算数异常ArithmeticException等等,这种异常是运行时发生,无法预先捕捉处理的。Error也是unchecked exception,也是无法预先处理的。

参考:https://juejin.cn/post/6965407291260534820

2 Spring框架异常处理

Spring 提供方便的 API 把具体技术相关的异常(比如由JDBOHibernate or JDO 抛出的)转化为一致的 unchecked 异常。

3 定位Spring框架转化为哪种unchecked异常

3.1 捕获RuntimeException定位Spring框架转化抛出的异常类

直接在ExceptionHandlerAdvice中捕获RuntimeException,然后DEBUG,查看异常class类型,发现都是继承自MyBatisSystemException

在这里插入图片描述

3.2 进一步查看包名判断

进一步查看包名发现为org.springframework.dao,基本可以判定捕获MyBatisSystemException可以实现要求

packageorg.mybatis.spring;importorg.springframework.dao.UncategorizedDataAccessException;publicclassMyBatisSystemExceptionextendsUncategorizedDataAccessException{privatestaticfinallong serialVersionUID =-5284728621670758939L;publicMyBatisSystemException(Throwable cause){super((String)null, cause);}}

3.3 识别MyBatisSystemException下级实现

MyBatisSystemException目前没有下级实现类

3.3 识别MyBatisSystemException继承实现

可以看到继承父类均为abstract修饰,一直到NestedRuntimeException继承RuntimeException。则已经找到MyBatisSystemException的所有上级继承父类,进一步确认MyBatisSystemException符合作为全局异常捕获ExceptionHandler的最上级实现异常类型,而不会漏异常捕获。

packageorg.springframework.dao;importorg.springframework.lang.Nullable;publicabstractclassUncategorizedDataAccessExceptionextendsNonTransientDataAccessException{publicUncategorizedDataAccessException(@NullableString msg,@NullableThrowable cause){super(msg, cause);}}
packageorg.springframework.dao;importorg.springframework.lang.Nullable;publicabstractclassNonTransientDataAccessExceptionextendsDataAccessException{publicNonTransientDataAccessException(String msg){super(msg);}publicNonTransientDataAccessException(@NullableString msg,@NullableThrowable cause){super(msg, cause);}}
packageorg.springframework.dao;importorg.springframework.core.NestedRuntimeException;importorg.springframework.lang.Nullable;publicabstractclassDataAccessExceptionextendsNestedRuntimeException{publicDataAccessException(String msg){super(msg);}publicDataAccessException(@NullableString msg,@NullableThrowable cause){super(msg, cause);}}
packageorg.springframework.core;importorg.springframework.lang.Nullable;publicabstractclassNestedRuntimeExceptionextendsRuntimeException{privatestaticfinallong serialVersionUID =5439915454935047936L;publicNestedRuntimeException(String msg){super(msg);}publicNestedRuntimeException(@NullableString msg,@NullableThrowable cause){super(msg, cause);}@NullablepublicStringgetMessage(){returnNestedExceptionUtils.buildMessage(super.getMessage(),this.getCause());}@NullablepublicThrowablegetRootCause(){returnNestedExceptionUtils.getRootCause(this);}publicThrowablegetMostSpecificCause(){Throwable rootCause =this.getRootCause();return(Throwable)(rootCause !=null? rootCause :this);}publicbooleancontains(@NullableClass<?> exType){if(exType ==null){returnfalse;}elseif(exType.isInstance(this)){returntrue;}else{Throwable cause =this.getCause();if(cause ==this){returnfalse;}elseif(cause instanceofNestedRuntimeException){return((NestedRuntimeException)cause).contains(exType);}else{while(cause !=null){if(exType.isInstance(cause)){returntrue;}if(cause.getCause()== cause){break;}

                    cause = cause.getCause();}returnfalse;}}}static{NestedExceptionUtils.class.getName();}}}

                    cause = cause.getCause();}returnfalse;}}}static{NestedExceptionUtils.class.getName();}}
标签: sql 前端 数据库

本文转载自: https://blog.csdn.net/Master_Shifu_/article/details/134806717
版权归原作者 Master_Shifu_ 所有, 如有侵权,请联系我们删除。

“Mabatis处理异常屏蔽SQL返回前端全局异常捕获处理”的评论:

还没有评论