nushell/crates/nu-protocol/src/errors/labeled_error.rs
132ikl 8b80ceac32
Add From<IoError> for LabeledError (#15327)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Adds an `impl From<IoError> for LabeledError`, similar to the existing
`From<ShellError>` implementation. Helpful for plugins.

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
N/A 
# 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 toolkit.nu; toolkit test stdlib"` 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
> ```
-->
N/A
# 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.
-->
N/A
2025-03-20 11:42:31 -04:00

256 lines
8.0 KiB
Rust

use super::{shell_error::io::IoError, ShellError};
use crate::Span;
use miette::Diagnostic;
use serde::{Deserialize, Serialize};
use std::fmt;
/// A very generic type of error used for interfacing with external code, such as scripts and
/// plugins.
///
/// This generally covers most of the interface of [`miette::Diagnostic`], but with types that are
/// well-defined for our protocol.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LabeledError {
/// The main message for the error.
pub msg: String,
/// Labeled spans attached to the error, demonstrating to the user where the problem is.
#[serde(default)]
pub labels: Box<Vec<ErrorLabel>>,
/// A unique machine- and search-friendly error code to associate to the error. (e.g.
/// `nu::shell::missing_config_value`)
#[serde(default)]
pub code: Option<String>,
/// A link to documentation about the error, used in conjunction with `code`
#[serde(default)]
pub url: Option<String>,
/// Additional help for the error, usually a hint about what the user might try
#[serde(default)]
pub help: Option<String>,
/// Errors that are related to or caused this error
#[serde(default)]
pub inner: Box<Vec<LabeledError>>,
}
impl LabeledError {
/// Create a new plain [`LabeledError`] with the given message.
///
/// This is usually used builder-style with methods like [`.with_label()`](Self::with_label) to
/// build an error.
///
/// # Example
///
/// ```rust
/// # use nu_protocol::LabeledError;
/// let error = LabeledError::new("Something bad happened");
/// assert_eq!("Something bad happened", error.to_string());
/// ```
pub fn new(msg: impl Into<String>) -> LabeledError {
LabeledError {
msg: msg.into(),
labels: Box::new(vec![]),
code: None,
url: None,
help: None,
inner: Box::new(vec![]),
}
}
/// Add a labeled span to the error to demonstrate to the user where the problem is.
///
/// # Example
///
/// ```rust
/// # use nu_protocol::{LabeledError, Span};
/// # let span = Span::test_data();
/// let error = LabeledError::new("An error")
/// .with_label("happened here", span);
/// assert_eq!("happened here", &error.labels[0].text);
/// assert_eq!(span, error.labels[0].span);
/// ```
pub fn with_label(mut self, text: impl Into<String>, span: Span) -> Self {
self.labels.push(ErrorLabel {
text: text.into(),
span,
});
self
}
/// Add a unique machine- and search-friendly error code to associate to the error. (e.g.
/// `nu::shell::missing_config_value`)
///
/// # Example
///
/// ```rust
/// # use nu_protocol::LabeledError;
/// let error = LabeledError::new("An error")
/// .with_code("my_product::error");
/// assert_eq!(Some("my_product::error"), error.code.as_deref());
/// ```
pub fn with_code(mut self, code: impl Into<String>) -> Self {
self.code = Some(code.into());
self
}
/// Add a link to documentation about the error, used in conjunction with `code`.
///
/// # Example
///
/// ```rust
/// # use nu_protocol::LabeledError;
/// let error = LabeledError::new("An error")
/// .with_url("https://example.org/");
/// assert_eq!(Some("https://example.org/"), error.url.as_deref());
/// ```
pub fn with_url(mut self, url: impl Into<String>) -> Self {
self.url = Some(url.into());
self
}
/// Add additional help for the error, usually a hint about what the user might try.
///
/// # Example
///
/// ```rust
/// # use nu_protocol::LabeledError;
/// let error = LabeledError::new("An error")
/// .with_help("did you try turning it off and back on again?");
/// assert_eq!(Some("did you try turning it off and back on again?"), error.help.as_deref());
/// ```
pub fn with_help(mut self, help: impl Into<String>) -> Self {
self.help = Some(help.into());
self
}
/// Add an error that is related to or caused this error.
///
/// # Example
///
/// ```rust
/// # use nu_protocol::LabeledError;
/// let error = LabeledError::new("An error")
/// .with_inner(LabeledError::new("out of coolant"));
/// assert_eq!(LabeledError::new("out of coolant"), error.inner[0]);
/// ```
pub fn with_inner(mut self, inner: impl Into<LabeledError>) -> Self {
self.inner.push(inner.into());
self
}
/// Create a [`LabeledError`] from a type that implements [`miette::Diagnostic`].
///
/// # Example
///
/// [`ShellError`] implements `miette::Diagnostic`:
///
/// ```rust
/// # use nu_protocol::{ShellError, LabeledError, shell_error::io::IoError, Span};
/// #
/// let error = LabeledError::from_diagnostic(
/// &ShellError::Io(IoError::new_with_additional_context(
/// std::io::ErrorKind::Other,
/// Span::test_data(),
/// None,
/// "some error"
/// ))
/// );
/// assert!(error.to_string().contains("I/O error"));
/// ```
pub fn from_diagnostic(diag: &(impl miette::Diagnostic + ?Sized)) -> LabeledError {
LabeledError {
msg: diag.to_string(),
labels: diag
.labels()
.into_iter()
.flatten()
.map(|label| ErrorLabel {
text: label.label().unwrap_or("").into(),
span: Span::new(label.offset(), label.offset() + label.len()),
})
.collect::<Vec<_>>()
.into(),
code: diag.code().map(|s| s.to_string()),
url: diag.url().map(|s| s.to_string()),
help: diag.help().map(|s| s.to_string()),
inner: diag
.related()
.into_iter()
.flatten()
.map(Self::from_diagnostic)
.collect::<Vec<_>>()
.into(),
}
}
}
/// A labeled span within a [`LabeledError`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ErrorLabel {
/// Text to show together with the span
pub text: String,
/// Span pointing at where the text references in the source
pub span: Span,
}
impl fmt::Display for LabeledError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.msg)
}
}
impl std::error::Error for LabeledError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.inner.first().map(|r| r as _)
}
}
impl Diagnostic for LabeledError {
fn code<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
self.code.as_ref().map(Box::new).map(|b| b as _)
}
fn severity(&self) -> Option<miette::Severity> {
None
}
fn help<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
self.help.as_ref().map(Box::new).map(|b| b as _)
}
fn url<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
self.url.as_ref().map(Box::new).map(|b| b as _)
}
fn source_code(&self) -> Option<&dyn miette::SourceCode> {
None
}
fn labels(&self) -> Option<Box<dyn Iterator<Item = miette::LabeledSpan> + '_>> {
Some(Box::new(self.labels.iter().map(|label| {
miette::LabeledSpan::new_with_span(
Some(label.text.clone()).filter(|s| !s.is_empty()),
label.span,
)
})))
}
fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
Some(Box::new(self.inner.iter().map(|r| r as _)))
}
fn diagnostic_source(&self) -> Option<&dyn Diagnostic> {
None
}
}
impl From<ShellError> for LabeledError {
fn from(err: ShellError) -> Self {
LabeledError::from_diagnostic(&err)
}
}
impl From<IoError> for LabeledError {
fn from(err: IoError) -> Self {
LabeledError::from_diagnostic(&err)
}
}