From 33ff704b8c4ecd983ad75d5fe8388d838cfe7d69 Mon Sep 17 00:00:00 2001 From: ramsessanchez <63934382+ramsessanchez@users.noreply.github.com> Date: Mon, 6 Nov 2023 14:43:59 -0800 Subject: [PATCH] re-introduce the exceptions at their previous throw points --- .../kiota/http/OkHttpRequestAdapter.java | 26 +++++++++---------- .../kiota/http/OkHttpRequestAdapterTest.java | 4 +-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java index a581416e9..83176d32b 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java @@ -59,7 +59,7 @@ /** RequestAdapter implementation for OkHttp */ public class OkHttpRequestAdapter implements com.microsoft.kiota.RequestAdapter { - private final static String contentTypeHeaderKey = "Content-Type"; + private static final String contentTypeHeaderKey = "Content-Type"; @Nonnull private final OkHttpClient client; @Nonnull @@ -162,9 +162,9 @@ public void enableBackingStore(@Nullable final BackingStoreFactory backingStoreF } } - private final String nullRequestInfoParameter = "parameter requestInfo cannot be null"; - private final String nullTargetClassParameter = "parameter targetClass cannot be null"; - private final String nullFactoryParameter = "parameter factory cannot be null"; + private static final String nullRequestInfoParameter = "parameter requestInfo cannot be null"; + private static final String nullTargetClassParameter = "parameter targetClass cannot be null"; + private static final String nullFactoryParameter = "parameter factory cannot be null"; @Nullable public List sendCollection(@Nonnull final RequestInformation requestInfo, @Nonnull final ParsableFactory factory, @Nullable final HashMap> errorMappings) { Objects.requireNonNull(requestInfo, nullRequestInfoParameter); @@ -606,7 +606,7 @@ private Response throwIfFailedResponse(@Nonnull final Response response, @Nonnul } } private final static String claimsKey = "claims"; - private Response getHttpResponseMessage(@Nonnull final RequestInformation requestInfo, @Nonnull final Span parentSpan, @Nonnull final Span spanForAttributes, @Nullable final String claims) throws IOException { + private Response getHttpResponseMessage(@Nonnull final RequestInformation requestInfo, @Nonnull final Span parentSpan, @Nonnull final Span spanForAttributes, @Nullable final String claims) { Objects.requireNonNull(requestInfo, nullRequestInfoParameter); final Span span = GlobalOpenTelemetry.getTracer(obsOptions.getTracerInstrumentationName()).spanBuilder("getHttpResponseMessage").setParent(Context.current().with(parentSpan)).startSpan(); try(final Scope scope = span.makeCurrent()) { @@ -632,9 +632,9 @@ private Response getHttpResponseMessage(@Nonnull final RequestInformation reques spanForAttributes.setAttribute(SemanticAttributes.HTTP_FLAVOR, response.protocol().toString().toUpperCase(Locale.ROOT)); return this.retryCAEResponseIfRequired(response, requestInfo, span, spanForAttributes, claims); } - catch ( IOException ex) { + catch (IOException | URISyntaxException ex) { spanForAttributes.recordException(ex); - throw ex; + throw new RuntimeException(ex); } finally { span.end(); } @@ -655,7 +655,7 @@ private String getHeaderValue(final Response response, String key) { /** Key used for events when an authentication challenge is returned by the API */ @Nonnull public static final String authenticateChallengedEventKey = "com.microsoft.kiota.authenticate_challenge_received"; - private Response retryCAEResponseIfRequired(@Nonnull final Response response, @Nonnull final RequestInformation requestInfo, @Nonnull final Span parentSpan, @Nonnull final Span spanForAttributes, @Nullable final String claims) throws IOException { + private Response retryCAEResponseIfRequired(@Nonnull final Response response, @Nonnull final RequestInformation requestInfo, @Nonnull final Span parentSpan, @Nonnull final Span spanForAttributes, @Nullable final String claims) { final Span span = GlobalOpenTelemetry.getTracer(obsOptions.getTracerInstrumentationName()).spanBuilder("retryCAEResponseIfRequired").setParent(Context.current().with(parentSpan)).startSpan(); try(final Scope scope = span.makeCurrent()) { final String responseClaims = this.getClaimsFromResponse(response, requestInfo, claims); @@ -665,7 +665,7 @@ private Response retryCAEResponseIfRequired(@Nonnull final Response response, @N requestInfo.content.reset(); } catch (IOException ex) { spanForAttributes.recordException(ex); - throw ex; + throw new RuntimeException(ex); } } closeResponse(true, response); @@ -718,6 +718,9 @@ public T convertToNativeRequest(@Nonnull final RequestInformation requestInf try(final Scope scope = span.makeCurrent()) { this.authProvider.authenticateRequest(requestInfo, null); return (T) getRequestFromRequestInformation(requestInfo, span, span); + } catch (URISyntaxException | MalformedURLException ex) { + span.recordException(ex); + throw new RuntimeException(ex); } finally { span.end(); } @@ -733,7 +736,7 @@ public T convertToNativeRequest(@Nonnull final RequestInformation requestInf * @throws URISyntaxException if the URI is invalid. * @throws MalformedURLException if the URL is invalid. */ - protected @Nonnull Request getRequestFromRequestInformation(@Nonnull final RequestInformation requestInfo, @Nonnull final Span parentSpan, @Nonnull final Span spanForAttributes) { + protected @Nonnull Request getRequestFromRequestInformation(@Nonnull final RequestInformation requestInfo, @Nonnull final Span parentSpan, @Nonnull final Span spanForAttributes) throws URISyntaxException, MalformedURLException{ final Span span = GlobalOpenTelemetry.getTracer(obsOptions.getTracerInstrumentationName()).spanBuilder("getRequestFromRequestInformation").setParent(Context.current().with(parentSpan)).startSpan(); try(final Scope scope = span.makeCurrent()) { spanForAttributes.setAttribute(SemanticAttributes.HTTP_METHOD, requestInfo.httpMethod.toString()); @@ -802,9 +805,6 @@ public void writeTo(@Nonnull BufferedSink sink) throws IOException { } } return request; - } catch(URISyntaxException | MalformedURLException ex) { - span.recordException(ex); - throw new RuntimeException(ex); } finally { span.end(); } diff --git a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/OkHttpRequestAdapterTest.java b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/OkHttpRequestAdapterTest.java index 48543dd0a..f9732934b 100644 --- a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/OkHttpRequestAdapterTest.java +++ b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/OkHttpRequestAdapterTest.java @@ -47,7 +47,7 @@ public class OkHttpRequestAdapterTest { @EnumSource(value = HttpMethod.class, names = {"PUT", "POST", "PATCH"}) public void PostRequestsShouldHaveEmptyBody(HttpMethod method) throws Exception { // Unexpected exception thrown: java.lang.IllegalArgumentException: method POST must have a request body. final AuthenticationProvider authenticationProviderMock = mock(AuthenticationProvider.class); - final OkHttpRequestAdapter adapter = new OkHttpRequestAdapter(authenticationProviderMock) { + final var adapter = new OkHttpRequestAdapter(authenticationProviderMock) { public Request test() throws Exception { RequestInformation ri = new RequestInformation(); ri.httpMethod = method; @@ -184,7 +184,7 @@ public void throwsAPIException() throws Exception { when(mockFactory.getParseNode(any(String.class), any(InputStream.class))).thenReturn(mockParseNode); when(mockFactory.getValidContentType()).thenReturn("application/json"); final var requestAdapter = new OkHttpRequestAdapter(authenticationProviderMock, mockFactory, null, client); - final var exception = assertThrows(ExecutionException.class, ()->requestAdapter.send(requestInformation, (node) -> mockEntity, null)) ; + final var exception = assertThrows(RuntimeException.class, ()->requestAdapter.send(requestInformation, (node) -> mockEntity, null)) ; final var cause = exception.getCause(); assertTrue(cause instanceof ApiException); assertEquals(404, ((ApiException)cause).getResponseStatusCode());