@@ -125,42 +125,39 @@ public <T extends Resource> CompletionStage<T> read(final URI identifier, final
125125 headers .firstValue (USER_AGENT ).ifPresent (agent -> builder .setHeader (USER_AGENT , agent ));
126126
127127 final Request request = builder .build ();
128- return client .send (request , Response .BodyHandlers .ofByteArray ())
129- .thenApply (response -> {
130- if (response .statusCode () >= ERROR_STATUS ) {
131- throw SolidClientException .handle (
132- "Unable to read resource at " + request .uri (),
133- response .uri (),
134- response .statusCode (),
135- response .headers (),
136- new String (response .body ())
137- );
138- } else {
139- final String contentType = response .headers ().firstValue (CONTENT_TYPE )
140- .orElse ("application/octet-stream" );
141- try {
142- // Check that this is an RDFSoure
143- if (RDFSource .class .isAssignableFrom (clazz )) {
144- final Dataset dataset = SolidResourceHandlers .buildDataset (contentType , response .body (),
145- request .uri ().toString ()).orElse (null );
146- final T obj = construct (request .uri (), clazz , dataset , response .headers ());
147- final ValidationResult res = RDFSource .class .cast (obj ).validate ();
148- if (!res .isValid ()) {
149- throw new DataMappingException (
150- "Unable to map resource into type: [" + clazz .getSimpleName () + "] " ,
151- res .getResults ());
152- }
153- return obj ;
154- // Otherwise, create a non-RDF-bearing resource
155- } else {
156- return construct (request .uri (), clazz , contentType ,
157- new ByteArrayInputStream (response .body ()), response .headers ());
128+ return client .send (
129+ request ,
130+ Response .BodyHandlers .throwOnError (Response .BodyHandlers .ofByteArray (), (r ) -> isSuccess (r .statusCode ()))
131+ ).thenApply (response -> {
132+ final String contentType = response .headers ().firstValue (CONTENT_TYPE )
133+ .orElse ("application/octet-stream" );
134+ try {
135+ // Check that this is an RDFSoure
136+ if (RDFSource .class .isAssignableFrom (clazz )) {
137+ final Dataset dataset = SolidResourceHandlers .buildDataset (contentType , response .body (),
138+ request .uri ().toString ()).orElse (null );
139+ final T obj = construct (request .uri (), clazz , dataset , response .headers ());
140+ final ValidationResult res = RDFSource .class .cast (obj ).validate ();
141+ if (!res .isValid ()) {
142+ throw new DataMappingException (
143+ "Unable to map resource into type: [" + clazz .getSimpleName () + "] " ,
144+ res .getResults ());
158145 }
159- } catch (final ReflectiveOperationException ex ) {
160- throw new SolidResourceException ("Unable to read resource into type " + clazz .getName (),
161- ex );
146+ return obj ;
147+ // Otherwise, create a non-RDF-bearing resource
148+ } else {
149+ return construct (request .uri (), clazz , contentType ,
150+ new ByteArrayInputStream (response .body ()), response .headers ());
162151 }
152+ } catch (final ReflectiveOperationException ex ) {
153+ throw new SolidResourceException ("Unable to read resource into type " + clazz .getName (),
154+ ex );
163155 }
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 );
164161 });
165162 }
166163
@@ -279,19 +276,19 @@ public <T extends Resource> CompletionStage<Void> delete(final T resource, final
279276 defaultHeaders .firstValue (USER_AGENT ).ifPresent (agent -> builder .setHeader (USER_AGENT , agent ));
280277 headers .firstValue (USER_AGENT ).ifPresent (agent -> builder .setHeader (USER_AGENT , agent ));
281278
282- return client .send (builder .build (), Response .BodyHandlers .ofByteArray ()).thenApply (res -> {
283- if (isSuccess (res .statusCode ())) {
284- return null ;
285- } else {
286- throw SolidClientException .handle (
287- "Unable to delete resource" ,
288- resource .getIdentifier (),
289- res .statusCode (),
290- res .headers (),
291- new String (res .body (), StandardCharsets .UTF_8 )
292- );
279+ return client .send (
280+ builder .build (),
281+ Response .BodyHandlers .throwOnError (
282+ Response .BodyHandlers .ofByteArray (),
283+ (r ) -> isSuccess (r .statusCode ())
284+ )
285+ ).exceptionally (exception -> {
286+ if (exception instanceof ClientHttpException ) {
287+ throw SolidClientException .handle ((ClientHttpException ) exception );
293288 }
294- });
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 -> {});
295292 }
296293
297294 /**
0 commit comments