Skip to content

Commit 6523d56

Browse files
author
adriancole
committed
Decoders can throw checked exceptions, but needn't declare Throwable
1 parent 80ca28f commit 6523d56

9 files changed

Lines changed: 21 additions & 18 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### Version 3.0
22
* decoupled ErrorDecoder from fallback handling
3+
* Decoders can throw checked exceptions, but needn't declare Throwable
34

45
### Version 2.0.0
56
* removes guava and jax-rs dependencies

feign-core/src/main/java/feign/Client.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ HttpURLConnection convertAndSend(Request request, Options options) throws IOExce
107107
return connection;
108108
}
109109

110-
private static final int BUF_SIZE = 0x800; // 2K chars (4K bytes)
111-
112110
Response convertResponse(HttpURLConnection connection) throws IOException {
113111
int status = connection.getResponseCode();
114112
String reason = connection.getResponseMessage();

feign-core/src/main/java/feign/FeignException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import java.io.IOException;
1919

20-
import feign.codec.ToStringDecoder;
20+
import feign.codec.StringDecoder;
2121

2222
import static java.lang.String.format;
2323

@@ -29,7 +29,7 @@ static FeignException errorReading(Request request, Response response, IOExcepti
2929
return new FeignException(format("%s %s %s", cause.getMessage(), request.method(), request.url(), 0), cause);
3030
}
3131

32-
private static final ToStringDecoder toString = new ToStringDecoder();
32+
private static final StringDecoder toString = new StringDecoder();
3333

3434
public static FeignException errorStatus(String methodKey, Response response) {
3535
String message = format("status %s reading %s", response.status(), methodKey);

feign-core/src/main/java/feign/ReflectiveFeign.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import feign.codec.Decoder;
3434
import feign.codec.ErrorDecoder;
3535
import feign.codec.FormEncoder;
36-
import feign.codec.ToStringDecoder;
36+
import feign.codec.StringDecoder;
3737

3838
import static feign.Util.checkArgument;
3939
import static feign.Util.checkNotNull;
@@ -52,7 +52,7 @@ public class ReflectiveFeign extends Feign {
5252
* creates an api binding to the {@code target}. As this invokes reflection,
5353
* care should be taken to cache the result.
5454
*/
55-
@Override public <T> T newInstance(Target<T> target) {
55+
@SuppressWarnings("unchecked") @Override public <T> T newInstance(Target<T> target) {
5656
Map<String, MethodHandler> nameToHandler = targetToHandlersByName.apply(target);
5757
Map<Method, MethodHandler> methodToHandler = new LinkedHashMap<Method, MethodHandler>();
5858
for (Method method : target.type().getDeclaredMethods()) {
@@ -143,7 +143,7 @@ public Map<String, MethodHandler> apply(Target key) {
143143
Decoder decoder = forMethodOrClass(decoders, md.configKey());
144144
if (decoder == null
145145
&& (md.returnType() == void.class || md.returnType() == Response.class)) {
146-
decoder = new ToStringDecoder();
146+
decoder = new StringDecoder();
147147
}
148148
if (decoder == null) {
149149
throw noConfig(md.configKey(), Decoder.class);

feign-core/src/main/java/feign/Util.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public static String emptyToNull(String string) {
9595
/**
9696
* Adapted from {@code com.google.common.base.Strings#emptyToNull}.
9797
*/
98+
@SuppressWarnings("unchecked")
9899
public static <T> T[] toArray(Iterable<? extends T> iterable, Class<T> type) {
99100
Collection<T> collection;
100101
if (iterable instanceof Collection) {

feign-core/src/main/java/feign/codec/Decoder.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ public abstract class Decoder {
6666
* @param type Target object type.
6767
* @return instance of {@code type}
6868
* @throws IOException if there was a network error reading the response.
69+
* @throws Exception if the decoder threw a checked exception.
6970
*/
70-
public Object decode(String methodKey, Response response, Type type) throws Throwable {
71+
public Object decode(String methodKey, Response response, Type type) throws Exception {
7172
Response.Body body = response.body();
7273
if (body == null)
7374
return null;
@@ -89,7 +90,8 @@ public Object decode(String methodKey, Response response, Type type) throws Thro
8990
* manages resources.
9091
* @param type Target object type.
9192
* @return instance of {@code type}
92-
* @throws Throwable will be propagated safely to the caller.
93+
* @throws IOException will be propagated safely to the caller.
94+
* @throws Exception if the decoder threw a checked exception.
9395
*/
94-
public abstract Object decode(String methodKey, Reader reader, Type type) throws Throwable;
96+
public abstract Object decode(String methodKey, Reader reader, Type type) throws Exception;
9597
}

feign-core/src/main/java/feign/codec/Decoders.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package feign.codec;
1717

18+
import java.io.IOException;
1819
import java.io.Reader;
1920
import java.lang.reflect.Type;
2021
import java.util.ArrayList;
@@ -66,7 +67,7 @@ public static <T> Decoder transformFirstGroup(String pattern, final ApplyFirstGr
6667
checkNotNull(applyFirstGroup, "applyFirstGroup");
6768
return new Decoder() {
6869
@Override
69-
public Object decode(String methodKey, Reader reader, Type type) throws Throwable {
70+
public Object decode(String methodKey, Reader reader, Type type) throws IOException {
7071
Matcher matcher = patternForMatcher.matcher(Decoders.toString(reader));
7172
if (matcher.find()) {
7273
return applyFirstGroup.apply(matcher.group(1));
@@ -112,7 +113,7 @@ public static <T> Decoder transformEachFirstGroup(String pattern, final ApplyFir
112113
checkNotNull(applyFirstGroup, "applyFirstGroup");
113114
return new Decoder() {
114115
@Override
115-
public List<T> decode(String methodKey, Reader reader, Type type) throws Throwable {
116+
public List<T> decode(String methodKey, Reader reader, Type type) throws IOException {
116117
Matcher matcher = patternForMatcher.matcher(Decoders.toString(reader));
117118
List<T> result = new ArrayList<T>();
118119
while (matcher.find()) {
@@ -143,11 +144,11 @@ public static Decoder eachFirstGroup(String pattern) {
143144
return transformEachFirstGroup(pattern, IDENTITY);
144145
}
145146

146-
private static String toString(Reader reader) throws Throwable {
147+
private static String toString(Reader reader) throws IOException {
147148
return TO_STRING.decode(null, reader, null).toString();
148149
}
149150

150-
private static final Decoder TO_STRING = new ToStringDecoder();
151+
private static final StringDecoder TO_STRING = new StringDecoder();
151152

152153
private static final ApplyFirstGroup<String> IDENTITY = new ApplyFirstGroup<String>() {
153154
@Override public String apply(String firstGroup) {

feign-core/src/main/java/feign/codec/ToStringDecoder.java renamed to feign-core/src/main/java/feign/codec/StringDecoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
/**
2828
* Adapted from {@code com.google.common.io.CharStreams.toString()}.
2929
*/
30-
public class ToStringDecoder extends Decoder {
30+
public class StringDecoder extends Decoder {
3131
private static final int BUF_SIZE = 0x800; // 2K chars (4K bytes)
3232

3333
// overridden to throw only IOException

feign-core/src/test/java/feign/FeignTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import dagger.Provides;
3737
import feign.codec.Decoder;
3838
import feign.codec.ErrorDecoder;
39-
import feign.codec.ToStringDecoder;
39+
import feign.codec.StringDecoder;
4040

4141
import static org.testng.Assert.assertEquals;
4242

@@ -58,7 +58,7 @@ static class Module {
5858
// until dagger supports real map binding, we need to recreate the
5959
// entire map, as opposed to overriding a single entry.
6060
@Provides @Singleton Map<String, Decoder> decoders() {
61-
return ImmutableMap.<String, Decoder>of("TestInterface", new ToStringDecoder());
61+
return ImmutableMap.<String, Decoder>of("TestInterface", new StringDecoder());
6262
}
6363
}
6464
}
@@ -146,7 +146,7 @@ public void doesntRetryAfterResponseIsSent() throws IOException, InterruptedExce
146146
return ImmutableMap.<String, Decoder>of("TestInterface", new Decoder() {
147147

148148
@Override
149-
public Object decode(String methodKey, Reader reader, Type type) throws Throwable {
149+
public Object decode(String methodKey, Reader reader, Type type) throws IOException {
150150
throw new IOException("error reading response");
151151
}
152152

0 commit comments

Comments
 (0)