From 3b5f7a336fec53f49b137caf051ba4033bb8f7c2 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Mon, 7 Feb 2022 14:28:48 -0500 Subject: [PATCH] - fixes error response implementation in java to avoid bypassing native response handler Signed-off-by: Vincent Biret --- .../kiota/http/OkHttpRequestAdapter.java | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/http/java/okhttp/lib/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java b/http/java/okhttp/lib/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java index e381ca8e37..c91373631e 100644 --- a/http/java/okhttp/lib/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java +++ b/http/java/okhttp/lib/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java @@ -97,13 +97,15 @@ public CompletableFuture> sendC Objects.requireNonNull(requestInfo, "parameter requestInfo cannot be null"); return this.getHttpResponseMessage(requestInfo) - .thenCompose(r -> this.throwFailedResponse(r, errorMappings)) .thenCompose(response -> { if(responseHandler == null) { try { + this.throwFailedResponse(response, errorMappings); final ParseNode rootNode = getRootParseNode(response); final Iterable result = rootNode.getCollectionOfObjectValues(targetClass); return CompletableFuture.completedStage(result); + } catch(ApiException ex) { + return CompletableFuture.failedFuture(ex); } catch(IOException ex) { return CompletableFuture.failedFuture(new RuntimeException("failed to read the response body", ex)); } finally { @@ -119,13 +121,15 @@ public CompletableFuture sendAsync(@Nonn Objects.requireNonNull(requestInfo, "parameter requestInfo cannot be null"); return this.getHttpResponseMessage(requestInfo) - .thenCompose(r -> this.throwFailedResponse(r, errorMappings)) .thenCompose(response -> { if(responseHandler == null) { try { + this.throwFailedResponse(response, errorMappings); final ParseNode rootNode = getRootParseNode(response); final ModelType result = rootNode.getObjectValue(targetClass); return CompletableFuture.completedStage(result); + } catch(ApiException ex) { + return CompletableFuture.failedFuture(ex); } catch(IOException ex) { return CompletableFuture.failedFuture(new RuntimeException("failed to read the response body", ex)); } finally { @@ -142,10 +146,10 @@ private String getMediaTypeAndSubType(final MediaType mediaType) { @Nonnull public CompletableFuture sendPrimitiveAsync(@Nonnull final RequestInformation requestInfo, @Nonnull final Class targetClass, @Nullable final ResponseHandler responseHandler, @Nullable final HashMap> errorMappings) { return this.getHttpResponseMessage(requestInfo) - .thenCompose(r -> this.throwFailedResponse(r, errorMappings)) .thenCompose(response -> { if(responseHandler == null) { try { + this.throwFailedResponse(response, errorMappings); if(targetClass == Void.class) { return CompletableFuture.completedStage(null); } else { @@ -175,6 +179,8 @@ public CompletableFuture sendPrimitiveAsync(@Nonnull fina } return CompletableFuture.completedStage((ModelType)result); } + } catch(ApiException ex) { + return CompletableFuture.failedFuture(ex); } catch(IOException ex) { return CompletableFuture.failedFuture(new RuntimeException("failed to read the response body", ex)); } finally { @@ -189,13 +195,15 @@ public CompletableFuture> sendPrimitiveCollectio Objects.requireNonNull(requestInfo, "parameter requestInfo cannot be null"); return this.getHttpResponseMessage(requestInfo) - .thenCompose(r -> this.throwFailedResponse(r, errorMappings)) .thenCompose(response -> { if(responseHandler == null) { try { + this.throwFailedResponse(response, errorMappings); final ParseNode rootNode = getRootParseNode(response); final Iterable result = rootNode.getCollectionOfPrimitiveValues(targetClass); return CompletableFuture.completedStage(result); + } catch(ApiException ex) { + return CompletableFuture.failedFuture(ex); } catch(IOException ex) { return CompletableFuture.failedFuture(new RuntimeException("failed to read the response body", ex)); } finally { @@ -213,8 +221,8 @@ private ParseNode getRootParseNode(final Response response) throws IOException { return rootNode; } } - private CompletableFuture throwFailedResponse(final Response response, final HashMap> errorMappings) { - if (response.isSuccessful()) return CompletableFuture.completedFuture(response); + private Response throwFailedResponse(final Response response, final HashMap> errorMappings) throws IOException, ApiException { + if (response.isSuccessful()) return response; final String statusCodeAsString = Integer.toString(response.code()); final Integer statusCode = response.code(); @@ -222,7 +230,7 @@ private CompletableFuture throwFailedResponse(final Response response, !errorMappings.containsKey(statusCodeAsString) && !(statusCode >= 400 && statusCode < 500 && errorMappings.containsKey("4XX")) && !(statusCode >= 500 && statusCode < 600 && errorMappings.containsKey("5XX"))) { - return CompletableFuture.failedFuture(new ApiException("the server returned an unexpected status code and no error class is registered for this code " + statusCode)); + throw new ApiException("the server returned an unexpected status code and no error class is registered for this code " + statusCode); } final Class errorClass = errorMappings.containsKey(statusCodeAsString) ? errorMappings.get(statusCodeAsString) : @@ -232,13 +240,11 @@ private CompletableFuture throwFailedResponse(final Response response, try { final ParseNode rootNode = getRootParseNode(response); final Parsable error = rootNode.getObjectValue(errorClass); - if (error instanceof Exception) { - return CompletableFuture.failedFuture((Exception)error); + if (error instanceof ApiException) { + throw (ApiException)error; } else { - return CompletableFuture.failedFuture(new ApiException("unexpected error type " + error.getClass().getName())); + throw new ApiException("unexpected error type " + error.getClass().getName()); } - } catch (IOException ex) { - return CompletableFuture.failedFuture(ex); } finally { response.close(); }