|
|
@@ -1,81 +1,88 @@
|
|
|
-package com.goafanti.common.error;
|
|
|
-
|
|
|
-import javax.annotation.Resource;
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.core.io.support.ResourcePropertySource;
|
|
|
-import org.springframework.web.servlet.ModelAndView;
|
|
|
-import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
|
|
|
-
|
|
|
-import com.goafanti.common.bo.Error;
|
|
|
-import com.goafanti.common.bo.Result;
|
|
|
-import com.goafanti.common.utils.LoggerUtils;
|
|
|
-import com.goafanti.common.utils.StringUtils;
|
|
|
-import com.goafanti.core.shiro.filter.ShiroFilterUtils;
|
|
|
-
|
|
|
-public class SystemExceptionResolver extends SimpleMappingExceptionResolver {
|
|
|
- @Resource(name = "errorResource")
|
|
|
- private ResourcePropertySource errorResource;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private ShiroFilterUtils shiroFilterUtils;
|
|
|
-
|
|
|
- @Override
|
|
|
- protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
|
|
|
- Exception ex) {
|
|
|
- // Expose ModelAndView for chosen error view.
|
|
|
- String viewName = determineViewName(ex, request);
|
|
|
-
|
|
|
- ex = handleErrorMessage(ex);
|
|
|
-
|
|
|
- if (viewName != null) {
|
|
|
- if (!shiroFilterUtils.isAjax(request)) {// JSP
|
|
|
- // Apply HTTP status code for error views, if specified.
|
|
|
- // Only apply it if we're processing a top-level request.
|
|
|
- Integer statusCode = determineStatusCode(request, viewName);
|
|
|
- if (statusCode != null) {
|
|
|
- applyStatusCodeIfPossible(request, response, statusCode);
|
|
|
- }
|
|
|
- return getModelAndView(viewName, ex, request);
|
|
|
- } else {// JSON
|
|
|
- Result res = new Result();
|
|
|
- res.getError().add(new Error(viewName, ex.getMessage()));
|
|
|
- shiroFilterUtils.out(response, res);
|
|
|
- return null;
|
|
|
- }
|
|
|
- } else {
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private Exception handleErrorMessage(Exception ex) {
|
|
|
- Error error = null;
|
|
|
- if (ex instanceof BusinessException) {
|
|
|
- BusinessException bex = (BusinessException) ex;
|
|
|
- error = new Error(bex.getCode(), bex.getField(), (String) errorResource.getProperty(bex.getCode()));
|
|
|
- } else if (ex instanceof IllegalArgumentException) {
|
|
|
- Object message = errorResource.getProperty(ex.getMessage());
|
|
|
- if (message == null) {
|
|
|
- message = errorResource.getProperty(ex.getClass().getName());
|
|
|
- }
|
|
|
- error = new Error((String) message);
|
|
|
- } else {
|
|
|
- error = new Error((String) errorResource.getProperty(ex.getClass().getName()));
|
|
|
- LoggerUtils.fmtError(getClass(), ex, "系统异常。");
|
|
|
- }
|
|
|
- if (StringUtils.isEmpty(error.getMessage())) {
|
|
|
- error.setMessage("系统异常,请联系管理员。");
|
|
|
- }
|
|
|
- return new BusinessException(error, ex.getCause());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @param errorResource
|
|
|
- * the errorResource to set
|
|
|
- */
|
|
|
- public void setErrorResource(ResourcePropertySource errorResource) {
|
|
|
- this.errorResource = errorResource;
|
|
|
- }
|
|
|
-}
|
|
|
+package com.goafanti.common.error;
|
|
|
+
|
|
|
+import java.text.MessageFormat;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.core.io.support.ResourcePropertySource;
|
|
|
+import org.springframework.web.servlet.ModelAndView;
|
|
|
+import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
|
|
|
+
|
|
|
+import com.goafanti.common.bo.Error;
|
|
|
+import com.goafanti.common.bo.Result;
|
|
|
+import com.goafanti.common.utils.LoggerUtils;
|
|
|
+import com.goafanti.common.utils.StringUtils;
|
|
|
+import com.goafanti.core.shiro.filter.ShiroFilterUtils;
|
|
|
+
|
|
|
+public class SystemExceptionResolver extends SimpleMappingExceptionResolver {
|
|
|
+ @Resource(name = "errorResource")
|
|
|
+ private ResourcePropertySource errorResource;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ShiroFilterUtils shiroFilterUtils;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
|
|
|
+ Exception ex) {
|
|
|
+ // Expose ModelAndView for chosen error view.
|
|
|
+ String viewName = determineViewName(ex, request);
|
|
|
+
|
|
|
+ ex = handleErrorMessage(ex);
|
|
|
+
|
|
|
+ if (viewName != null) {
|
|
|
+ if (!shiroFilterUtils.isAjax(request)) {// JSP
|
|
|
+ // Apply HTTP status code for error views, if specified.
|
|
|
+ // Only apply it if we're processing a top-level request.
|
|
|
+ Integer statusCode = determineStatusCode(request, viewName);
|
|
|
+ if (statusCode != null) {
|
|
|
+ applyStatusCodeIfPossible(request, response, statusCode);
|
|
|
+ }
|
|
|
+ return getModelAndView(viewName, ex, request);
|
|
|
+ } else {// JSON
|
|
|
+ Result res = new Result();
|
|
|
+ res.getError().add(new Error(viewName,ex.getMessage()));
|
|
|
+ shiroFilterUtils.out(response, res);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Exception handleErrorMessage(Exception ex) {
|
|
|
+ Error error = null;
|
|
|
+ if (ex instanceof BusinessException) {
|
|
|
+ BusinessException bex = (BusinessException) ex;
|
|
|
+ String message = (String)errorResource.getProperty(bex.getCode());
|
|
|
+ if(StringUtils.isEmpty(message)){
|
|
|
+ error = new Error(bex.getCode(),bex.getMessage());
|
|
|
+ }else{
|
|
|
+ error = new Error(bex.getCode(), MessageFormat.format(message, bex.getField()));
|
|
|
+ }
|
|
|
+ } else if (ex instanceof IllegalArgumentException) {
|
|
|
+ Object message = errorResource.getProperty(ex.getMessage());
|
|
|
+ if (message == null) {
|
|
|
+ message = errorResource.getProperty(ex.getClass().getName());
|
|
|
+ }
|
|
|
+ error = new Error((String) message);
|
|
|
+ } else {
|
|
|
+ error = new Error((String) errorResource.getProperty(ex.getClass().getName()));
|
|
|
+ LoggerUtils.fmtError(getClass(), ex, "系统异常。");
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(error.getMessage())) {
|
|
|
+ error.setMessage("系统异常,请联系管理员。");
|
|
|
+ }
|
|
|
+ return new BusinessException(error, ex.getCause());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param errorResource
|
|
|
+ * the errorResource to set
|
|
|
+ */
|
|
|
+ public void setErrorResource(ResourcePropertySource errorResource) {
|
|
|
+ this.errorResource = errorResource;
|
|
|
+ }
|
|
|
+}
|