Log error cases

This commit is contained in:
René Pfeuffer
2018-08-14 13:58:22 +02:00
parent dcdab9a00e
commit 99a17c5103

View File

@@ -38,33 +38,23 @@ public class ContentResource {
@Path("{revision}/{path: .*}") @Path("{revision}/{path: .*}")
public Response get(@PathParam("namespace") String namespace, @PathParam("name") String name, @PathParam("revision") String revision, @PathParam("path") String path) { public Response get(@PathParam("namespace") String namespace, @PathParam("name") String name, @PathParam("revision") String revision, @PathParam("path") String path) {
try (RepositoryService repositoryService = serviceFactory.create(new NamespaceAndName(namespace, name))) { try (RepositoryService repositoryService = serviceFactory.create(new NamespaceAndName(namespace, name))) {
try {
StreamingOutput stream = os -> { StreamingOutput stream = os -> {
try { try {
repositoryService.getCatCommand().setRevision(revision).retriveContent(os, path); repositoryService.getCatCommand().setRevision(revision).retriveContent(os, path);
} catch (PathNotFoundException e) { } catch (PathNotFoundException e) {
LOG.debug("path '{}' not found in repository {}/{}", path, namespace, name, e);
throw new WebApplicationException(Status.NOT_FOUND); throw new WebApplicationException(Status.NOT_FOUND);
} catch (RepositoryException e) { } catch (RepositoryException e) {
LOG.info("error reading repository resource {} from {}/{}", path, namespace, name, e);
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR); throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
} }
os.close(); os.close();
}; };
Response.ResponseBuilder responseBuilder = Response.ok(stream); Response.ResponseBuilder responseBuilder = Response.ok(stream);
appendContentType(path, getHead(revision, path, repositoryService), responseBuilder); return createContentHeader(namespace, name, revision, path, repositoryService, responseBuilder);
return responseBuilder.build();
} catch (PathNotFoundException e) {
return Response.status(Status.NOT_FOUND).build();
} catch (IOException e) {
LOG.error("error reading repository resource {} from {}/{}", path, namespace, name, e);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
} catch (RepositoryException e) {
LOG.error("error reading repository resource {} from {}/{}", path, namespace, name, e);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
}
} catch (RepositoryNotFoundException e) { } catch (RepositoryNotFoundException e) {
LOG.debug("path '{}' not found in repository {}/{}", path, namespace, name, e);
return Response.status(Status.NOT_FOUND).build(); return Response.status(Status.NOT_FOUND).build();
} }
} }
@@ -73,27 +63,31 @@ public class ContentResource {
@Path("{revision}/{path: .*}") @Path("{revision}/{path: .*}")
public Response metadata(@PathParam("namespace") String namespace, @PathParam("name") String name, @PathParam("revision") String revision, @PathParam("path") String path) { public Response metadata(@PathParam("namespace") String namespace, @PathParam("name") String name, @PathParam("revision") String revision, @PathParam("path") String path) {
try (RepositoryService repositoryService = serviceFactory.create(new NamespaceAndName(namespace, name))) { try (RepositoryService repositoryService = serviceFactory.create(new NamespaceAndName(namespace, name))) {
try {
Response.ResponseBuilder responseBuilder = Response.ok(); Response.ResponseBuilder responseBuilder = Response.ok();
return createContentHeader(namespace, name, revision, path, repositoryService, responseBuilder);
appendContentType(path, getHead(revision, path, repositoryService), responseBuilder);
return responseBuilder.build();
} catch (PathNotFoundException e) {
return Response.status(Status.NOT_FOUND).build();
} catch (IOException e) {
LOG.error("error reading repository resource {} from {}/{}", path, namespace, name, e);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
} catch (RepositoryException e) {
LOG.error("error reading repository resource {} from {}/{}", path, namespace, name, e);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
}
} catch (RepositoryNotFoundException e) { } catch (RepositoryNotFoundException e) {
LOG.debug("path '{}' not found in repository {}/{}", path, namespace, name, e);
return Response.status(Status.NOT_FOUND).build(); return Response.status(Status.NOT_FOUND).build();
} }
} }
private void appendContentType(String path, byte[] head, Response.ResponseBuilder responseBuilder) { private Response createContentHeader(String namespace, String name, String revision, String path, RepositoryService repositoryService, Response.ResponseBuilder responseBuilder) {
try {
appendContentHeader(path, getHead(revision, path, repositoryService), responseBuilder);
} catch (PathNotFoundException e) {
LOG.debug("path '{}' not found in repository {}/{}", path, namespace, name, e);
return Response.status(Status.NOT_FOUND).build();
} catch (IOException e) {
LOG.info("error reading repository resource {} from {}/{}", path, namespace, name, e);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
} catch (RepositoryException e) {
LOG.info("error reading repository resource {} from {}/{}", path, namespace, name, e);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
}
return responseBuilder.build();
}
private void appendContentHeader(String path, byte[] head, Response.ResponseBuilder responseBuilder) {
ContentType contentType = ContentTypes.detect(path, head); ContentType contentType = ContentTypes.detect(path, head);
responseBuilder.header("Content-Type", contentType.getRaw()); responseBuilder.header("Content-Type", contentType.getRaw());
contentType.getLanguage().ifPresent(language -> responseBuilder.header("Language", language)); contentType.getLanguage().ifPresent(language -> responseBuilder.header("Language", language));