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
19
20
21
22
23
24 public interface ExceptionTranslator {
25
26 @ToString
27 final class Location {
28
29
30
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
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
105
106
107
108
109 Optional<Location> find(Throwable throwable);
110
111
112
113
114
115
116
117
118
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 }