微服务的全局异常处理器

    技术2022-07-11  83

    如果是dubbo服务 直接在对外暴露服务的哪个模块添加该类即可如果是springboot微服务 我们可以把他配置到一个基础工程里 这个工程没有启动类 提供公用的util jar包等等 也不需要注册到注册中心并且在其他项目的启动类添加如下配置 我在网关配置的 目的是为了让服务知道该配置 否则不生效ApiReturnObject是一个统一返回值类型 可以根据自己喜好进行定义 这里就不贴代码了 package com.softdev.config; import com.softdev.common.config.BusinessException; import com.softdev.common.config.CommonErrorCode; import com.softdev.common.config.ErrorCode; import com.softdev.common.utils.ApiReturnObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 全局异常处理器 * @author Administrator * @version 1.0 **/ @ControllerAdvice//与@Exceptionhandler配合使用实现全局异常处理 public class GlobalExceptionHandler { private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class); //捕获Exception异常 @ExceptionHandler(value = Exception.class) @ResponseBody @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public ApiReturnObject processExcetion(HttpServletRequest request, HttpServletResponse response, Exception e){ //解析异常信息 //如果是系统自定义异常,直接取出errCode和errMessage if(e instanceof BusinessException){ LOGGER.info(e.getMessage(),e); //解析系统自定义异常信息 BusinessException businessException= (BusinessException) e; ErrorCode errorCode = businessException.getErrorCode(); //错误代码 int code = errorCode.getCode(); //错误信息 String desc = errorCode.getDesc(); return new ApiReturnObject(String.valueOf(code),desc,""); } LOGGER.error("系统异常:",e); //统一定义为99999系统未知错误 return new ApiReturnObject(String.valueOf(CommonErrorCode.UNKOWN.getCode()),CommonErrorCode.UNKOWN.getDesc()); } } 在如上有了全局处理器后 我们可以自定义一个异常类和枚举类自定义异常类是可预判的 比如前端传参不正确等等可以抛出自定义异常 package com.softdev.common.config; public class BusinessException extends RuntimeException { private static final long serialVersionUID = 5565760508056698922L; private ErrorCode errorCode; public BusinessException(ErrorCode errorCode) { super(); this.errorCode = errorCode; } public BusinessException() { super(); } public BusinessException(String arg0, Throwable arg1, boolean arg2, boolean arg3) { super(arg0, arg1, arg2, arg3); } public BusinessException(ErrorCode errorCode, String arg0, Throwable arg1, boolean arg2, boolean arg3) { super(arg0, arg1, arg2, arg3); this.errorCode = errorCode; } public BusinessException(String arg0, Throwable arg1) { super(arg0, arg1); } public BusinessException(ErrorCode errorCode, String arg0, Throwable arg1) { super(arg0, arg1); this.errorCode = errorCode; } public BusinessException(String arg0) { super(arg0); } public BusinessException(ErrorCode errorCode, String arg0) { super(arg0); this.errorCode = errorCode; } public BusinessException(Throwable arg0) { super(arg0); } public BusinessException(ErrorCode errorCode, Throwable arg0) { super(arg0); this.errorCode = errorCode; } public ErrorCode getErrorCode() { return errorCode; } public void setErrorCode(ErrorCode errorCode) { this.errorCode = errorCode; } } 枚举类 规范抛出的异常类型 package com.softdev.common.config; /** * 异常编码 0成功、-1熔断、 -2 标准参数校验不通过 -3会话超时 * 前两位:服务标识 * 中间两位:模块标识 * 后两位:异常标识 */ public enum CommonErrorCode implements ErrorCode { 公用异常编码 // /** * 传入参数与接口不匹配 */ E_100101(100101,"传入参数与接口不匹配"), /** * 验证码错误 */ E_100102(100102,"验证码错误"), /** * 验证码为空 */ E_100103(100103,"验证码为空"), /** * 查询结果为空 */ E_100104(100104,"查询结果为空"), /** * ID格式不正确或超出Long存储范围 */ E_100105(100105,"ID格式不正确或超出Long存储范围"), /** * 上传出错 */ E_100106(100106,"上传错误"), E_100107(100107,"发送验证码错误"), E_100108(100108,"入参不全"), 特殊异常编码/ E_999991(999991,"调用微服务-授权服务 被熔断"), E_999992(999992,"调用微服务-用户服务 被熔断"), E_999993(999993,"调用微服务-资源服务 被熔断"), E_999994(999994,"调用微服务-同步服务 被熔断"), E_999910(999910,"调用微服务-没有传tenantId租户Id"), E_999911(999911,"调用微服务-没有json-token令牌"), E_999912(999912,"调用微服务-json-token令牌解析有误"), E_999913(999913,"调用微服务-json-token令牌有误-没有当前租户信息"), E_999914(999914,"调用微服务-json-token令牌有误-该租户下没有权限信息"), E_NO_AUTHORITY(999997,"没有访问权限"), CUSTOM(999998,"自定义异常"), /** * 未知错误 */ UNKOWN(999999,"未知错误"); private int code; private String desc; public int getCode() { return code; } public String getDesc() { return desc; } private CommonErrorCode(int code, String desc) { this.code = code; this.desc = desc; } public static CommonErrorCode setErrorCode(int code) { for (CommonErrorCode errorCode : CommonErrorCode.values()) { if (errorCode.getCode()==code) { return errorCode; } } return null; } } package com.softdev.common.config; public interface ErrorCode { int getCode(); String getDesc(); }
    Processed: 0.011, SQL: 9