ExtendedErrorAttributes.java

  1. package cn.home1.oss.lib.errorhandle.internal;

  2. import static cn.home1.oss.lib.errorhandle.api.ExceptionResolver.COMPOSITE_EXCEPTION_RESOLVER;

  3. import com.google.common.collect.ImmutableMap;

  4. import cn.home1.oss.lib.errorhandle.api.ExceptionResolver;
  5. import cn.home1.oss.lib.errorhandle.api.ResolvedError;
  6. import cn.home1.oss.lib.errorhandle.api.ResolvedErrorException;

  7. import lombok.Setter;

  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.beans.factory.annotation.Qualifier;
  10. import org.springframework.boot.autoconfigure.web.ErrorAttributes;
  11. import org.springframework.core.Ordered;
  12. import org.springframework.core.annotation.Order;
  13. import org.springframework.web.context.request.RequestAttributes;
  14. import org.springframework.web.servlet.HandlerExceptionResolver;
  15. import org.springframework.web.servlet.ModelAndView;

  16. import java.util.Map;

  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;

  19. /**
  20.  * {@link org.springframework.boot.autoconfigure.web.DefaultErrorAttributes}
  21.  *
  22.  * <p>
  23.  * Created by zhanghaolun on 16/8/9.
  24.  * </p>
  25.  */
  26. @Order(Ordered.HIGHEST_PRECEDENCE)
  27. public class ExtendedErrorAttributes //
  28.   implements ErrorAttributes, HandlerExceptionResolver, Ordered {

  29.   private static final String ERROR_ATTRIBUTE = ExtendedErrorAttributes.class.getName() + ".ERROR";

  30.   @Qualifier(COMPOSITE_EXCEPTION_RESOLVER)
  31.   @Autowired
  32.   @Setter
  33.   private ExceptionResolver<Throwable> resolver;

  34.   @Override
  35.   public Map<String, Object> getErrorAttributes( //
  36.     final RequestAttributes requestAttributes, //
  37.     final boolean includeStackTrace //
  38.   ) {
  39.     final Throwable error = getError(requestAttributes);
  40.     final ResolvedError resolved = ResolvedErrorException.isResolvedError(error) ? //
  41.       ((ResolvedErrorException) error).getError() : //
  42.       this.resolver.resolve(requestAttributes, error);
  43.     return resolved != null ? resolved.toErrorAttributes() : ImmutableMap.of();
  44.   }

  45.   @Override
  46.   public int getOrder() {
  47.     return Ordered.HIGHEST_PRECEDENCE;
  48.   }

  49.   @Override
  50.   public ModelAndView resolveException( //
  51.     final HttpServletRequest request, //
  52.     final HttpServletResponse response, //
  53.     final Object handler, //
  54.     final Exception ex //
  55.   ) {
  56.     final ResolvedError resolved = this.resolver.resolve(request, ex);
  57.     this.storeErrorAttributes(request, resolved != null ? new ResolvedErrorException(resolved) : ex);
  58.     return null;
  59.   }

  60.   private void storeErrorAttributes(final HttpServletRequest request, final Exception ex) {
  61.     request.setAttribute(ERROR_ATTRIBUTE, ex);
  62.   }

  63.   @Override
  64.   public Throwable getError(final RequestAttributes requestAttributes) {
  65.     final Throwable found = getAttribute(requestAttributes, ERROR_ATTRIBUTE);
  66.     return found != null ? found : getAttribute(requestAttributes, "javax.servlet.error.exception");
  67.   }

  68.   @SuppressWarnings("unchecked")
  69.   private <T> T getAttribute(final RequestAttributes requestAttributes, final String name) {
  70.     return (T) requestAttributes.getAttribute(name, RequestAttributes.SCOPE_REQUEST);
  71.   }
  72. }