Skip to content

Throw exception on nonzero exit code #5347

Closed

Description

@notramo

Related problem

One of the most powerful features of the Elvish shell is that it throws an exception when a program exits with non-zero.
For those few programs that returns nonzero even in normal operation (e.g. diff), catching exceptions is easier than doing manual error checking every time for every line of code.

Exceptions could be utilized in other parts of the language too (throw command for usage by the programmer, type errors, etc.).

Describe the solution you'd like

Copy the exception feature of the Elvish shell.

Describe alternatives you've considered

No response

Additional context and details

No response

Activity

  1. fdncred commented on Apr 27, 2022

    @fdncred
    Contributor

    The problem with that is that non-zero exit codes from externals are not always errors.

  2. sholderbach commented on Apr 27, 2022

    @sholderbach
    Member

    If you want to read up on how this difficulty due to the messy reality of exit codes pops up (during nushell's development), you can check out JT's blog post on that https://www.jntrnr.com/exit-codes/

  3. added
    category:enhancement
    status:needs-design
    on Apr 28, 2022
  4. notramo commented on Apr 28, 2022

    @notramo
    Author

    @fdncred Please read previous comments before commenting. And please take a look at how Elvish does this before contributing to the discussion. Your comment didn't help anything.

    The problem with that is that non-zero exit codes from externals are not always errors.

    Elvish can handle that too. I am using Elvish as a daily driver, and this functionality is one of the most useful. This is what keeps me from switching to Nushell.

    https://elv.sh/ref/language.html#exception-capture
    https://elv.sh/ref/language.html#try
    Also, exceptions are mentioned in other parts of the documentation, search for "exception" in that page.

  5. flying-sheep commented on May 11, 2022

    @flying-sheep
    Contributor

    As far as I understand, this is supposed to work, see #3858

  6. ClementNerma commented on Feb 13, 2023

    @ClementNerma

    Any news on this? #3858 was closed without any advancement.

  7. sholderbach commented on Feb 13, 2023

    @sholderbach
    Member

    Mhh we seem to properly suppress the non-zero exit code with try. But we do not return some easily digestable error, that can be processed in catch.

    And ; sequences react to the non-zero exit.

    /home/stefan/nushell〉try { diff }; echo test
    diff: missing operand after 'diff'
    diff: Try 'diff --help' for more information.
    test
    /home/stefan/nushell〉diff; echo test
    diff: missing operand after 'diff'
    diff: Try 'diff --help' for more information.
    
  8. added
    A:error-handling
    and removed on Feb 13, 2023
  9. flying-sheep commented on Feb 13, 2023

    @flying-sheep
    Contributor

    Great! This makes sense given these use cases:

    1. I use an external command that happens to write to stderr. I want to see that stream as it appears.
    2. I use an external command that can fail. I want my script to behave sanely and abort unless I use try.

    For the following use case, we have do -c!

    1. I run an external command that only writes to stderr when it fails. I want to propagate that as an error message in case of failure
    〉do -c { diff }
    Error: nu::shell::external_command (link)
    
      × External command failed
       ╭─[entry #11:1:1]
     1 │ do -c { diff }
       ·         ──┬─
       ·           ╰── External command failed
       ╰────
      help: diff: fehlender Operand nach »diff«
            diff: »diff --help« gibt Ihnen mehr Informationen.
    

    And for this last use case, we have do -s { … } | complete:

    1. I run an external command where “exit status ≠ 0” doesn’t mean failure and I want to handle that:
    〉do -s { LANG=C diff /etc/profile.d/jre.sh /etc/profile.d/jre.csh } | complete
    ╭───────────┬──────────────────────────────────────────────────╮
    │ stdout    │ 3,4c3                                            │
    │           │ < append_path '/usr/lib/jvm/default/bin'         │
    │           │ < export PATH                                    │
    │           │ ---                                              │
    │           │ > setenv PATH "${PATH}:/usr/lib/jvm/default/bin" │
    │           │                                                  │
    │ stderr    │                                                  │
    │ exit_code │ 1                                                │
    ╰───────────┴──────────────────────────────────────────────────╯
    

    I’m not sure why do -i { … } | complete swallows the exit code, but this should be good. We just need to have more extensive documentation for 1. all these use cases and 2. the different kinds of errors do can handle, but everything should be good now!

  10. Zoybean commented on Feb 27, 2024

    @Zoybean
    Contributor

    I think it would be nice (though I don't know how the implementation would go) if do -i and do -c would preserve the exit code in some way, or if there was a separate flag that did that. My use case is that I want the numeric exit code from an external command, but don't want to use complete as it prevents streaming the stdout.

  11. flying-sheep commented on Feb 27, 2024

    @flying-sheep
    Contributor
  12. Zoybean commented on Feb 27, 2024

    @Zoybean
    Contributor

    The example given in the book doesn't work in scripts for me - if the code is non-zero, it counts as an error and quits the script:

    > def nonzero [] {
      print "start"
      do {bash -c false}
      print $env.LAST_EXIT_CODE
      print "end"
    }
    > nonzero
    start
    
  13. fdncred commented on Feb 27, 2024

    @fdncred
    Contributor

    if you use complete you can get the exit code. I typically use -i to ignore errors.

    ❯ do -i { bash -c false } | complete
    ╭───────────┬───╮
    │ stdout    │   │
    │ stderr    │   │
    │ exit_code │ 1 │
    ╰───────────┴───╯
    
  14. Zoybean commented on Feb 28, 2024

    @Zoybean
    Contributor

    @fdncred please see my earlier comment:

    I think it would be nice (though I don't know how the implementation would go) if do -i and do -c would preserve the exit code in some way, or if there was a separate flag that did that. My use case is that I want the numeric exit code from an external command, but don't want to use complete as it prevents streaming the stdout.

    tl;dr I need the streaming stdout so I can't use complete, and I need the exit code so I can't use do -i or do -c or try-catch

  15. fdncred commented on Feb 28, 2024

    @fdncred
    Contributor

    I guess we'd have to have a list of exit codes. Not sure I'm a fan of that.

  16. added a commit that references this issue on Sep 7, 2024
    3d008e2
  17. added this to the v0.98.0 milestone on Sep 7, 2024
  18. DoumanAsh commented on Oct 10, 2024

    @DoumanAsh

    How do I disable this behavior?
    It is super annoying in console
    Please do not introduce such bad behavior as default (for some reason it is defaults to true for me despite what PR says)

    To fix this bug:
    DoumanAsh/collectionScripts@82129bf

  19. IanManske commented on Oct 10, 2024

    @IanManske
    Member

    Relevant release notes are here for everyone's future reference: https://www.nushell.sh/blog/2024-09-17-nushell_0_98_0.html#non-zero-exit-codes-are-now-errors-toc

    Basically, if you don't use the default config, you have to add a section to your config.

  20. DoumanAsh commented on Oct 10, 2024

    @DoumanAsh

    Basically, if you don't use the default config, you have to add a section to your config.

    The fact that defaults of uninitialized values is different from default config is already pointing to a bigger problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:error-handlingcategory:enhancementstatus:needs-design

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions