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

Avoid crashing on error sometimes: more control over the failure mechanism #7039

Open
Pascalio opened this issue Nov 6, 2022 · 1 comment
Labels
enhancement New feature or request error-handling How errors in externals/nu code are caught or handled programmatically (see also unhelpful-error) semantics Places where we should define/clarify nushell's semantics

Comments

@Pascalio
Copy link

Pascalio commented Nov 6, 2022

Related problem

New to nu (sorry^^) but as far as I can gather, currently, as soon as there's an error, the whole nu script will terminate with the error message. While the message is quite detailed and useful, this might not be enough or simply not be desirable. My current case:
open corrupt_file.json
echo "In case of corrupt file, call the Doctor.
The last informative message can never be printed because the script will have crashed before.
Alternatively, you may also want to take some default actions if the json file seems corrupt, without terminating the script, say open backup.json...

Describe the solution you'd like

Without allowing the risk of leaving corrupt data in our pipelines, there may be options, like in Rust, to continue the operations or to terminate more gracefully. (Please forgive me if there are already, really haven't found them yet.)
Suggestions:

Error handler parameter

Certain commands could take some --error_handler <Block> that gets executed after the error message is printed.
The error_handler could have a default implementation for all types, which results in the current behaviour. So if no user-defined implementation shadows it, things would stay the same.
But if the user needs to, the error_handler can be re-implemented; it could have automatic access to error structure as well.

Return a type Result instead of terminating

The hardcore Rustacean's pick... Could look like this:
open data.json | ok_or $error_handler | get something if the error is to be dealt with; or
open data.json | unwrap | get someting if we don't mind crashing.

It is worth noting that, currently, nu terminates indifferently on 2 very different kinds of problems:

  1. an error in the script
  2. an error from the outside
    While a coding error should probably cause immediate termination, data from the outside can't be relied on: it should be dealt with differently than the usual internal errors. So maybe we'd just need an IOResult, whereas the rest could stay as it is.

Describe alternatives you've considered

To avoid crashing on error, open --raw corrupt_file.json works, but then you're basically left on your own with structured data to parse and you can't use the builtins as they would crash on error too.

Additional context and details

No response

@Pascalio Pascalio added the enhancement New feature or request label Nov 6, 2022
@sholderbach sholderbach added the semantics Places where we should define/clarify nushell's semantics label Nov 8, 2022
@Pascalio
Copy link
Author

So, what do you think about this somewhat burning issue? Or is there any suitable workaround?

@sholderbach sholderbach added the error-handling How errors in externals/nu code are caught or handled programmatically (see also unhelpful-error) label Oct 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request error-handling How errors in externals/nu code are caught or handled programmatically (see also unhelpful-error) semantics Places where we should define/clarify nushell's semantics
Projects
None yet
Development

No branches or pull requests

2 participants