Skip to content

Commit d722196

Browse files
committed
Provide exception mapper to throwing handler
1 parent 50f8786 commit d722196

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

solid/src/main/java/com/inrupt/client/solid/SolidClient.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.IOException;
2828
import java.io.InputStream;
2929
import java.net.URI;
30+
import java.nio.charset.StandardCharsets;
3031
import java.util.Collections;
3132
import java.util.List;
3233
import java.util.Map;
@@ -62,6 +63,18 @@ public class SolidClient {
6263
this.fetchAfterWrite = fetchAfterWrite;
6364
}
6465

66+
private static Function<Response.ResponseInfo, ClientHttpException> httpExceptionMapper(final String message) {
67+
return res -> {
68+
return SolidClientException.handle(
69+
message,
70+
res.uri(),
71+
res.statusCode(),
72+
res.headers(),
73+
new String(res.body().array(), StandardCharsets.UTF_8)
74+
);
75+
};
76+
}
77+
6578
/**
6679
* Create a session-scoped client.
6780
*
@@ -126,7 +139,8 @@ public <T extends Resource> CompletionStage<T> read(final URI identifier, final
126139
request,
127140
Response.BodyHandlers.throwOnError(
128141
Response.BodyHandlers.ofByteArray(),
129-
(r) -> isSuccess(r.statusCode())
142+
(r) -> isSuccess(r.statusCode()),
143+
httpExceptionMapper("Reading resource " + request.uri() + " failed.")
130144
)
131145
).thenApply(response -> {
132146
final String contentType = response.headers().firstValue(CONTENT_TYPE)
@@ -153,11 +167,6 @@ public <T extends Resource> CompletionStage<T> read(final URI identifier, final
153167
throw new SolidResourceException("Unable to read resource into type " + clazz.getName(),
154168
ex);
155169
}
156-
}).exceptionally(exception -> {
157-
if (exception instanceof ClientHttpException) {
158-
throw SolidClientException.handle((ClientHttpException) exception);
159-
}
160-
throw new RuntimeException("Something went wrong reading " + request.uri(), exception);
161170
});
162171
}
163172

@@ -280,15 +289,11 @@ public <T extends Resource> CompletionStage<Void> delete(final T resource, final
280289
builder.build(),
281290
Response.BodyHandlers.throwOnError(
282291
Response.BodyHandlers.ofByteArray(),
283-
(r) -> isSuccess(r.statusCode())
292+
(r) -> isSuccess(r.statusCode()),
293+
httpExceptionMapper("Deleting resource " + resource.getIdentifier() + " failed.")
284294
)
285-
).exceptionally(exception -> {
286-
if (exception instanceof ClientHttpException) {
287-
throw SolidClientException.handle((ClientHttpException) exception);
288-
}
289-
throw new RuntimeException("Something went wrong reading " + resource.getIdentifier(), exception);
290-
// FIXME I don't understand why the following is required.
291-
}).thenAccept(o -> { /* no-op */ });
295+
)// FIXME I don't understand why the following is required.
296+
.thenAccept(o -> { /* no-op */ });
292297
}
293298

294299
/**

0 commit comments

Comments
 (0)