Skip to content

Inconsistent error handling #10624

@NfNitLoop

Description

@NfNitLoop

Describe the bug

I find it a bit confusing how/when errors are handled by Nushell. It seems maybe my confusion was due to a bug?

How to reproduce

> # 1: Define some function that might fail partway through:
> def foo [] { echo start; ^false; echo end }

> # 2: Default behavior is to stop execution. But not very loudly. Maybe "begin" looks like good output to me?
> foo  
begin

> # 3: I can force a more "loud" error with `do -c`:
> do -c { foo } 
begin
Error: nu::shell::external_command

  × External command failed
   ╭─[entry #66:1:1]
 1  def foo [] { echo begin; ^false; echo end }
   ·                           ──┬──
   ·                             ╰── External command failed
   ╰────
  help:

> # 4: When used in a pipe, suddenly the behavior changes. No short-circuiting OR loud failure.
> foo | $"result: ($in)"  # no early exit or loud error.
result: end

> # 5: Even if I try to m force the loud failure, pipes seem to override it:
> do -c { foo } | $"result: ($in)"  # ... even if we try to force it.
result: end

Expected behavior

  • My ideal behavior would be for foo to fail with the "External command failed" error in all cases without me needing to do anything special. Silent failures are a bit of a pet peeve of mine. But I think changing that behavior is a ticket all by itself, so I'm happy to set that aside for now. (But if you agree, can we open that ticket? 😊)

  • Probably need some more detailed docs on how errors are supposed to be handled so when some hapless newb like me comes across it they can figure it out. 😅 The Book has a Creating your own errors section, but doesn't mention how errors are handled by default. The do for core page documents some error handling flags, but I really can't tell from them what the behavior should be w.r.t. my foo function, or what the default behavior is. For the record, current behavior is:

~> do -i { foo }
begin
~> do -s { foo }
begin
~> do -p { foo }
begin
~> do { foo }
begin
~> do -c { foo }
begin
Error: nu::shell::external_command [etc.]
  • And once the desired behavior is documented, the remaining bug is making things work that way.

Screenshots

No response

Configuration

key value
version 0.85.0
branch
commit_hash
build_os macos-aarch64
build_target aarch64-apple-darwin
rust_version rustc 1.72.0 (5680fa18f 2023-08-23) (Homebrew)
cargo_version cargo 1.72.0 (26bba4830 2023-08-26)
build_time 2023-09-19 18:42:47 +00:00
build_rust_channel release
allocator mimalloc
features dataframe, default, sqlite, trash, which, zip
installed_plugins

Additional context

❤️ Nushell!

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:error-handlingHow errors in externals/nu code are caught or handled programmatically (see also unhelpful-error)A:external-commandsIssues related to external commandsstatus:needs-triageAn issue that hasn't had any proper look

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions