forked from OpenFeign/feign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorDecoder.java
More file actions
144 lines (131 loc) · 5.33 KB
/
ErrorDecoder.java
File metadata and controls
144 lines (131 loc) · 5.33 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
/*
* Copyright 2013 Netflix, Inc.
*
* 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 com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Ticker;
import com.google.common.reflect.TypeToken;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import feign.FeignException;
import feign.Response;
import feign.RetryableException;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.getFirst;
import static com.google.common.net.HttpHeaders.RETRY_AFTER;
import static feign.FeignException.errorStatus;
import static java.util.Locale.US;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
/**
* Allows you to massage an exception into a application-specific one, or
* fallback to a default value. Falling back to null on
* {@link Response#status() status 404}, or converting out to a throttle
* exception are examples of this in use.
* <p/>
* Ex.
* <p/>
* <pre>
* class IllegalArgumentExceptionOn404Decoder extends ErrorDecoder {
*
* @Override
* public Object decode(String methodKey, Response response, TypeToken<?> type) throws Throwable {
* if (response.status() == 404)
* throw new IllegalArgumentException("zone not found");
* return ErrorDecoder.DEFAULT.decode(request, response, type);
* }
*
* }
* </pre>
*/
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 or return fallback values 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} >=
* {@code 300}.
* @param type Target object type.
* @return instance of {@code type}
* @throws Throwable 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 Object decode(String methodKey, Response response, TypeToken<?> type) throws Throwable;
public static final ErrorDecoder DEFAULT = new ErrorDecoder() {
private final RetryAfterDecoder retryAfterDecoder = new RetryAfterDecoder();
@Override
public Object decode(String methodKey, Response response, TypeToken<?> type) throws Throwable {
FeignException exception = errorStatus(methodKey, response);
Optional<Date> retryAfter = retryAfterDecoder.apply(getFirst(response.headers().get(RETRY_AFTER), null));
if (retryAfter.isPresent())
throw new RetryableException(exception.getMessage(), exception, retryAfter.get());
throw exception;
}
};
/**
* Decodes a {@link com.google.common.net.HttpHeaders#RETRY_AFTER} header into an absolute date,
* if possible.
*
* @see <a
* href="https://tools.ietf.org/html/rfc2616#section-14.37">Retry-After
* format</a>
*/
static class RetryAfterDecoder implements Function<String, Optional<Date>> {
static final DateFormat RFC822_FORMAT = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", US);
private final Ticker currentTimeNanos;
private final DateFormat rfc822Format;
RetryAfterDecoder() {
this(Ticker.systemTicker(), RFC822_FORMAT);
}
RetryAfterDecoder(Ticker currentTimeNanos, DateFormat rfc822Format) {
this.currentTimeNanos = checkNotNull(currentTimeNanos, "currentTimeNanos");
this.rfc822Format = checkNotNull(rfc822Format, "rfc822Format");
}
/**
* returns a date 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>
*/
@Override
public Optional<Date> apply(String retryAfter) {
if (retryAfter == null)
return Optional.absent();
if (retryAfter.matches("^[0-9]+$")) {
long currentTimeMillis = NANOSECONDS.toMillis(currentTimeNanos.read());
long deltaMillis = SECONDS.toMillis(Long.parseLong(retryAfter));
return Optional.of(new Date(currentTimeMillis + deltaMillis));
}
synchronized (rfc822Format) {
try {
return Optional.of(rfc822Format.parse(retryAfter));
} catch (ParseException ignored) {
return Optional.absent();
}
}
}
}
}