Skip to content

http: use the response body for the error message - #18387

Merged
WindSoilder merged 3 commits into
nushell:mainfrom
madjar:http-errors
Jun 13, 2026
Merged

WindSoilder merged 3 commits into
nushell:mainfrom
madjar:http-errors

Conversation

@madjar

@madjar madjar commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

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:
image

After:
image

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

madjar added 2 commits June 10, 2026 14:57
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 WindSoilder left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After introducing this variant, NetworkFailure is useless, you can delete it.

@madjar madjar Jun 12, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment why application/json is special

@WindSoilder WindSoilder left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM

@WindSoilder
WindSoilder merged commit 1a7e728 into nushell:main Jun 13, 2026
17 checks passed
@github-actions github-actions Bot added this to the v0.114.0 milestone Jun 13, 2026
@fdncred fdncred added notes:ready Indicates Ready for Release notes notes:additions Noted in "Additions" section labels Jun 22, 2026
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.
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:additions Noted in "Additions" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

http commands should include the response body in errors

3 participants