异常处理应包括哪些异常?以及将日志写入数据库的时间和方法

时间:2019-05-07 09:59:19

标签: java spring-boot

处理程序

    @ControllerAdvice
    @ResponseBody
    @Slf4j
    public class GlobalExceptionHandler  {
        @ExceptionHandler(value=Exception.class)
        public Response<String> exceptionHandler(HttpServletRequest request, Exception e) {
            Log logger=new Log();
            logger.setCreatedAt(LocalDateTime.now());
           // logger.setApiName();
           // logger.setData();
           // logger.setResult();
            if (e instanceof GlobalException) {
                GlobalException ex = (GlobalException) e;
                return Response.error(ex.getMessage());
            } else if (e instanceof BindException) {
                BindException ex = (BindException) e;
                List<ObjectError> errors = ex.getAllErrors();
                ObjectError error = errors.get(0);
                String msg = error.getDefaultMessage();
                return Response.error(msg);//to do
            } else {
                return Response.error(errorSys.getMsg());
            }
        }

例外

public class GlobalException extends RuntimeException {
    private ApiHttpCode apiHttpCode;
    public GlobalException(ApiHttpCode apiHttpCode){
       super(apiHttpCode.toString());
       this.apiHttpCode=apiHttpCode;
    }
    public ApiHttpCode getApiHttpCode(){
        return apiHttpCode;
    }
}

log

public class Log {
    private String id;
    private String apiName;
    private String data;
    private String result;
    private String requestId;
    private LocalDateTime createdAt;
}

0 个答案:

没有答案