From 00cbebceabbae7d4bfc3c3c48b59cb3dbf166db4 Mon Sep 17 00:00:00 2001 From: Martynas Date: Sun, 29 Oct 2023 23:25:48 +0100 Subject: [PATCH] Improved error logging in `graph/Item::put` Improved error handling in `CSVGraphStoreRowProcessor` --- .../stream/csv/CSVGraphStoreRowProcessor.java | 17 ++++++++++++++--- .../linkeddatahub/resource/graph/Item.java | 15 ++++++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/atomgraph/linkeddatahub/imports/stream/csv/CSVGraphStoreRowProcessor.java b/src/main/java/com/atomgraph/linkeddatahub/imports/stream/csv/CSVGraphStoreRowProcessor.java index aee822bed..48633eeec 100644 --- a/src/main/java/com/atomgraph/linkeddatahub/imports/stream/csv/CSVGraphStoreRowProcessor.java +++ b/src/main/java/com/atomgraph/linkeddatahub/imports/stream/csv/CSVGraphStoreRowProcessor.java @@ -24,6 +24,7 @@ import jakarta.ws.rs.client.Client; import jakarta.ws.rs.client.Entity; import jakarta.ws.rs.core.Response; +import java.io.IOException; import java.net.URI; import org.apache.jena.atlas.lib.IRILib; import org.apache.jena.query.Dataset; @@ -34,6 +35,8 @@ import org.apache.jena.rdf.model.Property; import org.apache.jena.rdf.model.Resource; import org.glassfish.jersey.uri.UriComponent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @@ -42,6 +45,8 @@ public class CSVGraphStoreRowProcessor implements RowProcessor // extends com.atomgraph.etl.csv.stream.CSVStreamRDFProcessor { + private static final Logger log = LoggerFactory.getLogger(CSVGraphStoreRowProcessor.class); + private final Service service, adminService; private final LinkedDataClient ldc; private final String base; @@ -102,10 +107,16 @@ public void rowProcessed(String[] row, ParsingContext context) protected Response put(Entity entity, String graphURI) { // forward the stream to the named graph document. Buffer the entity first so that the server response is not returned before the client response completes - try (Response response = getLinkedDataClient().put(URI.create(graphURI), getLinkedDataClient().getReadableMediaTypes(Model.class), entity)) + try (Response cr = getLinkedDataClient().put(URI.create(graphURI), getLinkedDataClient().getReadableMediaTypes(Model.class), entity)) { - return Response.status(response.getStatus()). - entity(response.readEntity(Model.class)). + if (!cr.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL)) + { + if (log.isErrorEnabled()) log.error("RDF document with URI <{}> could not be successfully created using PUT. Status code: {}", graphURI, cr.getStatus()); + throw new RuntimeException(new IOException("RDF document with URI <" + graphURI + "> could not be successfully created using PUT. Status code: " + cr.getStatus())); + } + + return Response.status(cr.getStatus()). + entity(cr.readEntity(Model.class)). build(); } } diff --git a/src/main/java/com/atomgraph/linkeddatahub/resource/graph/Item.java b/src/main/java/com/atomgraph/linkeddatahub/resource/graph/Item.java index 20f5b9851..e4c3f0341 100644 --- a/src/main/java/com/atomgraph/linkeddatahub/resource/graph/Item.java +++ b/src/main/java/com/atomgraph/linkeddatahub/resource/graph/Item.java @@ -171,11 +171,17 @@ public Response put(Model model, @QueryParam("default") @DefaultValue("false") B Set allowedMethods = getAllowedMethods(getURI()); if (!allowedMethods.contains(HttpMethod.PUT)) - throw new WebApplicationException("Cannot update document", Response.status(Response.Status.METHOD_NOT_ALLOWED).allow(allowedMethods).build()); + { + if (log.isErrorEnabled()) log.error("Method '{}' is not allowed on document URI <{}>", HttpMethod.PUT, getURI()); + throw new WebApplicationException("Method '" + HttpMethod.PUT + "' is not allowed on document URI <" + getURI() + ">", Response.status(Response.Status.METHOD_NOT_ALLOWED).allow(allowedMethods).build()); + } // enforce that document URIs always end with a slash if (!getURI().toString().endsWith("/")) - throw new WebApplicationException("Document URIs need to end with a slash", UNPROCESSABLE_ENTITY.getStatusCode()); // 422 Unprocessable Entity + { + if (log.isErrorEnabled()) log.error("Document URI <{}> does not end with a slash", getURI()); + throw new WebApplicationException("Document URI <" + getURI() + "> does not end with a slash", UNPROCESSABLE_ENTITY.getStatusCode()); // 422 Unprocessable Entity + } final boolean existingGraph = getDatasetAccessor().containsModel(getURI().toString()); @@ -202,7 +208,10 @@ public Response put(Model model, @QueryParam("default") @DefaultValue("false") B if (!resource.hasProperty(RDF.type, Default.Root) && !resource.hasProperty(RDF.type, DH.Container) && !resource.hasProperty(RDF.type, DH.Item)) - throw new WebApplicationException("Named graph <" + getURI() + "> must contain a document resource (instance of dh:Container or dh:Item)", 422); // 422 Unprocessable Entity + { + if (log.isErrorEnabled()) log.error("Named graph <{}> must contain a document resource (instance of dh:Container or dh:Item)", getURI()); + throw new WebApplicationException("Named graph <" + getURI() + "> must contain a document resource (instance of dh:Container or dh:Item)", UNPROCESSABLE_ENTITY.getStatusCode()); // 422 Unprocessable Entity + } resource.removeAll(DCTerms.modified). addLiteral(DCTerms.modified, ResourceFactory.createTypedLiteral(GregorianCalendar.getInstance()));