SystemExceptionResolver.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package com.goafanti.common.error;
  2. import com.goafanti.common.bo.Error;
  3. import com.goafanti.common.bo.Result;
  4. import com.goafanti.common.utils.LoggerUtils;
  5. import com.goafanti.common.utils.StringUtils;
  6. import com.goafanti.core.shiro.filter.ShiroFilterUtils;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.beans.factory.annotation.Value;
  9. import org.springframework.core.io.support.ResourcePropertySource;
  10. import org.springframework.web.multipart.MaxUploadSizeExceededException;
  11. import org.springframework.web.servlet.ModelAndView;
  12. import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
  13. import javax.annotation.Resource;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import java.text.MessageFormat;
  17. public class SystemExceptionResolver extends SimpleMappingExceptionResolver{
  18. @Resource(name = "errorResource")
  19. private ResourcePropertySource errorResource;
  20. @Value(value = "${app.name}")
  21. private String appName;
  22. @Autowired
  23. private ShiroFilterUtils shiroFilterUtils;
  24. @Override
  25. protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
  26. Exception ex) {
  27. /*if(request.getHeader("User-Agent") != null && request.getHeader("User-Agent").indexOf(appName) > -1){
  28. setExceptionMappings(new Properties());
  29. return null;
  30. }
  31. */
  32. // Expose ModelAndView for chosen error view.
  33. String viewName = determineViewName(ex, request);
  34. ex = handleErrorMessage(ex);
  35. if (viewName != null) {
  36. if (!shiroFilterUtils.isAjax(request)) {// JSP
  37. // Apply HTTP status code for error views, if specified.
  38. // Only apply it if we're processing a top-level request.
  39. Integer statusCode = determineStatusCode(request, viewName);
  40. if (statusCode != null) {
  41. applyStatusCodeIfPossible(request, response, statusCode);
  42. }
  43. return getModelAndView(viewName, ex, request);
  44. } else {// JSON
  45. Result res = new Result();
  46. res.getError().add(new Error(viewName,ex.getMessage()));
  47. shiroFilterUtils.out(response, res);
  48. return null;
  49. }
  50. } else {
  51. return null;
  52. }
  53. }
  54. private Exception handleErrorMessage(Exception ex) {
  55. Error error = null;
  56. if (ex instanceof BusinessException) {
  57. BusinessException bex = (BusinessException) ex;
  58. String message = (String)errorResource.getProperty(bex.getCode());
  59. if(StringUtils.isEmpty(message)){
  60. error = new Error(bex.getCode(),bex.getMessage());
  61. }else{
  62. error = new Error(bex.getCode(), MessageFormat.format(message, bex.getField()));
  63. }
  64. } else if (ex instanceof IllegalArgumentException) {
  65. Object message = errorResource.getProperty(ex.getMessage());
  66. if (message == null) {
  67. message = errorResource.getProperty(ex.getClass().getName());
  68. }
  69. error = new Error((String) message);
  70. }else if (ex instanceof ClassCastException) {
  71. Object message = errorResource.getProperty(ex.getMessage());
  72. if (message == null) {
  73. message = errorResource.getProperty(ex.getClass().getName());
  74. }
  75. error = new Error((String) message);
  76. }else if (ex instanceof MaxUploadSizeExceededException) {
  77. Object message = errorResource.getProperty(ex.getMessage());
  78. if (message == null) {
  79. message = errorResource.getProperty(ex.getClass().getName());
  80. }
  81. error = new Error((String) message);
  82. }else {
  83. error = new Error((String) errorResource.getProperty(ex.getClass().getName()));
  84. LoggerUtils.fmtError(getClass(), ex, "数据异常。",ex.getClass().getName());
  85. }
  86. if (StringUtils.isEmpty(error.getMessage())) {
  87. error.setMessage("数据异常,请稍后再试,如再失败请联系管理员。");
  88. }
  89. return new BusinessException(error, ex.getCause());
  90. }
  91. /**
  92. * @param errorResource
  93. * the errorResource to set
  94. */
  95. public void setErrorResource(ResourcePropertySource errorResource) {
  96. this.errorResource = errorResource;
  97. }
  98. }