Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve user errors instead of creating new ApiExceptions #742

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

## [0.7.8] - 2023-10-13

### Fixed

- Fixed a bug to preserve the user defined error instead of converting it to generic ApiException.

## [0.7.7] - 2023-10-12

### Added
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.microsoft.kiota;

import com.microsoft.kiota.serialization.Parsable;
import jakarta.annotation.Nonnull;

import java.util.Objects;
import java.util.function.Supplier;

/** Builder class for ApiException. */
public class ApiExceptionBuilder {
Expand All @@ -17,11 +19,18 @@ public ApiExceptionBuilder() {

/**
* Constructs an ApiExceptionBuilder starting from a base ApiException
* @param base The original ApiException to be used as a base.
* @param builder A builder for the ApiException to be used as a base.
*/
public ApiExceptionBuilder(@Nonnull final ApiException base) {
Objects.requireNonNull(base);
value = new ApiException(base.getMessage(), base.getCause());
public ApiExceptionBuilder(@Nonnull final Supplier<Parsable> builder) {
Objects.requireNonNull(builder);
final Parsable error = builder.get();
if (error instanceof ApiException) {
value = (ApiException) error;
} else {
value = new ApiExceptionBuilder()
.withMessage("\"unexpected error type \" + error.getClass().getName()")
.build();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,15 +625,7 @@ private Response throwIfFailedResponse(@Nonnull final Response response, @Nonnul
spanForAttributes.setAttribute(errorBodyFoundAttributeName, true);
final Span deserializationSpan = GlobalOpenTelemetry.getTracer(obsOptions.getTracerInstrumentationName()).spanBuilder("getObjectValue").setParent(Context.current().with(span)).startSpan();
try(final Scope deserializationScope = deserializationSpan.makeCurrent()) {
final Parsable error = rootNode.getObjectValue(errorClass);
ApiExceptionBuilder resultBuilder;
if (error instanceof ApiException) {
resultBuilder = new ApiExceptionBuilder((ApiException)error);
} else {
resultBuilder = new ApiExceptionBuilder()
.withMessage("unexpected error type " + error.getClass().getName());
}
ApiException result = resultBuilder
ApiException result = new ApiExceptionBuilder(() -> rootNode.getObjectValue(errorClass))
.withResponseStatusCode(statusCode)
.withResponseHeaders(responseHeaders)
.build();
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ org.gradle.caching=true
mavenGroupId = com.microsoft.kiota
mavenMajorVersion = 0
mavenMinorVersion = 7
mavenPatchVersion = 7
mavenPatchVersion = 8
mavenArtifactSuffix =

#These values are used to run functional tests
Expand Down