天天看点

springcloud统一异常处理

异常处理流程

springcloud统一异常处理

@ControllerAdvice

public class BaseExceptionHandler {

@ExceptionHandler(value = Exception.class)
@ResponseBody
public Result error(Exception e){
    e.printStackTrace();        
    return new Result(false, StatusCode.ERROR, "系统正忙!程序员正在骑马赶来的路上!");
}

@ExceptionHandler(value = CustomException.class)
@ResponseBody
public Result custom(CustomException e){
    e.printStackTrace();
    return new Result(false, StatusCode.ERROR, e.getMessage());
}
           

}

public class CustomException extends RuntimeException {
    private String message;

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public CustomException(String message) {
        this.message = message;
    }

    public CustomException() {
    }
}

           

继续阅读