Repository navigation
Better generic errors for plugins (and perhaps scripts) - #12236
Conversation
4208fd1 to
8594967
Compare
sholderbach
left a comment
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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)
|
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 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 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 Good catch on the size - I will box it. |
|
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. |
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.
63e2c23 to
89e27b1
Compare
|
@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,
)), |
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 |
|
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. |
|
I think what's in 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 |
# Description This PR fixes a problem introduced with PR #12236. That PR accidentally stopped `--as-table` from working. Closes #12689 It works again.  # 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. -->
# 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.  # 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. -->
Description
This makes
LabeledErrormuch more capable of representing close toeverything a
miette::Diagnosticcan, includingShellError, andallows plugins to generate multiple error spans, codes, help, etc.
LabeledErroris now embeddable withinShellErroras a transparentvariant.
This could also be used to improve
error makeandtry/catchtoreflect
LabeledErrorexactly 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 fmttoolkit clippytoolkit testtoolkit test stdlibAfter Submitting