Skip to content

Commit

Permalink
Improved error logging in graph/Item::put
Browse files Browse the repository at this point in the history
Improved error handling in `CSVGraphStoreRowProcessor`
  • Loading branch information
namedgraph committed Oct 29, 2023
1 parent 65896a0 commit 00cbebc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
*
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/com/atomgraph/linkeddatahub/resource/graph/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,17 @@ public Response put(Model model, @QueryParam("default") @DefaultValue("false") B

Set<String> 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());

Expand All @@ -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()));
Expand Down

0 comments on commit 00cbebc

Please sign in to comment.