Skip to content

Commit

Permalink
Improve HTTP 404 handling and logging (#3940)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikreuzer authored Dec 20, 2023
1 parent e8641ef commit dceec22
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

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;

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
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;
Expand Down Expand Up @@ -485,7 +484,7 @@ public Response postItemCommand(@PathParam("itemname") @Parameter(description =
return Response.status(Status.BAD_REQUEST).build();
}
} else {
throw new WebApplicationException(404);
return getItemNotFoundResponse(itemname);
}
}

Expand Down Expand Up @@ -524,7 +523,7 @@ public Response addMember(@PathParam("itemName") @Parameter(description = "item

return Response.ok(null, MediaType.TEXT_PLAIN).build();
} catch (ItemNotFoundException e) {
return Response.status(Status.NOT_FOUND).build();
return getItemNotFoundResponse(itemName);
}
}

Expand Down Expand Up @@ -563,7 +562,7 @@ public Response removeMember(@PathParam("itemName") @Parameter(description = "it

return Response.ok(null, MediaType.TEXT_PLAIN).build();
} catch (ItemNotFoundException e) {
return Response.status(Status.NOT_FOUND).build();
return getItemNotFoundResponse(itemName);
}
}

Expand Down

0 comments on commit dceec22

Please sign in to comment.