/*
* 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 java.lang.reflect.Type;
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 feign.FeignException.errorStatus;
import static feign.Util.RETRY_AFTER;
import static feign.Util.checkNotNull;
import static feign.Util.firstOrNull;
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.
*
* Ex.
*
*
* class IllegalArgumentExceptionOn404Decoder extends ErrorDecoder {
*
* @Override
* public Object decode(String methodKey, Response response, Type<?> type) throws Throwable {
* if (response.status() == 404)
* throw new IllegalArgumentException("zone not found");
* return ErrorDecoder.DEFAULT.decode(request, response, type);
* }
*
* }
*
*/
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} is greater than or equal to {@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, Type type) throws Throwable;
public static final ErrorDecoder DEFAULT = new ErrorDecoder() {
private final RetryAfterDecoder retryAfterDecoder = new RetryAfterDecoder();
@Override
public Object decode(String methodKey, Response response, Type type) throws Throwable {
FeignException exception = errorStatus(methodKey, response);
Date retryAfter = retryAfterDecoder.apply(firstOrNull(response.headers(), RETRY_AFTER));
if (retryAfter != null)
throw new RetryableException(exception.getMessage(), exception, retryAfter);
throw exception;
}
};
/**
* Decodes a {@link feign.Util#RETRY_AFTER} header into an absolute date,
* if possible.
*