Improve HTTP 404 handling and logging (#3940)

pull/3945/head
Kai Kreuzer 2023-12-20 12:35:25 +01:00 committed by GitHub
parent e8641efd5b
commit dceec22a35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 9 deletions

View File

@ -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<Exception> {
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);

View File

@ -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);
}
}