java统一异常处理_java异常统一处理

一般系统抛出的错误不含错误代码,出去部分的404,500,400之外,我们如果想吧错误代码定义的更细致,就需要自己继承RuntimeExeption这个类后,重新定义构造方法定义自己的错误信息。

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

public class DescribeException extends RuntimeException{

private Integer code;

/**

* 继承exception,加入错误状态值

* @param exceptionEnum

*/

public DescribeException(ExceptionEnum exceptionEnum) {

super(exceptionEnum.getMsg());

this.code = exceptionEnum.getCode();

}

/**

* 自定义错误信息

* @param message

* @param code

*/

public DescribeException(String message, Integer code) {

super(message);

this.code = code;

}

public Integer getCode() {

return code;

}

public void setCode(Integer code) {

this.code = code;

}

}

View Code

使用一个handler来判定我们try。。。catch。。。的错误是我们已知的错误还是未知的,如果已知,返回错误,未知返回未知错误和记录日志,留着以后查找错误。

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

@ControllerAdvice

public class ExceptionHandle {

private final static Logger LOGGER = LoggerFactory.getLogger(ExceptionHandle.class);

/**

* 判断错误是否是已定义的已知错误,不是则由未知错误代替,同时记录在log中

* @param e

* @return

*/

@ExceptionHandler(value = Exception.class)

@ResponseBody

public Result exceptionGet(Exception e){

if(e instanceof DescribeException){

DescribeException MyException = (DescribeException) e;

return ResultUtil.error(MyException.getCode(),MyException.getMessage());

}

LOGGER.error("【系统异常】{}",e);

return ResultUtil.error(ExceptionEnum.UNKNOW_ERROR);

}

}

View Code

这样我们就不用返回日志到前端了,让用户感觉很奇怪。

但是这时候调用接口出现异常,我们怎么来判断是前端还是后端造成的问题呢。这时候就要用aop进行拦截

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

@Aspect

@Component

public class HttpAspect {

private final static Logger LOGGER = LoggerFactory.getLogger(HttpAspect.class);

@Autowired

private ExceptionHandle exceptionHandle;

@Pointcut("execution(public * com.zzp.controller.*.*(..))")

public void log(){

}

@Before("log()")

public void doBefore(JoinPoint joinPoint){

ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

HttpServletRequest request = attributes.getRequest();

//url

LOGGER.info("url={}",request.getRequestURL());

//method

LOGGER.info("method={}",request.getMethod());

//ip

LOGGER.info("id={}",request.getRemoteAddr());

//class_method

LOGGER.info("class_method={}",joinPoint.getSignature().getDeclaringTypeName() + "," + joinPoint.getSignature().getName());

//args[]

LOGGER.info("args={}",joinPoint.getArgs());

}

@Around("log()")

public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

Result result = null;

try {

} catch (Exception e) {

return exceptionHandle.exceptionGet(e);

}

if(result == null){

return proceedingJoinPoint.proceed();

}else {

return result;

}

}

@AfterReturning(pointcut = "log()",returning = "object")//打印输出结果

public void doAfterReturing(Object object){

LOGGER.info("response={}",object.toString());

}

}

View Code

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值