|
18 | 18 | import io.dapr.config.Properties;
|
19 | 19 | import io.dapr.exceptions.DaprError;
|
20 | 20 | import io.dapr.exceptions.DaprException;
|
21 |
| -import io.dapr.exceptions.DaprHttpException; |
22 | 21 | import io.dapr.utils.Version;
|
23 | 22 | import okhttp3.Call;
|
24 | 23 | import okhttp3.Callback;
|
@@ -73,6 +72,12 @@ public class DaprHttp implements AutoCloseable {
|
73 | 72 | private static final Set<String> ALLOWED_CONTEXT_IN_HEADERS =
|
74 | 73 | Collections.unmodifiableSet(new HashSet<>(Arrays.asList("grpc-trace-bin", "traceparent", "tracestate")));
|
75 | 74 |
|
| 75 | + |
| 76 | + /** |
| 77 | + * Error response parser. |
| 78 | + */ |
| 79 | + private static DaprErrorResponseParser parser = new DefaultDaprErrorResponseParser(); |
| 80 | + |
76 | 81 | /**
|
77 | 82 | * HTTP Methods supported.
|
78 | 83 | */
|
@@ -163,10 +168,11 @@ public int getStatusCode() {
|
163 | 168 | * @param port Port for calling Dapr. (e.g. 3500)
|
164 | 169 | * @param httpClient RestClient used for all API calls in this new instance.
|
165 | 170 | */
|
166 |
| - DaprHttp(String hostname, int port, OkHttpClient httpClient) { |
| 171 | + DaprHttp(String hostname, int port, OkHttpClient httpClient, DaprErrorResponseParser parser) { |
167 | 172 | this.hostname = hostname;
|
168 | 173 | this.port = port;
|
169 | 174 | this.httpClient = httpClient;
|
| 175 | + this.parser = parser; |
170 | 176 | }
|
171 | 177 |
|
172 | 178 | /**
|
@@ -324,24 +330,6 @@ private CompletableFuture<Response> doInvokeApi(String method,
|
324 | 330 | return future;
|
325 | 331 | }
|
326 | 332 |
|
327 |
| - /** |
328 |
| - * Tries to parse an error from Dapr response body. |
329 |
| - * |
330 |
| - * @param json Response body from Dapr. |
331 |
| - * @return DaprError or null if could not parse. |
332 |
| - */ |
333 |
| - private static DaprError parseDaprError(byte[] json) { |
334 |
| - if ((json == null) || (json.length == 0)) { |
335 |
| - return null; |
336 |
| - } |
337 |
| - |
338 |
| - try { |
339 |
| - return OBJECT_MAPPER.readValue(json, DaprError.class); |
340 |
| - } catch (IOException e) { |
341 |
| - throw new DaprException("UNKNOWN", new String(json, StandardCharsets.UTF_8)); |
342 |
| - } |
343 |
| - } |
344 |
| - |
345 | 333 | private static byte[] getBodyBytesOrEmptyArray(okhttp3.Response response) throws IOException {
|
346 | 334 | ResponseBody body = response.body();
|
347 | 335 | if (body != null) {
|
@@ -369,24 +357,8 @@ public void onFailure(Call call, IOException e) {
|
369 | 357 | @Override
|
370 | 358 | public void onResponse(@NotNull Call call, @NotNull okhttp3.Response response) throws IOException {
|
371 | 359 | if (!response.isSuccessful()) {
|
372 |
| - try { |
373 |
| - DaprError error = parseDaprError(getBodyBytesOrEmptyArray(response)); |
374 |
| - if ((error != null) && (error.getErrorCode() != null)) { |
375 |
| - if (error.getMessage() != null) { |
376 |
| - future.completeExceptionally(new DaprException(error)); |
377 |
| - } else { |
378 |
| - future.completeExceptionally( |
379 |
| - new DaprException(error.getErrorCode(), "HTTP status code: " + response.code())); |
380 |
| - } |
381 |
| - return; |
382 |
| - } |
383 |
| - |
384 |
| - future.completeExceptionally(new DaprException("UNKNOWN", "HTTP status code: " + response.code())); |
385 |
| - return; |
386 |
| - } catch (DaprException e) { |
387 |
| - future.completeExceptionally(new DaprHttpException(response)); |
388 |
| - return; |
389 |
| - } |
| 360 | + DaprException customException = parser.parse(response); |
| 361 | + future.completeExceptionally(customException); |
390 | 362 | }
|
391 | 363 |
|
392 | 364 | Map<String, String> mapHeaders = new HashMap<>();
|
|
0 commit comments