Skip to content

OptionalAwareDecoder in feign core? #294

Description

@uschi2000

Hej again,
How are the chances of getting a "feign-guava-optional" module (or similar) into feign that knows about 404-decoding Guava Optional? Such a module would have a Guava dependency and only make sense where decode404 is enabled.
R.

/**
 * Decorates a Feign {@link Decoder} such that it returns {@link Optional#absent} when observing an HTTP 404 error code
 * for a method with {@link Type} {@link Optional}. Propagates the exception returned by the given {@link ErrorDecoder}
 * in case the response is 404 for a non-Optional method.
 */
public final class OptionalAwareDecoder implements Decoder {

    private final Decoder delegate;
    private final ErrorDecoder errorDecoder;

    public OptionalAwareDecoder(Decoder delegate, ErrorDecoder errorDecoder) {
        this.delegate = delegate;
        this.errorDecoder = errorDecoder;
    }

    @Override
    public Object decode(Response response, Type type) throws IOException, FeignException {
        if (response.status() == 404) {
            if (Types.getRawType(type).equals(Optional.class)) {
                return Optional.absent();
            } else {
                throw Throwables.propagate(errorDecoder.decode(null, response));
            }
        } else {
            return delegate.decode(response, type);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions