統(tǒng)一異常處理

無(wú)標(biāo)題-2023-07-14-1503.png
//異常信息
@Getter  
public enum ResponseCode {  

    SUCCESS(0, "Success"),  

    INTERNAL_ERROR(1, "服務(wù)器內(nèi)部錯(cuò)誤"),  

    USER_INPUT_ERROR(2, "用戶(hù)輸入錯(cuò)誤"),  

    AUTHENTICATION_NEEDED(3, "Token過(guò)期或無(wú)效"),  

    FORBIDDEN(4, "禁止訪(fǎng)問(wèn)"),  

    TOO_FREQUENT_VISIT(5, "訪(fǎng)問(wèn)太頻繁,請(qǐng)休息一會(huì)兒");  

    private final int code;  

    private final String message;  

    private final Response.Status status;  

    ResponseCode(int code, String message, Response.Status status) {  
        this.code = code;  
        this.message = message;  
        this.status = status;  
    }  

    ResponseCode(int code, String message) {  
        this(code, message, Response.Status.INTERNAL_SERVER_ERROR);  
    }  

}
/**
返回消息類(lèi)
**/
@Getter  
@Setter  
public class GenericResponse<T> {  

    private int code;  

    private T data;  

    private String message;  

    public GenericResponse() {};  

    public GenericResponse(int code, T data) {  
        this.code = code;  
        this.data = data;  
    }  

    public GenericResponse(int code, T data, String message) {  
        this(code, data);  
        this.message = message;  
    }  

    public GenericResponse(ResponseCode responseCode) {  
        this.code = responseCode.getCode();  
        this.data = null;  
        this.message = responseCode.getMessage();  
    }  

    public GenericResponse(ResponseCode responseCode, T data) {  
        this(responseCode);  
        this.data = data;  
    }  

    public GenericResponse(ResponseCode responseCode, T data, String message) {  
        this(responseCode, data);  
        this.message = message;  
    }  
}

/**
自定義異常
**/

@Getter  
public class AuroraRuntimeException extends RuntimeException {  

    private final ResponseCode code;  

    public AuroraRuntimeException() {  
        super(String.format("%s", ResponseCode.INTERNAL_ERROR.getMessage()));  
        this.code = ResponseCode.INTERNAL_ERROR;  
    }  

    public AuroraRuntimeException(Throwable e) {  
        super(e);  
        this.code = ResponseCode.INTERNAL_ERROR;  
    }  

    public AuroraRuntimeException(String msg) {  
        this(ResponseCode.INTERNAL_ERROR, msg);  
    }  

    public AuroraRuntimeException(ResponseCode code) {  
        super(String.format("%s", code.getMessage()));  
        this.code = code;  
    }  

    public AuroraRuntimeException(ResponseCode code, String msg) {  
        super(msg);  
        this.code = code;  
    }  

}

/**
全局Rest異常處理類(lèi)
**/
Log4j2  
@RestControllerAdvice  
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {  

    /**  
     * 定義要捕獲的異常 可以多個(gè) @ExceptionHandler({})     *  
     * @param request  request  
     * @param e        exception  
     * @param response response  
     * @return 響應(yīng)結(jié)果  
     */  
    @ExceptionHandler(AuroraRuntimeException.class)  
    public GenericResponse customExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {  
        AuroraRuntimeException exception = (AuroraRuntimeException) e;  

       if (exception.getCode() == ResponseCode.USER_INPUT_ERROR) {  
           response.setStatus(HttpStatus.BAD_REQUEST.value());  
       } else if (exception.getCode() == ResponseCode.FORBIDDEN) {  
           response.setStatus(HttpStatus.FORBIDDEN.value());  
       } else {  
           response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());  
       }  

        return new GenericResponse(exception.getCode(), null, exception.getMessage());  
    }  

    @ExceptionHandler(NotLoginException.class)  
    public GenericResponse tokenExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {  
        log.error("token exception", e);  
        response.setStatus(HttpStatus.FORBIDDEN.value());  
        return new GenericResponse(ResponseCode.AUTHENTICATION_NEEDED);  
    }  

}
/**
用戶(hù)拋異常
**/
public User getUserInfo(Long userId) {  
    // some logic

    User user = daoFactory.getExtendedUserMapper().selectByPrimaryKey(userId);  
    if (user == null) {  
        throw new AuroraRuntimeException(ResponseCode.USER_INPUT_ERROR, "用戶(hù)id不存在");  
    }

    // some logic
    ....
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容