2727import java .io .IOException ;
2828import java .io .InputStream ;
2929import java .net .URI ;
30+ import java .nio .charset .StandardCharsets ;
3031import java .util .Collections ;
3132import java .util .List ;
3233import 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