From dceec22a355b2317a7af094e29a96a5f6cf315d8 Mon Sep 17 00:00:00 2001 From: Kai Kreuzer Date: Wed, 20 Dec 2023 12:35:25 +0100 Subject: [PATCH] Improve HTTP 404 handling and logging (#3940) --- .../rest/core/internal/JSONResponseExceptionMapper.java | 9 ++++----- .../core/io/rest/core/internal/item/ItemResource.java | 7 +++---- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/JSONResponseExceptionMapper.java b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/JSONResponseExceptionMapper.java index 8a2dcd4a1..c0a658eff 100644 --- a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/JSONResponseExceptionMapper.java +++ b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/JSONResponseExceptionMapper.java @@ -14,7 +14,7 @@ package org.openhab.core.io.rest.core.internal; import java.io.IOException; -import javax.ws.rs.NotFoundException; +import javax.ws.rs.ClientErrorException; import javax.ws.rs.core.Response; import javax.ws.rs.ext.ExceptionMapper; @@ -51,12 +51,11 @@ public class JSONResponseExceptionMapper implements ExceptionMapper { logger.debug("Failed writing HTTP response, since other side closed the connection", e); // Returning null results in a Response.Status.NO_CONTENT response. return null; - } else if (e instanceof NotFoundException) { + } else if (e instanceof ClientErrorException cee) { // we catch this exception to avoid confusion errors in the log file, since this is not any error situation // see https://github.com/openhab/openhab-distro/issues/1616 - logger.debug("Requested resource not (yet) found", e); - // Returning null results in a Response.Status.NO_CONTENT response. - return Response.status(Response.Status.NOT_FOUND).build(); + logger.debug("Requested resource not (yet) found", cee); + return cee.getResponse(); } else { logger.error("Unexpected exception occurred while processing REST request.", e); return delegate.toResponse(e); diff --git a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/item/ItemResource.java b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/item/ItemResource.java index ccc7936c1..36675bc89 100644 --- a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/item/ItemResource.java +++ b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/item/ItemResource.java @@ -42,7 +42,6 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; -import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.CacheControl; import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; @@ -485,7 +484,7 @@ public class ItemResource implements RESTResource { return Response.status(Status.BAD_REQUEST).build(); } } else { - throw new WebApplicationException(404); + return getItemNotFoundResponse(itemname); } } @@ -524,7 +523,7 @@ public class ItemResource implements RESTResource { return Response.ok(null, MediaType.TEXT_PLAIN).build(); } catch (ItemNotFoundException e) { - return Response.status(Status.NOT_FOUND).build(); + return getItemNotFoundResponse(itemName); } } @@ -563,7 +562,7 @@ public class ItemResource implements RESTResource { return Response.ok(null, MediaType.TEXT_PLAIN).build(); } catch (ItemNotFoundException e) { - return Response.status(Status.NOT_FOUND).build(); + return getItemNotFoundResponse(itemName); } }