Skip to content

Return a JSON body for contents API 404 errors - #1711

Open
Thanatos9404 wants to merge 1 commit into
jupyter-server:mainfrom
Thanatos9404:fix-contents-json-error-body
Open

Thanatos9404 wants to merge 1 commit into
jupyter-server:mainfrom
Thanatos9404:fix-contents-json-error-body

Conversation

@Thanatos9404

@Thanatos9404 Thanatos9404 commented Oct 19, 2026 •

Copy link
Copy Markdown

Fixes #1530

@tsharma-dev mentioned in March that they were exploring a fix, and I could not find a PR since, so I went ahead. Happy to close this if you have work in progress.

ContentsHandler._finish_error wrote the error message as plain text, but APIHandler.finish sets Content-Type: application/json. So contents API 404s (missing file, missing directory, hidden path) claimed to be JSON and broke clients that parse them as JSON.

The body is now {"message": "...", "reason": null}, the same shape APIHandler.write_error uses for a web.HTTPError. The status code and message text are unchanged, and the handler still finishes the response itself as in #1357, so nothing new is logged. I didn't raise web.HTTPError(code, reason=message) as suggested on the issue, because the text would then go into reason, while message, the field JupyterLab shows, would become "Not Found".

Tests: test_get_contents_no_such_file, test_list_nonexistant_dir and test_get_404_hidden now check the Content-Type and that the body is JSON with message and reason. They fail on main with a JSONDecodeError and pass with this change.

ContentsHandler._finish_error wrote the error message as plain text,
while APIHandler.finish always sets Content-Type to application/json.
A GET for a missing or hidden file therefore returned a body that is
labelled as JSON but cannot be parsed as JSON.

Write {"message": ..., "reason": null} instead, which is the shape
APIHandler.write_error produces for an HTTPError. The handler still
sets the status and finishes the response itself instead of raising,
so the quiet logging added in jupyter-server#1357 is unchanged.

The 404 tests for a missing file, a missing directory and hidden paths
now check the Content-Type header and parse the body as JSON.

This fixes jupyter-server#1530.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSON request finishes with non-json response

1 participant