Repository navigation
http: use the response body for the error message - #18387
Merged
Merged
Conversation
When a failure occurs with an external API, the body of the response usually contains information about the error. This commit uses that body as the error message when such an error occurs, so the user gets immediate feedback instead of a generic message about getting a 400. This is especially useful during interactive use when experimenting with an API. This behaviour is only enabled when the response is application/json, as a proxy for "is an API, and the body is likely to be relevant". Fixes nushell#15091
WindSoilder
requested changes
Jun 12, 2026
WindSoilder
left a comment
Contributor
There was a problem hiding this comment.
Hi, thanks for your contribution! I left some comments
| /// Check the response body for more details. | ||
| #[error("HTTP Error {code} ({reason}): {url}")] | ||
| #[diagnostic(code(nu::shell::http_error))] | ||
| HttpError { |
Contributor
There was a problem hiding this comment.
After introducing this variant, NetworkFailure is useless, you can delete it.
Contributor
Author
There was a problem hiding this comment.
It is still used on ConnectionFailure, other unhandled error in handling of the http request, and when the redirect mode is set to error. I'm not sure how to map these cases to HttpError because they don't have a code (or a reason).
Contributor
There was a problem hiding this comment.
OK, thank you for clarify
| span, | ||
| }, | ||
| fn handle_status_error(span: Span, requested_url: &str, response: &mut Response) -> ShellError { | ||
| let msg = if response.header("content-type") == Some("application/json") { |
Contributor
There was a problem hiding this comment.
Please add a comment why application/json is special
fdncred
pushed a commit
that referenced
this pull request
Nov 16, 2026
<!-- Thanks for contributing to Nushell! Before submitting, please read the contributing guide: https://github.com/nushell/nushell/blob/main/CONTRIBUTING.md This template helps reviewers understand your changes and allows us to generate high-quality release notes. --> ## Description This is a follow up to #18387, which provided the full body on failed http call for json responses. The content-type check was naive and would not handle the case of the header having a charset, like in "application/json; charset=utf-8". This PR fixes the feature to work for any json body. <!-- Explain what this PR does and why. This section is intentionally flexible: - Describe the problem - Explain your approach - Include technical details if relevant Good examples: - "In this PR, I fixed..." - "In this PR, I added support for..." - "This change improves X by..." Write as much or as little as needed for reviewers to understand your changes. --> ## User-facing changes (Release notes) - Fixed an issue where some http errors with json response bodies would not be displayed to the user. <!-- This section is used (mostly as-is) for https://www.nushell.sh/blog/ Describe how Nushell behavior changes from a user's perspective. Do NOT describe internal Rust changes here. Write in a release note style, for example: - "Added support for..." - "Fixed an issue where..." - "Nushell now supports..." - "Improved performance of..." If your changes do NOT affect users (internal refactors, cleanup, etc.), just write: - "n/a" - "nan" - or similar This tells us the change should not appear in the changelog. Tips: - Focus on observable behavior - Include examples if helpful - Keep it concise You can: - Write a short paragraph (will appear as a bullet point), OR - Use headings (###) if your change needs more structure Avoid writing things like: - "In this PR, I refactored..." - "This updates internal code..." You may leave this blank until the PR is ready. --> ## Additional notes No related ticket, I went straight for the fix. <!-- Optional. Examples: - fixes #123 - closes #456 - related #789 Anything else reviewers should know. Remove this section if not needed. -->
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.
Description
When a failure occurs with an external API, the body of the response usually contains information about the error.
This commit uses that body as the error message when such an error occurs, so the user gets immediate feedback instead of a generic message about getting a 400. This is especially useful during interactive use when experimenting with an API.
This behaviour is only enabled when the response is application/json, as a proxy for "is an API, and the body is likely to be relevant".
Before:

After:

User-facing changes (Release notes)
The http commands now use the response body as error message when getting an error response.
Additional notes
Fixes #15091