美文网首页
统一异常处理

统一异常处理

作者: 超人001 | 来源:发表于2023-07-18 09:39 被阅读0次
无标题-2023-07-14-1503.png
//异常信息
@Getter  
public enum ResponseCode {  

    SUCCESS(0, "Success"),  

    INTERNAL_ERROR(1, "服务器内部错误"),  

    USER_INPUT_ERROR(2, "用户输入错误"),  

    AUTHENTICATION_NEEDED(3, "Token过期或无效"),  

    FORBIDDEN(4, "禁止访问"),  

    TOO_FREQUENT_VISIT(5, "访问太频繁,请休息一会儿");  

    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);  
    }  

}
/**
返回消息类
**/
@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异常处理类
**/
Log4j2  
@RestControllerAdvice  
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {  

    /**  
     * 定义要捕获的异常 可以多个 @ExceptionHandler({})     *  
     * @param request  request  
     * @param e        exception  
     * @param response response  
     * @return 响应结果  
     */  
    @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);  
    }  

}
/**
用户抛异常
**/
public User getUserInfo(Long userId) {  
    // some logic

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

    // some logic
    ....
}

相关文章

  • spring/springmvc 全局异常处理

    1.在项目中为什么要统一异常处理 当异常返回到前端页面的时候可以统一处理,避免前端无法处理异常 不做统一异常处理,...

  • 统一异常处理

    一、什么是统一异常处理 1、制造异常 2、什么是统一异常处理我们想让异常结果也显示为统一的返回结果对象,并且统一处...

  • springboot全局异常处理

    一、单个controller范围的异常处理/** 统一异常处理 @return*/@RequestMapping(...

  • springboot之统一异常处理

    spring统一异常处理 使用spring的统一异常处理,我们就不再需要在业务代码中就行显式的捕获异常处理,在da...

  • 统一异常处理

    1.定义返回结果对象 2.定义返回结果工具类, 3.枚举消息状态 .4.自定义异常类 5.统一异常拦截 6.业务中...

  • 统一异常处理

    首先,利用枚举,来定义异常类型。定义枚举ResultEnum: 自定义异常,新建CustomException类:...

  • 统一异常处理

    简要描述: 在web层controller业务代码中,一般不要处理任何异常,将异常完全跑出,由ExceptionH...

  • 统一异常处理

    Overview 利用Spring AOP思想,对项目中的异常进行统一处理。 实现思路 通过@Controller...

  • JAVA 异常控制

    文章 Spring RestFul API统一异常处理SpringBoot RESTful 应用中的异常处理小结S...

  • @ControllerAdvice

    @ExceptionHandler 异常统一处理 处理web请求中的异常 请求:http://localhos...

网友评论

      本文标题:统一异常处理

      本文链接:https://www.haomeiwen.com/subject/ffjnudtx.html