Repository navigation
Expand file tree
/
Copy pathErrorDecoder.java
More file actions
120 lines (110 loc) · 4.59 KB
/
Copy pathErrorDecoder.java
File metadata and controls
120 lines (110 loc) · 4.59 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
/*
* Copyright © 2012 The Feign Authors (feign@commonhaus.dev)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package feign.codec;
import static feign.Util.checkNotNull;
import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME;
import static java.util.concurrent.TimeUnit.SECONDS;
import feign.Response;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
/**
* Allows you to massage an exception into a application-specific one. Converting out to a throttle
* exception are examples of this in use.
*
* <p>Ex:
*
* <pre>
* class IllegalArgumentExceptionOn404Decoder implements ErrorDecoder {
*
* @Override
* public Exception decode(String methodKey, Response response) {
* if (response.status() == 400)
* throw new IllegalArgumentException("bad zone name");
* return new ErrorDecoder.Default().decode(methodKey, response);
* }
*
* }
* </pre>
*
* <p><b>Error handling</b>
*
* <p>Responses where {@link Response#status()} is not in the 2xx range are classified as errors,
* addressed by the {@link ErrorDecoder}. That said, certain RPC apis return errors defined in the
* {@link Response#body()} even on a 200 status. For example, in the DynECT api, a job still running
* condition is returned with a 200 status, encoded in json. When scenarios like this occur, you
* should raise an application-specific exception (which may be {@link feign.RetryableException
* retryable}).
*
* <p><b>Not Found Semantics</b>
*
* <p>It is commonly the case that 404 (Not Found) status has semantic value in HTTP apis. While the
* default behavior is to raise exeception, users can alternatively enable 404 processing via {@link
* feign.Feign.Builder#dismiss404()}.
*/
public interface ErrorDecoder {
/**
* Implement this method in order to decode an HTTP {@link Response} when {@link
* Response#status()} is not in the 2xx range. Please raise application-specific exceptions where
* possible. If your exception is retryable, wrap or subclass {@link RetryableException}
*
* @param methodKey {@link feign.Feign#configKey} of the java method that invoked the request. ex.
* {@code IAM#getUser()}
* @param response HTTP response where {@link Response#status() status} is greater than or equal
* to {@code 300}.
* @return Exception IOException, if there was a network error reading the response or an
* application-specific exception decoded by the implementation. If the throwable is
* retryable, it should be wrapped, or a subtype of {@link RetryableException}
*/
public Exception decode(String methodKey, Response response);
/**
* Decodes a {@link feign.Util#RETRY_AFTER} header into an epoch millisecond, if possible.<br>
* See <a href="https://tools.ietf.org/html/rfc2616#section-14.37">Retry-After format</a>
*/
public static class RetryAfterDecoder {
private final DateTimeFormatter dateTimeFormatter;
public RetryAfterDecoder() {
this(RFC_1123_DATE_TIME);
}
public RetryAfterDecoder(DateTimeFormatter dateTimeFormatter) {
this.dateTimeFormatter = checkNotNull(dateTimeFormatter, "dateTimeFormatter");
}
protected long currentTimeMillis() {
return System.currentTimeMillis();
}
/**
* returns an epoch millisecond that corresponds to the first time a request can be retried.
*
* @param retryAfter String in <a href="https://tools.ietf.org/html/rfc2616#section-14.37"
* >Retry-After format</a>
*/
public Long apply(String retryAfter) {
if (retryAfter == null) {
return null;
}
try {
if (retryAfter.matches("^[0-9]+\\.?0*$")) {
retryAfter = retryAfter.replaceAll("\\.0*$", "");
long deltaMillis = SECONDS.toMillis(Long.parseLong(retryAfter));
return currentTimeMillis() + deltaMillis;
}
return ZonedDateTime.parse(retryAfter, dateTimeFormatter).toInstant().toEpochMilli();
} catch (NumberFormatException | NullPointerException | DateTimeParseException ignored) {
return null;
}
}
}
}