Skip to content

Commit

Permalink
re-introduce the exceptions at their previous throw points
Browse files Browse the repository at this point in the history
  • Loading branch information
ramsessanchez committed Nov 6, 2023
1 parent 9493e97 commit 33ff704
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <ModelType extends Parsable> List<ModelType> sendCollection(@Nonnull final RequestInformation requestInfo, @Nonnull final ParsableFactory<ModelType> factory, @Nullable final HashMap<String, ParsableFactory<? extends Parsable>> errorMappings) {
Objects.requireNonNull(requestInfo, nullRequestInfoParameter);
Expand Down Expand Up @@ -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()) {
Expand All @@ -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();
}
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -718,6 +718,9 @@ public <T> 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();
}
Expand All @@ -733,7 +736,7 @@ public <T> 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());
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 33ff704

Please sign in to comment.