use of com.jeesuite.springweb.model.WrapperResponseEntity in project jeesuite-libs by vakinge.
the class GlobalExceptionHandler method exceptionHandler.
@ExceptionHandler(Exception.class)
@ResponseBody
public WrapperResponseEntity exceptionHandler(Exception e, HttpServletResponse response) {
// 缓存回滚
if (rollbackCacheMethod != null) {
try {
rollbackCacheMethod.invoke(null);
} catch (Exception e2) {
}
}
WrapperResponseEntity resp = new WrapperResponseEntity();
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) {
List<ObjectError> errors = ((MethodArgumentNotValidException) e).getBindingResult().getAllErrors();
StringBuffer errorMsg = new StringBuffer();
errors.stream().forEach(x -> errorMsg.append(x.getDefaultMessage()).append(";"));
resp.setCode(400);
resp.setMsg(errorMsg.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,异常信息由异常体传递
if (Boolean.parseBoolean(CurrentRuntimeContext.getRequest().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, e);
return resp;
}
use of com.jeesuite.springweb.model.WrapperResponseEntity in project jeesuite-libs by vakinge.
the class GlobalExceptionHandler method exceptionHandler.
@ExceptionHandler(Exception.class)
@ResponseBody
public WrapperResponseEntity exceptionHandler(Exception e, HttpServletResponse response) {
// 缓存回滚
if (rollbackCacheMethod != null) {
try {
rollbackCacheMethod.invoke(null);
} catch (Exception e2) {
}
}
WrapperResponseEntity resp = new WrapperResponseEntity();
if (e.getCause() != null && e.getCause() instanceof JeesuiteBaseException) {
JeesuiteBaseException e1 = (JeesuiteBaseException) e.getCause();
resp.setCode(e1.getCode());
resp.setMsg(e1.getMessage());
} else 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 {
Throwable parent = e.getCause();
if (parent instanceof IllegalStateException) {
resp.setCode(501);
resp.setMsg(e.getMessage());
} else {
resp.setCode(500);
resp.setMsg("系统繁忙");
}
logger.error("", e);
}
if (resp.getCode() >= 400 && resp.getCode() <= 500) {
response.setStatus(resp.getCode());
}
return resp;
}
Aggregations