Skip to content

Better generic errors for plugins (and perhaps scripts) - #12236

Merged
sholderbach merged 5 commits into
nushell:mainfrom
devyn:better-plugin-errors
Mar 21, 2024
Merged

sholderbach merged 5 commits into
nushell:mainfrom
devyn:better-plugin-errors

Conversation

@devyn

@devyn devyn commented Mar 19, 2024

Copy link
Copy Markdown
Contributor

Description

This makes LabeledError much more capable of representing close to
everything a miette::Diagnostic can, including ShellError, and
allows plugins to generate multiple error spans, codes, help, etc.

LabeledError is now embeddable within ShellError as a transparent
variant.

This could also be used to improve error make and try/catch to
reflect LabeledError exactly in the future.

Also cleaned up some errors in existing plugins.

User-Facing Changes

Breaking change for plugins. Nicer errors for users.

Tests + Formatting

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

After Submitting

  • Release notes - mention breaking change
  • Update plugin examples in the book

@devyn
devyn force-pushed the better-plugin-errors branch from 4208fd1 to 8594967 Compare March 19, 2024 13:03

@sholderbach sholderbach left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for making custom errors more approachable.

Generally I think we should invest some time in getting our ontology of errors right (e.g. #10698), so you can match on them in the future, instead of letting everyone roll their generic error by default. But that is work for the pre-stabilization future.

/// This is a generic error type used for user and plugin-generated errors.
#[error(transparent)]
#[diagnostic(transparent)]
LabeledError(#[from] super::LabeledError),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Might be worth boxing: currently ShellError takes 120 bytes, LabeledError takes 144 bytes alone, so this blows it up to 152 bytes (and to the next cache line)

@devyn

devyn commented Mar 19, 2024

Copy link
Copy Markdown
Contributor Author

@sholderbach

I completely agree that everything shipped with Nushell should use ShellErrors, and preferably not create generic ones unless they're very specific, but the point of this is to reduce the plugin protocol surface and offer more functionality to error make.

I don't think every alternate language plugin protocol library should have to keep track of every variant of ShellError - we should really just convert them automatically to some kind of generic error container that doesn't need to change much. On top of that, I think it would be useful for plugin authors to be able to create their own error hierarchies with miette or whatever else they use and not be restricted to what's in GenericError. Some plugins might get very complex and need to display very rich errors.

Then of course there's also script authors - I believe they should also be able to generate rich errors, but I don't think it makes sense to make them follow ShellError.

Good catch on the size - I will box it.

@sholderbach

Copy link
Copy Markdown
Member

Agreed for the plugins now and that we should be able to make free form errors.

Nevertheless I think we should nudge ourselves internally and then our users to use similar patterns/attributes for the errors so you can react to similar/different errors.

@devyn

devyn commented Mar 19, 2024

Copy link
Copy Markdown
Contributor Author

Agreed for the plugins now and that we should be able to make free form errors.

Nevertheless I think we should nudge ourselves internally and then our users to use similar patterns/attributes for the errors so you can react to similar/different errors.

I think that's sensible. From userland and plugin-land I think you should be able to match on the miette code, which is one of my motivations for exposing that to plugins and scripts.

devyn added 2 commits March 20, 2024 22:10
This makes `LabeledError` much more capable of representing close to
everything a `miette::Diagnostic` can, including `ShellError`, and
allows plugins to generate multiple error spans, codes, help, etc.

`LabeledError` is now embeddable within `ShellError` as a transparent
variant.

This could also be used to improve `error make` and `try/catch` to
reflect `LabeledError` exactly in the future.
@devyn
devyn force-pushed the better-plugin-errors branch from 63e2c23 to 89e27b1 Compare March 21, 2024 05:10

@sholderbach sholderbach left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@sholderbach
sholderbach merged commit efe25e3 into nushell:main Mar 21, 2024
@fdncred

fdncred commented Mar 21, 2024 •

Copy link
Copy Markdown
Contributor

@devyn I quickly confused myself because it seems that msg and label are swapped between the new and old style errors. What am I missing?

            None => Err(LabeledError {
                label: "No config sent".into(),
                msg: "Configuration for this plugin was not found in `$env.config.plugins.example`"
                    .into(),
                span: Some(call.head),
            }),

Below it looks like "No config sent" is the old label, when comparing it to the above, but then you can the old msg is used with with_label. Were these just wrong to begin with or what? Just wondering if our error messages show up the same or these things are swapped in the error messages too.

            None => Err(LabeledError::new("No config sent").with_label(
                "configuration for this plugin was not found in `$env.config.plugins.example`",
                call.head,
            )),

@hustcer hustcer added this to the v0.92.0 milestone Mar 21, 2024
@fdncred fdncred added api-change This PR should be mentioned in #api-updates channel on Discord deprecated:pr-plugins Deprecated: use the A:plugins label instead labels Mar 21, 2024
@devyn

devyn commented Mar 21, 2024

Copy link
Copy Markdown
Contributor Author

@fdncred

@devyn I quickly confused myself because it seems that msg and label are swapped between the new and old style errors. What am I missing?

It is kind of the opposite, but that's intentional. I always felt that it was LabeledError that was backwards compared to everything else. Miette calls them labeled spans and uses #[label], and then GenericError calls the main message error and has msg and span for the label. I just thought it was more confusing the way it was. I remember not being able to remember which was which when writing the error structs out before. This way feels easier to remember for me - you have an error you're adding label(s) to.

@fdncred

fdncred commented Mar 21, 2024

Copy link
Copy Markdown
Contributor

I've always been confused about what shows up where in errors. I'm fine with how you've changed it, I just want to be consistent so it's intuitive for everyone. I wouldn't want to have one type of error definition created one way and another type created the opposite way. Does that mean we need to modify ShellError? not sure really but if so, that would probably touch a ton of files.

@devyn

devyn commented Mar 22, 2024

Copy link
Copy Markdown
Contributor Author

I think what's in ShellError is not inconsistent with this - conceptually, GenericError has an error message, and then the msg and span are kind of grouped together (msg only shows if span is set). label isn't mentioned.

You can think of this as being like

LabeledError::new(error)
    .with_label(msg, span)
    .with_help(help)
    .with_inner(inner...)

in terms of equivalence to ShellError::GenericError.

fdncred added a commit that referenced this pull request Apr 28, 2024
# Description

This PR fixes a problem introduced with PR
#12236. That PR accidentally
stopped `--as-table` from working.

Closes #12689

It works again.

![image](https://github.com/nushell/nushell/assets/343840/b517507f-6b92-4e39-a389-5c69907d77c0)

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
maxim-uvarov pushed a commit to maxim-uvarov/nushell that referenced this pull request May 1, 2024
# Description

This PR fixes a problem introduced with PR
nushell#12236. That PR accidentally
stopped `--as-table` from working.

Closes nushell#12689

It works again.

![image](https://github.com/nushell/nushell/assets/343840/b517507f-6b92-4e39-a389-5c69907d77c0)

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api-change This PR should be mentioned in #api-updates channel on Discord deprecated:pr-plugins Deprecated: use the A:plugins label instead

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants