This repository was archived by the owner on Apr 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathHandledException.java
More file actions
173 lines (153 loc) · 6.43 KB
/
HandledException.java
File metadata and controls
173 lines (153 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package me.alidg.errors;
import org.springframework.http.HttpStatus;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import java.util.*;
import java.util.stream.Collectors;
import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNull;
/**
* Encapsulates details about a handled exception, including:
* <ul>
* <li>The mapped business level error codes and their arguments that can be used for message translation</li>
* <li>The corresponding HTTP status code</li>
* </ul>
*
* @author Ali Dehghani
* @see WebErrorHandler
*/
public class HandledException {
/**
* Collection of error codes corresponding to the handled exception. Usually this collection
* contains only one error code but not always, say for validation errors.
*/
private final List<ErrorWithArguments> errors;
/**
* Corresponding status code for the handled exception.
*/
private final HttpStatus statusCode;
/**
* Initialize a handled exception with a set of error codes, a HTTP status code and an
* optional collection of arguments.
*
* @param errors The corresponding error codes for the handled exception.
* @param statusCode The corresponding status code for the handled exception.
* @throws NullPointerException When one of the required parameters is null.
* @throws IllegalArgumentException At least one error code should be provided.
*/
public HandledException(@NonNull List<ErrorWithArguments> errors,
@NonNull HttpStatus statusCode) {
enforcePreconditions(errors, statusCode);
this.errors = errors;
this.statusCode = statusCode;
}
/**
* Initialize a handled exception with an error code, a HTTP status code and an
* optional collection of arguments.
*
* @param error The corresponding error code for the handled exception.
* @param statusCode The corresponding status code for the handled exception.
* @throws NullPointerException When one of the required parameters is null.
* @throws IllegalArgumentException At least one error code should be provided.
*/
public HandledException(@NonNull ErrorWithArguments error,
@NonNull HttpStatus statusCode) {
this(singletonList(error), statusCode);
}
/**
* Initialize a handled exception with a set of error codes, a HTTP status code and an
* optional collection of arguments.
*
* @param errorCodes The corresponding error codes for the handled exception.
* @param statusCode The corresponding status code for the handled exception.
* @param arguments Arguments to be exposed from the handled exception to the outside world.
* @throws NullPointerException When one of the required parameters is null.
* @throws IllegalArgumentException At least one error code should be provided.
* @deprecated This constructor should no longer be used as it does not allow to support the same error code
* multiple times
*/
@Deprecated
public HandledException(@NonNull Set<String> errorCodes,
@NonNull HttpStatus statusCode,
@Nullable Map<String, List<Argument>> arguments) {
this(convertToErrors(errorCodes, arguments), statusCode);
}
/**
* Initialize a handled exception with an error code, a HTTP status code and an
* optional collection of arguments.
*
* @param errorCode The corresponding error code for the handled exception.
* @param statusCode The corresponding status code for the handled exception.
* @param arguments Arguments to be exposed from the handled exception to the outside world.
* @throws NullPointerException When one of the required parameters is null.
* @throws IllegalArgumentException At least one error code should be provided.
* @deprecated This constructor should no longer be used as it does not allow to support the same error code
* multiple times
*/
@Deprecated
public HandledException(@NonNull String errorCode,
@NonNull HttpStatus statusCode,
@Nullable Map<String, List<Argument>> arguments) {
this(singleton(errorCode), statusCode, arguments);
}
/**
*
* @return Collection of errors
*/
@NonNull
public List<ErrorWithArguments> getErrors() {
return errors;
}
/**
* @return Collection of mapped error codes.
* @deprecated This method should no longer be used as it does not allow to support the same error code
* multiple times
*/
@NonNull
@Deprecated
public Set<String> getErrorCodes() {
return errors.stream()
.map(ErrorWithArguments::getErrorCode)
.collect(Collectors.toSet());
}
/**
*
* @return Collection of to-be-exposed arguments.
* @deprecated This method should no longer be used as it does not allow to support the same error code
* multiple times
*/
@NonNull
@Deprecated
public Map<String, List<Argument>> getArguments() {
return errors.stream()
.collect(Collectors.toMap(ErrorWithArguments::getErrorCode,
ErrorWithArguments::getArguments));
}
/**
* @return The mapped status code.
* @see #statusCode
*/
@NonNull
public HttpStatus getStatusCode() {
return statusCode;
}
private void enforcePreconditions(List<ErrorWithArguments> errorCodes, HttpStatus statusCode) {
requireNonNull(errorCodes, "Error codes is required");
requireNonNull(statusCode, "Status code is required");
if (errorCodes.isEmpty()) {
throw new IllegalArgumentException("At least one error code should be provided");
}
if (errorCodes.size() == 1 && errorCodes.contains(null)) {
throw new NullPointerException("The single error code can't be null");
}
}
@NonNull
private static List<ErrorWithArguments> convertToErrors(Set<String> errorCodes, Map<String, List<Argument>> arguments) {
List<ErrorWithArguments> result = new ArrayList<>();
for (String errorCode : errorCodes) {
result.add(new ErrorWithArguments(errorCode, arguments.get(errorCode)));
}
return result;
}
}