SystemExceptionResolver.java 3.3 KB

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