Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Immediately return error if detected as pipeline input or positional argument #14874

Merged
merged 2 commits into from
Jan 30, 2025

Conversation

132ikl
Copy link
Contributor

@132ikl 132ikl commented Jan 20, 2025

Description

This PR returns error values while checking pipeline input types and positional argument types. This should help return non-nested errors earlier and prevent confusing errors.

The positional argument change is directly related to an example given on Discord. Before this PR, this is the error shown:

Error: nu::shell::cant_convert

  × Can't convert to record.
    ╭─[/home/rose/tmp/script.nu:23:5]
 22 │         let entry = $in
 23 │ ╭─▶     {
 24 │ │         name: $entry,
 25 │ │         details: {
 26 │ │           context: $context
 27 │ │         }
 28 │ ├─▶     }
    · ╰──── can't convert error to record
 29 │       }
    ╰────

After this PR, this is the error shown:

Error: nu::shell::eval_block_with_input

  × Eval block failed with pipeline input
    ╭─[/home/rose/tmp/script.nu:23:5]
 22 │         let entry = $in
 23 │ ╭─▶     {
 24 │ │         name: $entry,
 25 │ │         details: {
 26 │ │           context: $context
 27 │ │         }
 28 │ ├─▶     }
    · ╰──── source value
 29 │       }
    ╰────

Error: nu::shell::type_mismatch

  × Type mismatch.
   ╭─[/home/rose/tmp/much.nu:3:38]
 2 │   $in | each { |elem|
 3 │     print $elem.details.context.yaml.0
   ·                                      ┬
   ·                                      ╰── Can't access record values with a row index. Try specifying a column name instead
 4 │   } | each { |elem|
   ╰────

I'm not certain if the pipeline input error check actually can ever be triggered, but it seems to be a good defensive error handling strategy regardless. My addition of the Value::Error case in the first place would suggest it can be, but after looking at it more closely the error that caused me to add the case in the first place was actually unrelated to input typechecking.

Additionally, this PR does not affect the handling of nested errors, so something like:

try { ... } catch {|e| $e | reject raw | to nuon }

works the same before and after this PR.

User-Facing Changes

Errors values detected as arguments to commands or as pipeline input to commands are immediately thrown, rather than passed to the command.

Tests + Formatting

  • 🟢 toolkit fmt
  • 🟢 toolkit clippy
  • 🟢 toolkit test
  • 🟢 toolkit test stdlib

After Submitting

N/A

@ajuckel
Copy link

ajuckel commented Jan 20, 2025

That's a super quick turnaround. Thank you @132ikl. This was prompted by a question I asked in the discord, where I couldn't figure out why a line of code was apparently failing (due to the error message). I tested this change locally, and it's a huge improvement. The error now shows exactly where the root problem is.

Here's a link to a gist I created with a demonstration of the prior problematic error message. Probably not a minimal example, but I'm still learning my way around nu.

@fdncred fdncred added error-handling How errors in externals/nu code are caught or handled programmatically (see also unhelpful-error) pr:errors This PR improves our error messages labels Jan 30, 2025
@fdncred fdncred merged commit 948965d into nushell:main Jan 30, 2025
15 checks passed
@fdncred
Copy link
Collaborator

fdncred commented Jan 30, 2025

Thanks

@github-actions github-actions bot added this to the v0.102.0 milestone Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error-handling How errors in externals/nu code are caught or handled programmatically (see also unhelpful-error) pr:errors This PR improves our error messages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants