Skip to content

Commit

Permalink
Provide exception mapper to throwing handler
Browse files Browse the repository at this point in the history
  • Loading branch information
NSeydoux committed Apr 17, 2024
1 parent 50f8786 commit d722196
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions solid/src/main/java/com/inrupt/client/solid/SolidClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -62,6 +63,18 @@ public class SolidClient {
this.fetchAfterWrite = fetchAfterWrite;
}

private static Function<Response.ResponseInfo, ClientHttpException> httpExceptionMapper(final String message) {
return res -> {
return SolidClientException.handle(
message,
res.uri(),
res.statusCode(),
res.headers(),
new String(res.body().array(), StandardCharsets.UTF_8)
);
};
}

/**
* Create a session-scoped client.
*
Expand Down Expand Up @@ -126,7 +139,8 @@ public <T extends Resource> CompletionStage<T> read(final URI identifier, final
request,
Response.BodyHandlers.throwOnError(
Response.BodyHandlers.ofByteArray(),
(r) -> isSuccess(r.statusCode())
(r) -> isSuccess(r.statusCode()),
httpExceptionMapper("Reading resource " + request.uri() + " failed.")
)
).thenApply(response -> {
final String contentType = response.headers().firstValue(CONTENT_TYPE)
Expand All @@ -153,11 +167,6 @@ public <T extends Resource> CompletionStage<T> read(final URI identifier, final
throw new SolidResourceException("Unable to read resource into type " + clazz.getName(),
ex);
}
}).exceptionally(exception -> {
if (exception instanceof ClientHttpException) {
throw SolidClientException.handle((ClientHttpException) exception);
}
throw new RuntimeException("Something went wrong reading " + request.uri(), exception);
});
}

Expand Down Expand Up @@ -280,15 +289,11 @@ public <T extends Resource> CompletionStage<Void> delete(final T resource, final
builder.build(),
Response.BodyHandlers.throwOnError(
Response.BodyHandlers.ofByteArray(),
(r) -> isSuccess(r.statusCode())
(r) -> isSuccess(r.statusCode()),
httpExceptionMapper("Deleting resource " + resource.getIdentifier() + " failed.")
)
).exceptionally(exception -> {
if (exception instanceof ClientHttpException) {
throw SolidClientException.handle((ClientHttpException) exception);
}
throw new RuntimeException("Something went wrong reading " + resource.getIdentifier(), exception);
// FIXME I don't understand why the following is required.
}).thenAccept(o -> { /* no-op */ });
)// FIXME I don't understand why the following is required.
.thenAccept(o -> { /* no-op */ });
}

/**
Expand Down

0 comments on commit d722196

Please sign in to comment.