1 /*
2 * Copyright 2019-2022 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.bremersee.exception.feign;
18
19 import feign.FeignException;
20 import feign.Request;
21 import java.util.Collection;
22 import java.util.Map;
23 import lombok.Getter;
24 import org.bremersee.exception.HttpStatusAware;
25 import org.bremersee.exception.RestApiExceptionAware;
26 import org.bremersee.exception.model.RestApiException;
27 import org.springframework.http.HttpStatus;
28
29 /**
30 * Feign exception that stores the error payload as a {@link RestApiException}. If the error payload
31 * cannot be parsed as {@link RestApiException}, the whole body of the error payload will be stored
32 * in the message field of the {@link RestApiException}.
33 *
34 * @author Christian Bremer
35 */
36 @Getter
37 public class FeignClientException extends FeignException
38 implements HttpStatusAware, RestApiExceptionAware {
39
40 /**
41 * The rest api exception.
42 */
43 private final RestApiException restApiException;
44
45 /**
46 * Instantiates a new feign client exception.
47 *
48 * @param status the response status code
49 * @param message the message of this {@link FeignClientException}
50 * @param request the original request
51 * @param responseHeaders the response headers
52 * @param responseBody the response body
53 * @param restApiException the rest api exception
54 */
55 public FeignClientException(
56 int status,
57 String message,
58 Request request,
59 Map<String, Collection<String>> responseHeaders,
60 byte[] responseBody,
61 RestApiException restApiException) {
62
63 super(
64 resolveHttpStatusCode(status),
65 message,
66 request,
67 responseBody,
68 responseHeaders);
69 this.restApiException = restApiException;
70 }
71
72 private static int resolveHttpStatusCode(int httpStatusCode) {
73 HttpStatus httpStatus = HttpStatus.resolve(httpStatusCode);
74 return httpStatus != null ? httpStatus.value() : HttpStatus.INTERNAL_SERVER_ERROR.value();
75 }
76
77 }