Repository navigation
Return a JSON body for contents API 404 errors - #1711
Open
Thanatos9404 wants to merge 1 commit into
Open
Thanatos9404 wants to merge 1 commit into
Thanatos9404 wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_errorwrote the error message as plain text, butAPIHandler.finishsetsContent-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 shapeAPIHandler.write_erroruses for aweb.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 raiseweb.HTTPError(code, reason=message)as suggested on the issue, because the text would then go intoreason, whilemessage, the field JupyterLab shows, would become "Not Found".Tests:
test_get_contents_no_such_file,test_list_nonexistant_dirandtest_get_404_hiddennow check theContent-Typeand that the body is JSON withmessageandreason. They fail on main with aJSONDecodeErrorand pass with this change.