Search in sources :

Example 1 with WrapperResponse

use of com.mendmix.common.model.WrapperResponse in project jeesuite-libs by vakinge.

the class GlobalExceptionHandler method exceptionHandler.

@ExceptionHandler(Exception.class)
@ResponseBody
public WrapperResponse<?> exceptionHandler(Exception e, HttpServletResponse response) {
    // 缓存回滚
    if (rollbackCacheMethod != null) {
        try {
            rollbackCacheMethod.invoke(null);
        } catch (Exception e2) {
        }
    }
    WrapperResponse<?> resp = new WrapperResponse<>();
    e = (Exception) getActualThrowable(e);
    if (e instanceof JeesuiteBaseException) {
        JeesuiteBaseException e1 = (JeesuiteBaseException) e;
        resp.setCode(e1.getCode());
        resp.setMsg(e1.getMessage());
    } else if (e instanceof org.springframework.web.HttpRequestMethodNotSupportedException) {
        resp.setCode(HttpStatus.METHOD_NOT_ALLOWED.value());
        resp.setMsg(e.getMessage());
    } else if (e instanceof org.springframework.web.HttpMediaTypeException) {
        resp.setCode(HttpStatus.UNSUPPORTED_MEDIA_TYPE.value());
        resp.setMsg(e.getMessage());
    } else if (e instanceof org.springframework.web.bind.MissingServletRequestParameterException) {
        resp.setCode(1001);
        resp.setMsg(e.getMessage());
    } else if (e instanceof MethodArgumentNotValidException) {
        resp.setCode(400);
        List<ObjectError> errors = ((MethodArgumentNotValidException) e).getBindingResult().getAllErrors();
        String fieldName;
        StringBuilder fieldNames = new StringBuilder();
        for (ObjectError error : errors) {
            String errMsg = error.getDefaultMessage();
            fieldName = parseFieldName(error);
            fieldNames.append(fieldName).append(",");
        }
        resp.setBizCode("error.parameter.notValid");
        resp.setMsg("参数错误[" + fieldNames.toString() + "]");
    } else {
        Throwable parent = e.getCause();
        if (parent instanceof IllegalStateException) {
            resp.setCode(501);
            resp.setMsg(e.getMessage());
        } else {
            resp.setCode(500);
            resp.setMsg("系统繁忙");
        }
    }
    // 默认情况http code都转换为200,异常信息由异常体传递
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    if (request != null && Boolean.parseBoolean(request.getHeader(CustomRequestHeaders.HEADER_HTTP_STATUS_KEEP))) {
        int errorCode = resp.getCode();
        if (errorCode >= 400 && errorCode <= 500) {
            response.setStatus(errorCode);
        } else {
            response.setStatus(500);
        }
    }
    response.addIntHeader(CustomRequestHeaders.HEADER_EXCEPTION_CODE, resp.getCode());
    response.addHeader(CustomRequestHeaders.HEADER_RESP_KEEP, Boolean.TRUE.toString());
    // 
    ActionLogCollector.onResponseEnd(response.getStatus(), e);
    return resp;
}
Also used : ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) HttpServletRequest(javax.servlet.http.HttpServletRequest) WrapperResponse(com.mendmix.common.model.WrapperResponse) ObjectError(org.springframework.validation.ObjectError) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with WrapperResponse

use of com.mendmix.common.model.WrapperResponse in project jeesuite-libs by vakinge.

the class ReactiveGlobalExceptionHandler method exceptionHandler.

@ExceptionHandler(Exception.class)
@ResponseBody
public WrapperResponse<?> exceptionHandler(ServerHttpRequest request, ServerHttpResponse response, Exception e) {
    WrapperResponse<?> resp = new WrapperResponse<>();
    e = (Exception) getActualThrowable(e);
    if (e instanceof JeesuiteBaseException) {
        JeesuiteBaseException e1 = (JeesuiteBaseException) e;
        resp.setCode(e1.getCode());
        resp.setMsg(e1.getMessage());
    } else if (e instanceof MethodArgumentNotValidException) {
        resp.setCode(400);
        List<ObjectError> errors = ((MethodArgumentNotValidException) e).getBindingResult().getAllErrors();
        String fieldName;
        StringBuilder fieldNames = new StringBuilder();
        for (ObjectError error : errors) {
            fieldName = parseFieldName(error);
            fieldNames.append(fieldName).append(",");
        }
        resp.setBizCode("error.parameter.notValid");
        resp.setMsg("参数错误[" + fieldNames.toString() + "]");
    } else {
        Throwable parent = e.getCause();
        if (parent instanceof IllegalStateException) {
            resp.setCode(501);
            resp.setMsg(e.getMessage());
        } else {
            resp.setCode(500);
            resp.setMsg("系统繁忙");
        }
    }
    // 
    ActionLogCollector.onResponseEnd(response.getRawStatusCode(), e);
    return resp;
}
Also used : WrapperResponse(com.mendmix.common.model.WrapperResponse) ObjectError(org.springframework.validation.ObjectError) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) List(java.util.List) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

JeesuiteBaseException (com.mendmix.common.JeesuiteBaseException)2 WrapperResponse (com.mendmix.common.model.WrapperResponse)2 ObjectError (org.springframework.validation.ObjectError)2 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)2 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 List (java.util.List)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)1