View Javadoc
1   package cn.home1.oss.lib.errorhandle.api;
2   
3   import static com.google.common.base.Preconditions.checkNotNull;
4   
5   import lombok.Getter;
6   import lombok.ToString;
7   
8   import org.springframework.context.HierarchicalMessageSource;
9   import org.springframework.context.MessageSource;
10  import org.springframework.web.context.request.RequestAttributes;
11  
12  import java.io.Serializable;
13  import java.util.Comparator;
14  import java.util.Map;
15  import java.util.Optional;
16  
17  /**
18   * ExceptionTranslator.
19   *
20   * <p>
21   * Created by zhanghaolun on 16/8/9.
22   * </p>
23   */
24  public interface ExceptionTranslator {
25  
26    @ToString
27    final class Location {
28  
29      /**
30       * Order first.
31       */
32      public static final Comparator<Location> ORDER_FIRST_COMPARATOR = (o1, o2) -> {
33        final int result;
34  
35        if (o1 != null && o2 != null) {
36          final int sourceOrderResult = o1.sourceOrder.compareTo(o2.sourceOrder);
37          final int levelResult = o1.level.compareTo(o2.level);
38          result = sourceOrderResult != 0 ? sourceOrderResult : levelResult;
39        } else if (o1 == null) {
40          result = 1;
41        } else {
42          result = -1;
43        }
44  
45        return result;
46      };
47  
48      /**
49       * Hierarchy first.
50       */
51      public static final Comparator<Location> HIERARCHY_FIRST_COMPARATOR = (o1, o2) -> {
52        final int result;
53  
54        if (o1 != null && o2 != null) {
55          final int sourceOrderResult = o1.sourceOrder.compareTo(o2.sourceOrder);
56          final int levelResult = o1.level.compareTo(o2.level);
57          result = levelResult != 0 ? levelResult : sourceOrderResult;
58        } else if (o1 == null) {
59          result = 1;
60        } else {
61          result = -1;
62        }
63  
64        return result;
65      };
66  
67      @Getter
68      private final MessageSource source;
69      @Getter
70      private final Integer sourceOrder;
71      @Getter
72      private final String key;
73      @Getter
74      private final Integer level;
75  
76      public Location( //
77        final MessageSource source, //
78        final Integer sourceOrder, //
79        final String key, //
80        final Integer level //
81      ) {
82        this.source = checkNotNull(source);
83        this.sourceOrder = checkNotNull(sourceOrder);
84        this.key = checkNotNull(key);
85        this.level = checkNotNull(level);
86      }
87    }
88  
89    @Deprecated
90    static HierarchicalMessageSource rootMessageSource(final MessageSource messageSource) {
91      final HierarchicalMessageSource result;
92      if (messageSource instanceof HierarchicalMessageSource) {
93        final MessageSource parent = ((HierarchicalMessageSource) messageSource).getParentMessageSource();
94        result = parent != null ? //
95          rootMessageSource(parent) //
96          : (HierarchicalMessageSource) messageSource; //
97      } else {
98        result = null;
99      }
100     return result;
101   }
102 
103   /**
104    * locate info.
105    *
106    * @param throwable The exception to handle and get data from.
107    * @return info location.
108    */
109   Optional<Location> find(Throwable throwable);
110 
111   /**
112    * Translate exception and generates localized message.
113    *
114    * @param template         template
115    * @param request          The current request attributes.
116    * @param throwable        throwable
117    * @param contextVariables The variables in context
118    * @return A TranslateResult.
119    */
120   Optional<String> localizedMessage( //
121     String template, //
122     RequestAttributes request, //
123     Throwable throwable, //
124     Map<String, Serializable> contextVariables //
125   );
126 
127   Optional<Integer> status(Location location);
128 
129   Optional<String> template(Location location);
130 }