SystemExceptionResolver.java 3.0 KB

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