use of com.guhanjie.exception.WebException in project weixin-boot by guhanjie.
the class BaseController method fail.
/**
* 返回异常失败,并带相关失败消息
* @param response 用于设置status=299,不需要时可设置传null
* @param e 抛出的异常
* @return {<br>"success": false<br>"result": false<br>"code": 错误编号<br>"error": 错误消息<br>"description": 页面显示错误消息<br>"cause": 调试信息<br>}
*/
protected Map<String, Object> fail(HttpServletResponse response, Exception e) {
// 平台异常
if (e instanceof WebException) {
WebException e2 = (WebException) e;
Integer code = e2.getCode();
String message = e2.getMessage();
String description = e2.getScreenMessage() == null ? e2.getMessage() : e2.getScreenMessage();
String causeMessage = e2.getCauseMessage();
if (response != null) {
response.setStatus(e2.getHttpStatus());
}
return fail(null, code, message, description, causeMessage);
} else // 系统异常
{
if (response != null) {
response.setStatus(500);
}
Integer code = WebExceptionEnum.SYSTEM_ERROR.getCode();
String message = WebExceptionEnum.SYSTEM_ERROR.getMessage();
String description = WebExceptionEnum.SYSTEM_ERROR.getScreenMessage();
String debugMessage = e.getMessage();
if (StringUtils.isBlank(debugMessage)) {
debugMessage = e.getClass().getSimpleName();
} else {
debugMessage = debugMessage.trim();
int index = debugMessage.indexOf('\n');
if (index == -1) {
index = debugMessage.length();
}
if (index > 0) {
debugMessage = debugMessage.substring(0, index % 200);
}
}
return fail(null, code, message, description, debugMessage);
}
}
Aggregations