1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.Collections;
23 import java.util.Map;
24 import javax.validation.constraints.NotNull;
25 import lombok.Getter;
26 import org.bremersee.exception.ErrorCodeAware;
27 import org.bremersee.exception.HttpResponseHeadersAware;
28 import org.bremersee.exception.HttpStatusAware;
29 import org.bremersee.exception.RestApiExceptionAware;
30 import org.bremersee.exception.RestApiExceptionUtils;
31 import org.bremersee.exception.model.RestApiException;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.lang.Nullable;
34 import org.springframework.util.StringUtils;
35
36
37
38
39
40
41
42
43 public class FeignClientException extends FeignException implements HttpStatusAware,
44 HttpResponseHeadersAware, RestApiExceptionAware, ErrorCodeAware {
45
46 @Getter
47 private final Request request;
48
49 @NotNull
50 private final Map<String, ? extends Collection<String>> headers;
51
52 @Getter
53 private final RestApiException restApiException;
54
55
56
57
58
59
60
61
62
63
64 @SuppressWarnings("WeakerAccess")
65 public FeignClientException(
66 @Nullable final Request request,
67 @Nullable final Map<String, ? extends Collection<String>> headers,
68 final int status,
69 @Nullable final String message,
70 @Nullable final RestApiException restApiException) {
71
72 super(
73 resolveHttpStatusCode(status),
74 StringUtils.hasText(message)
75 ? message
76 : RestApiExceptionUtils.NO_MESSAGE_VALUE,
77 request != null
78 ? request.body()
79 : null);
80 this.request = request;
81 this.headers = headers != null ? headers : Collections.emptyMap();
82 this.restApiException = restApiException;
83 }
84
85 @Override
86 public Map<String, ? extends Collection<String>> getMultiValueHeaders() {
87 return headers;
88 }
89
90 @Override
91 public String getErrorCode() {
92 return restApiException != null ? restApiException.getErrorCode() : null;
93 }
94
95 private static int resolveHttpStatusCode(final int httpStatusCode) {
96 final HttpStatus httpStatus = HttpStatus.resolve(httpStatusCode);
97 return httpStatus != null ? httpStatus.value() : HttpStatus.INTERNAL_SERVER_ERROR.value();
98 }
99
100 }