浏览代码

异常处理

Progr1mmer 6 年之前
父节点
当前提交
05930064e2

+ 15 - 55
common/common-web/src/main/java/com/yihu/jw/web/exception/ApiException.java

@ -1,74 +1,34 @@
package com.yihu.jw.web.exception;
import org.springframework.http.HttpStatus;
/**
 * API 异常。使用错误代码初始化,并可接收用于补充错误消息的参数。
 * 用于描述错误代码的信息配置在各服务配置文件中,并由服务配置中心统一管理。
 *
 * 错误描述结构,结构(字段errors对资源而言,REST规范错误不包含此结构):
 * {
 *    "message": "Validation Failed",
 *    "document_url": "https://ehr.yihu.com/docs/api/somewhere"
 *    "errors": [
 *        {
 *            "resource": "User",
 *            "field": "title",
 *            "code": "missing_field"
 *        }
 *    ]
 * }
 *
 * @author Sand
 * @version 1.0
 * @created 2015.12.20 16:05
 */
public class ApiException extends RuntimeException {
    private HttpStatus httpStatus; //Http 状态码,默认请求成功
    private int errorCode; //用于从配置环境中提取错误信息
    private String message; //错误消息
    private String documentURL; //文档连接
    private String errorDesc; //错误消息
    private int errorCode; //错误码
    public ApiException(String message) {
        this(-1, message);
    public ApiException(String errorDesc) {
        this(errorDesc, 500);
    }
    public ApiException(int errorCode, String message) {
        this(HttpStatus.OK, errorCode, message);
    }
    public ApiException(HttpStatus httpStatus, String message){
        this(httpStatus, -1, message, null);
    }
    public ApiException(HttpStatus httpStatus, int errorCode, String message){
        this(httpStatus, errorCode, message, null);
    }
    public ApiException(HttpStatus httpStatus, int errorCode, String message, String documentURL){
        super(message);
        this.httpStatus = httpStatus;
    public ApiException(String errorDesc, int errorCode) {
        super(errorDesc);
        this.errorDesc = errorDesc;
        this.errorCode = errorCode;
        this.message = message;
        this.documentURL = documentURL;
    }
    public HttpStatus httpStatus() {
        return httpStatus;
    public String getErrorDesc() {
        return errorDesc;
    }
    public Integer errorCode() {
        return errorCode;
    public void setErrorDesc(String errorDesc) {
        this.errorDesc = errorDesc;
    }
    public String documentURL() {
        return documentURL;
    public int getErrorCode() {
        return errorCode;
    }
    @Override
    public String getMessage() {
        return message;
    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }
}

+ 6 - 12
common/common-web/src/main/java/com/yihu/jw/web/handler/GlobalExceptionHandler.java

@ -33,30 +33,24 @@ public class GlobalExceptionHandler {
    @ResponseBody
    public Envelop handle(HttpServletResponse response, Exception e) throws IOException {
        Envelop envelop = new Envelop();
        if (e instanceof NoHandlerFoundException) {
            //response.setStatus(HttpStatus.NOT_FOUND.value());
        if (e instanceof NoHandlerFoundException) { //404
            envelop.setDesc(e.getMessage());
            envelop.setCode(HttpStatus.NOT_FOUND.value());
        } else if (e instanceof HttpRequestMethodNotSupportedException){
            //response.setStatus(HttpStatus.METHOD_NOT_ALLOWED.value());
        } else if (e instanceof HttpRequestMethodNotSupportedException){ //请求方法有误
            envelop.setDesc(e.getMessage());
            envelop.setCode(HttpStatus.METHOD_NOT_ALLOWED.value());
        } else if (e instanceof MissingServletRequestParameterException) {
            //response.setStatus(HttpStatus.BAD_REQUEST.value());
        } else if (e instanceof MissingServletRequestParameterException) { //参数有误
            envelop.setDesc(e.getMessage());
            envelop.setCode(HttpStatus.BAD_REQUEST.value());
        } else if (e instanceof FeignException) { //执行Feign失败的时候默认当前服务做为网关执行请求的时候,从上游服务器接收到无效的响应
            //response.setStatus(HttpStatus.BAD_GATEWAY.value());
            envelop.setDesc("Execute Feign: " + e.getMessage());
            envelop.setCode(HttpStatus.BAD_GATEWAY.value());
        } else if (e instanceof ApiException) { //业务逻辑的异常默认为请求成功,但是前端需要判断成功标识
        } else if (e instanceof ApiException) { //业务逻辑异常
            ApiException apiException = (ApiException) e;
            //response.setStatus(apiException.httpStatus().value());
            envelop.setDesc(e.getMessage());
            envelop.setCode(apiException.errorCode());
            envelop.setDesc(apiException.getErrorDesc());
            envelop.setCode(apiException.getErrorCode());
            return envelop; //此异常不进行日志记录
        } else {
            //response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            envelop.setDesc(e.getMessage());
            envelop.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        }