#[non_exhaustive]pub struct IoError {
pub kind: ErrorKind,
pub span: Span,
pub path: Option<PathBuf>,
pub additional_context: Option<AdditionalContext>,
pub location: Option<String>,
}
Expand description
Represents an I/O error in the ShellError::Io
variant.
This is the central I/O error for the ShellError::Io
variant.
It represents all I/O errors by encapsulating ErrorKind
, an extension of
std::io::ErrorKind
.
The span
indicates where the error occurred in user-provided code.
If the error is not tied to user-provided code, the location
refers to the precise point in
the Rust code where the error originated.
The optional path
provides the file or directory involved in the error.
If ErrorKind
alone doesn’t provide enough detail, additional context can be added to clarify
the issue.
For handling user input errors (e.g., commands), prefer using new
.
Alternatively, use the factory
method to simplify error creation in repeated
contexts.
For internal errors, use new_internal
to include the location in Rust
code where the error originated.
§Examples
§User Input Error
use std::path::PathBuf;
let path = PathBuf::from("/some/missing/file");
let error = IoError::new(
std::io::ErrorKind::NotFound,
span,
path
);
println!("Error: {:?}", error);
§Internal Error
let error = IoError::new_internal(
std::io::ErrorKind::UnexpectedEof,
"Failed to read data from buffer",
nu_protocol::location!()
);
println!("Error: {:?}", error);
§Using the Factory Method
use std::path::PathBuf;
let path = PathBuf::from("/some/file");
let from_io_error = IoError::factory(span, Some(path.as_path()));
let content = std::fs::read_to_string(&path).map_err(from_io_error)?;
§ShellErrorBridge
The ShellErrorBridge
struct is used to contain a
ShellError
inside a std::io::Error
.
This allows seamless transfer of ShellError
instances where std::io::Error
is expected.
When a ShellError
needs to be packed into an I/O context, use this bridge.
Similarly, when handling an I/O error that is expected to contain a ShellError
,
use the bridge to unpack it.
This approach ensures clarity about where such container transfers occur.
All other I/O errors should be handled using the provided constructors for IoError
.
This way, the code explicitly indicates when and where a ShellError
transfer might happen.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. }
syntax; cannot be matched against without a wildcard ..
; and struct update syntax will not work.kind: ErrorKind
The type of the underlying I/O error.
std::io::ErrorKind
provides detailed context about the type of I/O error that occurred
and is part of std::io::Error
.
If a kind cannot be represented by it, consider adding a new variant to ErrorKind
.
Only in very rare cases should std::io::ErrorKind::Other
be used, make sure you provide
additional_context
to get useful errors in these cases.
span: Span
The source location of the error.
path: Option<PathBuf>
The path related to the I/O error, if applicable.
Many I/O errors involve a file or directory path, but operating system error messages
often don’t include the specific path.
Setting this to Some
allows users to see which path caused the error.
additional_context: Option<AdditionalContext>
Additional details to provide more context about the error.
Only set this field if it adds meaningful context.
If ErrorKind
already contains all the necessary information, leave this as None
.
location: Option<String>
The precise location in the Rust code where the error originated.
This field is particularly useful for debugging errors that stem from the Rust
implementation rather than user-provided Nushell code.
The original Location
is converted to a string to more easily report the error
attributing the location.
This value is only used if span
is Span::unknown()
as most of the time we want to
refer to user code than the Rust code.
Implementations§
Source§impl IoError
impl IoError
Sourcepub fn new(
kind: impl Into<ErrorKind>,
span: Span,
path: impl Into<Option<PathBuf>>,
) -> IoError
pub fn new( kind: impl Into<ErrorKind>, span: Span, path: impl Into<Option<PathBuf>>, ) -> IoError
Creates a new IoError
with the given kind, span, and optional path.
This constructor should be used in all cases where the combination of the error kind, span,
and path provides enough information to describe the error clearly.
For example, errors like “File not found” or “Permission denied” are typically
self-explanatory when paired with the file path and the location in user-provided
Nushell code (span
).
§Constraints
If span
is unknown, use:
new_internal
if no path is available.new_internal_with_path
if a path is available.
Sourcepub fn new_with_additional_context(
kind: impl Into<ErrorKind>,
span: Span,
path: impl Into<Option<PathBuf>>,
additional_context: impl ToString,
) -> IoError
pub fn new_with_additional_context( kind: impl Into<ErrorKind>, span: Span, path: impl Into<Option<PathBuf>>, additional_context: impl ToString, ) -> IoError
Creates a new IoError
with additional context.
Use this constructor when the error kind, span, and path are not sufficient to fully
explain the error, and additional context can provide meaningful details.
Avoid redundant context (e.g., “Permission denied” for an error kind of
ErrorKind::PermissionDenied
).
§Constraints
If span
is unknown, use:
new_internal
if no path is available.new_internal_with_path
if a path is available.
Sourcepub fn new_internal(
kind: impl Into<ErrorKind>,
additional_context: impl ToString,
location: Location,
) -> IoError
pub fn new_internal( kind: impl Into<ErrorKind>, additional_context: impl ToString, location: Location, ) -> IoError
Creates a new IoError
for internal I/O errors without a user-provided span or path.
This constructor is intended for internal errors in the Rust implementation that still need to be reported to the end user. Since these errors are not tied to user-provided Nushell code, they generally have no meaningful span or path.
Instead, these errors provide:
additional_context
: Details about what went wrong internally.location
: The location in the Rust code where the error occurred, allowing us to trace and debug the issue. Use thenu_protocol::location!
macro to generate the location information.
§Examples
use nu_protocol::shell_error::io::IoError;
let error = IoError::new_internal(
std::io::ErrorKind::UnexpectedEof,
"Failed to read from buffer",
nu_protocol::location!(),
);
Sourcepub fn new_internal_with_path(
kind: impl Into<ErrorKind>,
additional_context: impl ToString,
location: Location,
path: PathBuf,
) -> IoError
pub fn new_internal_with_path( kind: impl Into<ErrorKind>, additional_context: impl ToString, location: Location, path: PathBuf, ) -> IoError
Creates a new IoError
for internal I/O errors with a specific path.
This constructor is similar to new_internal
but also includes a
file or directory path relevant to the error.
Use this function in rare cases where an internal error involves a specific path, and the
combination of path and additional context is helpful.
§Examples
use std::path::PathBuf;
use nu_protocol::shell_error::io::IoError;
let error = IoError::new_internal_with_path(
std::io::ErrorKind::NotFound,
"Could not find special file",
nu_protocol::location!(),
PathBuf::from("/some/file"),
);
Sourcepub fn factory<'p, P>(span: Span, path: P) -> impl Fn(Error) + use<'p, P>
pub fn factory<'p, P>(span: Span, path: P) -> impl Fn(Error) + use<'p, P>
Creates a factory closure for constructing IoError
instances from std::io::Error
values.
This method is particularly useful when you need to handle multiple I/O errors which all
take the same span and path.
Instead of calling .map_err(|err| IoError::new(err.kind(), span, path))
every time, you
can create the factory closure once and pass that into .map_err
.
Trait Implementations§
Source§impl Diagnostic for IoError
impl Diagnostic for IoError
Source§fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>>
fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>>
Diagnostic
. Ideally also globally unique, and documented
in the toplevel crate’s documentation for easy searching. Rust path
format (foo::bar::baz
) is recommended, but more classic codes like
E0123
or enums will work just fine.Source§fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>>
fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>>
Diagnostic
. Do you have any
advice for the poor soul who’s just run into this issue?Source§fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + '_>>
fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + '_>>
Diagnostic
’s Diagnostic::source_code
Source§fn diagnostic_source(&self) -> Option<&dyn Diagnostic>
fn diagnostic_source(&self) -> Option<&dyn Diagnostic>
Source§fn source_code(&self) -> Option<&dyn SourceCode>
fn source_code(&self) -> Option<&dyn SourceCode>
Diagnostic
’s Diagnostic::labels
to.Source§fn severity(&self) -> Option<Severity>
fn severity(&self) -> Option<Severity>
ReportHandler
s to change the display format
of this diagnostic. Read moreSource§fn url<'a>(&'a self) -> Option<Box<dyn Display + 'a>>
fn url<'a>(&'a self) -> Option<Box<dyn Display + 'a>>
Diagnostic
.Diagnostic
s.Source§impl Error for IoError
impl Error for IoError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
Source§impl From<IoError> for ShellError
impl From<IoError> for ShellError
Source§fn from(value: IoError) -> ShellError
fn from(value: IoError) -> ShellError
impl StructuralPartialEq for IoError
Auto Trait Implementations§
impl Freeze for IoError
impl RefUnwindSafe for IoError
impl Send for IoError
impl Sync for IoError
impl Unpin for IoError
impl UnwindSafe for IoError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoSpanned for T
impl<T> IntoSpanned for T
Source§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg
or
a color-specific method, such as OwoColorize::green
, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg
or
a color-specific method, such as OwoColorize::on_yellow
, Read more