微服务下全局异常处理方案微服务下全局异常处理方案
1、新建jhjcn-common-base基础工程
2、自定义全局异常类BaseException
package com.jhjcn.common.base.exception;
/**
* @Deacription TODO
* @Author jianhua.hong
* @Date 2020/2/25 9:56
**/
public class BaseException extends RuntimeException {
private String code;
private String message;
public BaseException(String message) {
this.message = message;
}
public BaseException(String code, String message) {
this.code = code;
this.message = message;
}
public BaseException(Throwable cause) {
super(cause);
}
public String getCode() {
return code;
}
@Override
public String getMessage() {
return message;
}
}
3、异常工具类
package com.jhjcn.common.base.exception;
/**
* @Deacription TODO
* @Author jianhua.hong
* @Date 2019/11/19 19:41
**/
public class ExceptionUtils {
public static String getStackTraceString(Throwable ex) {
StackTraceElement[] traceElements = ex.getStackTrace();
StringBuilder traceBuilder = new StringBuilder();
if (traceElements != null && traceElements.length > 0) {
for (StackTraceElement traceElement : traceElements) {
traceBuilder.append(traceElement.toString());
traceBuilder.append("");
}
}
return traceBuilder.toString();
}
}
4、自定义异常处理抽象类BaseExceptionHandler
评论0
最新资源