同一异常

Posted cabinet

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了同一异常相关的知识,希望对你有一定的参考价值。

@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("************************异常结束*******************************");
}









































以上是关于同一异常的主要内容,如果未能解决你的问题,请参考以下文章

片段中的Android致命异常

mvn命令异常:An error has occurred in Javadoc report generation: Unable to find javadoc command异常已解决(代码片段

mvn命令异常:An error has occurred in Javadoc report generation: Unable to find javadoc command异常已解决(代码片段

防止导航到同一个片段

springcloud报错-------关于 hystrix 的异常 FallbackDefinitionException:fallback method wasn't found(代码片段

片段中的getView()导致抛出异常,不确定原因