Skip to content

Commit

Permalink
Merge pull request #742 from andreaTP/fix-error
Browse files Browse the repository at this point in the history
Preserve user errors instead of creating new ApiExceptions
  • Loading branch information
baywet authored Oct 13, 2023
2 parents ba8599b + c7a0b30 commit 02e7f8e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
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

0 comments on commit 02e7f8e

Please sign in to comment.