zoukankan      html  css  js  c++  java
  • 同一异常

    @ControllerAdvice
    public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

    /**
    * 在controller里面内容执行之前,校验一些参数不匹配啊,Get post方法不对啊之类的
    */
    @Override
    protected ResponseEntity handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
    String msg = ex.getMessage();
    SysLog.error(msg);
    if(msg.indexOf("JSON parse error")>-1){
    msg = "JSON参数格式化错误!";
    }else if(msg.indexOf("not supported")>-1){
    msg = "数据参数传递类型错误!";
    }else{
    msg = msg.substring(msg.lastIndexOf("["));
    msg = msg.substring(1,msg.indexOf("]"));
    }
    ResultModel res = new ResultModel<>(Const.FAILURE,msg,ExceptionCode.FAILURE.getCode());
    return new ResponseEntity<>(res, HttpStatus.OK);

    }
    

    @ExceptionHandler(BizException.class)
    @ResponseBody
    public ResultModel bizExceptionHandler(HttpServletRequest request, BizException e) throws Exception {
    e.printStackTrace();

        ResultModel<String> result = new ResultModel();
        result.setState(Const.FAILURE);
        result.setMsgCode(e.getErrCode());
        result.setMsg(e.getErrMsg());
        if(e.getData()==null || e.getData().toString().trim().equals("")){
            result.setData(request.getRequestURL().toString());
        }else {
            if (e.getData() instanceof String) {
                result.setData(e.getData().toString());
            }else{
                result.setData(JSON.toJSONString(e.getData()));
            }
        }
        log(e, request);
    
        return result;
    }
    

    /**
    * 请求方法中校验抛出的异常
    * @param e
    * @return
    */
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseBody
    public ResultModel constraintViolationExceptionHandler(HttpServletRequest request,ConstraintViolationException e){
    //获取异常中第一个错误信息
    e.printStackTrace();

        ResultModel<String> result = new ResultModel();
        result.setState(Const.FAILURE);
        result.setMsgCode(ExceptionCode.FAILURE.getCode());
        result.setMsg(e.getMessage());
        result.setData(request.getRequestURL().toString());
    
        log(e, request);
    
        return result;
    }
    

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultModel jsonHandler(HttpServletRequest request, Exception e) throws Exception {
    e.printStackTrace();

        ResultModel<String> result = new ResultModel();
        result.setState(Const.FAILURE);
        result.setMsgCode(ExceptionCode.FAILURE.getCode());
        result.setMsg(e.getMessage());
        result.setData(request.getRequestURL().toString());
    
        log(e, request);
    
        return result;
    }
    
    private void log(Exception ex, HttpServletRequest request) {
        logger.error("************************异常开始*******************************");
    

    // if(getUser() != null)
    // logger.error("当前用户id是" + getUser().getUserId());
    logger.error(ex);
    logger.error("请求地址:" + request.getRequestURL());
    Enumeration enumeration = request.getParameterNames();
    logger.error("请求参数");
    while (enumeration.hasMoreElements()) {
    String name = enumeration.nextElement().toString();
    logger.error(name + "---" + request.getParameter(name));
    }

        StackTraceElement[] error = ex.getStackTrace();
        for (StackTraceElement stackTraceElement : error) {
            logger.error(stackTraceElement.toString());
        }
        logger.error("************************异常结束*******************************");
    }
  • 相关阅读:
    Vue单向数据流
    npm常用命令
    vue自定义指令
    slot的用法(Vue插槽)
    js闭包
    canvas 给画的多边形着色
    canvas画线
    canvas初体验
    canvas
    json
  • 原文地址:https://www.cnblogs.com/cabinet/p/13155044.html
  • Copyright © 2011-2022 走看看