pub struct Error { /* private fields */ }
Expand description
Implementations§
impl Error
Common errors constants for use in std
Source§impl Error
impl Error
1.0.0 · Sourcepub fn new<E>(kind: ErrorKind, error: E) -> Error
pub fn new<E>(kind: ErrorKind, error: E) -> Error
Creates a new I/O error from a known kind of error as well as an arbitrary error payload.
This function is used to generically create I/O errors which do not
originate from the OS itself. The error
argument is an arbitrary
payload which will be contained in this Error
.
Note that this function allocates memory on the heap.
If no extra payload is required, use the From
conversion from
ErrorKind
.
§Examples
use std::io::{Error, ErrorKind};
// errors can be created from strings
let custom_error = Error::new(ErrorKind::Other, "oh no!");
// errors can also be created from other errors
let custom_error2 = Error::new(ErrorKind::Interrupted, custom_error);
// creating an error without payload (and without memory allocation)
let eof_error = Error::from(ErrorKind::UnexpectedEof);
1.74.0 · Sourcepub fn other<E>(error: E) -> Error
pub fn other<E>(error: E) -> Error
Creates a new I/O error from an arbitrary error payload.
This function is used to generically create I/O errors which do not
originate from the OS itself. It is a shortcut for Error::new
with ErrorKind::Other
.
§Examples
use std::io::Error;
// errors can be created from strings
let custom_error = Error::other("oh no!");
// errors can also be created from other errors
let custom_error2 = Error::other(custom_error);
1.0.0 · Sourcepub fn last_os_error() -> Error
pub fn last_os_error() -> Error
Returns an error representing the last OS error which occurred.
This function reads the value of errno
for the target platform (e.g.
GetLastError
on Windows) and will return a corresponding instance of
Error
for the error code.
This should be called immediately after a call to a platform function, otherwise the state of the error value is indeterminate. In particular, other standard library functions may call platform functions that may (or may not) reset the error value even if they succeed.
§Examples
use std::io::Error;
let os_error = Error::last_os_error();
println!("last OS error: {os_error:?}");
1.0.0 · Sourcepub fn from_raw_os_error(code: i32) -> Error
pub fn from_raw_os_error(code: i32) -> Error
Creates a new instance of an Error
from a particular OS error code.
§Examples
On Linux:
use std::io;
let error = io::Error::from_raw_os_error(22);
assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
On Windows:
use std::io;
let error = io::Error::from_raw_os_error(10022);
assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
1.0.0 · Sourcepub fn raw_os_error(&self) -> Option<i32>
pub fn raw_os_error(&self) -> Option<i32>
Returns the OS error that this error represents (if any).
If this Error
was constructed via last_os_error
or
from_raw_os_error
, then this function will return Some
, otherwise
it will return None
.
§Examples
use std::io::{Error, ErrorKind};
fn print_os_error(err: &Error) {
if let Some(raw_os_err) = err.raw_os_error() {
println!("raw OS error: {raw_os_err:?}");
} else {
println!("Not an OS error");
}
}
fn main() {
// Will print "raw OS error: ...".
print_os_error(&Error::last_os_error());
// Will print "Not an OS error".
print_os_error(&Error::new(ErrorKind::Other, "oh no!"));
}
1.3.0 · Sourcepub fn get_ref(&self) -> Option<&(dyn Error + Send + Sync + 'static)>
pub fn get_ref(&self) -> Option<&(dyn Error + Send + Sync + 'static)>
Returns a reference to the inner error wrapped by this error (if any).
If this Error
was constructed via new
then this function will
return Some
, otherwise it will return None
.
§Examples
use std::io::{Error, ErrorKind};
fn print_error(err: &Error) {
if let Some(inner_err) = err.get_ref() {
println!("Inner error: {inner_err:?}");
} else {
println!("No inner error");
}
}
fn main() {
// Will print "No inner error".
print_error(&Error::last_os_error());
// Will print "Inner error: ...".
print_error(&Error::new(ErrorKind::Other, "oh no!"));
}
1.3.0 · Sourcepub fn get_mut(&mut self) -> Option<&mut (dyn Error + Send + Sync + 'static)>
pub fn get_mut(&mut self) -> Option<&mut (dyn Error + Send + Sync + 'static)>
Returns a mutable reference to the inner error wrapped by this error (if any).
If this Error
was constructed via new
then this function will
return Some
, otherwise it will return None
.
§Examples
use std::io::{Error, ErrorKind};
use std::{error, fmt};
use std::fmt::Display;
#[derive(Debug)]
struct MyError {
v: String,
}
impl MyError {
fn new() -> MyError {
MyError {
v: "oh no!".to_string()
}
}
fn change_message(&mut self, new_message: &str) {
self.v = new_message.to_string();
}
}
impl error::Error for MyError {}
impl Display for MyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "MyError: {}", self.v)
}
}
fn change_error(mut err: Error) -> Error {
if let Some(inner_err) = err.get_mut() {
inner_err.downcast_mut::<MyError>().unwrap().change_message("I've been changed!");
}
err
}
fn print_error(err: &Error) {
if let Some(inner_err) = err.get_ref() {
println!("Inner error: {inner_err}");
} else {
println!("No inner error");
}
}
fn main() {
// Will print "No inner error".
print_error(&change_error(Error::last_os_error()));
// Will print "Inner error: ...".
print_error(&change_error(Error::new(ErrorKind::Other, MyError::new())));
}
1.3.0 · Sourcepub fn into_inner(self) -> Option<Box<dyn Error + Send + Sync>>
pub fn into_inner(self) -> Option<Box<dyn Error + Send + Sync>>
Consumes the Error
, returning its inner error (if any).
If this Error
was constructed via new
or other
,
then this function will return Some
,
otherwise it will return None
.
§Examples
use std::io::{Error, ErrorKind};
fn print_error(err: Error) {
if let Some(inner_err) = err.into_inner() {
println!("Inner error: {inner_err}");
} else {
println!("No inner error");
}
}
fn main() {
// Will print "No inner error".
print_error(Error::last_os_error());
// Will print "Inner error: ...".
print_error(Error::new(ErrorKind::Other, "oh no!"));
}
1.79.0 · Sourcepub fn downcast<E>(self) -> Result<E, Error>
pub fn downcast<E>(self) -> Result<E, Error>
Attempts to downcast the custom boxed error to E
.
If this Error
contains a custom boxed error,
then it would attempt downcasting on the boxed error,
otherwise it will return Err
.
If the custom boxed error has the same type as E
, it will return Ok
,
otherwise it will also return Err
.
This method is meant to be a convenience routine for calling
Box<dyn Error + Sync + Send>::downcast
on the custom boxed error, returned by
Error::into_inner
.
§Examples
use std::fmt;
use std::io;
use std::error::Error;
#[derive(Debug)]
enum E {
Io(io::Error),
SomeOtherVariant,
}
impl fmt::Display for E {
// ...
}
impl Error for E {}
impl From<io::Error> for E {
fn from(err: io::Error) -> E {
err.downcast::<E>()
.unwrap_or_else(E::Io)
}
}
impl From<E> for io::Error {
fn from(err: E) -> io::Error {
match err {
E::Io(io_error) => io_error,
e => io::Error::new(io::ErrorKind::Other, e),
}
}
}
let e = E::SomeOtherVariant;
// Convert it to an io::Error
let io_error = io::Error::from(e);
// Cast it back to the original variant
let e = E::from(io_error);
assert!(matches!(e, E::SomeOtherVariant));
let io_error = io::Error::from(io::ErrorKind::AlreadyExists);
// Convert it to E
let e = E::from(io_error);
// Cast it back to the original variant
let io_error = io::Error::from(e);
assert_eq!(io_error.kind(), io::ErrorKind::AlreadyExists);
assert!(io_error.get_ref().is_none());
assert!(io_error.raw_os_error().is_none());
1.0.0 · Sourcepub fn kind(&self) -> ErrorKind
pub fn kind(&self) -> ErrorKind
Returns the corresponding ErrorKind
for this error.
This may be a value set by Rust code constructing custom io::Error
s,
or if this io::Error
was sourced from the operating system,
it will be a value inferred from the system’s error encoding.
See last_os_error
for more details.
§Examples
use std::io::{Error, ErrorKind};
fn print_error(err: Error) {
println!("{:?}", err.kind());
}
fn main() {
// As no error has (visibly) occurred, this may print anything!
// It likely prints a placeholder for unidentified (non-)errors.
print_error(Error::last_os_error());
// Will print "AddrInUse".
print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
}
Trait Implementations§
1.0.0 · Source§impl Error for Error
impl Error for Error
Source§fn description(&self) -> &str
fn description(&self) -> &str
Source§fn cause(&self) -> Option<&dyn Error>
fn cause(&self) -> Option<&dyn Error>
Source§impl<T> From<AsyncFdTryNewError<T>> for Error
impl<T> From<AsyncFdTryNewError<T>> for Error
Source§fn from(value: AsyncFdTryNewError<T>) -> Error
fn from(value: AsyncFdTryNewError<T>) -> Error
Source§impl From<Error> for BatchDownloadError
impl From<Error> for BatchDownloadError
Source§fn from(x: Error) -> BatchDownloadError
fn from(x: Error) -> BatchDownloadError
Source§impl From<Error> for BatchErrorProcessingError
impl From<Error> for BatchErrorProcessingError
Source§fn from(x: Error) -> BatchErrorProcessingError
fn from(x: Error) -> BatchErrorProcessingError
Source§impl From<Error> for BatchInputCreationError
impl From<Error> for BatchInputCreationError
Source§fn from(x: Error) -> BatchInputCreationError
fn from(x: Error) -> BatchInputCreationError
Source§impl From<Error> for BatchMetadataError
impl From<Error> for BatchMetadataError
Source§fn from(x: Error) -> BatchMetadataError
fn from(x: Error) -> BatchMetadataError
Source§impl From<Error> for BatchOutputProcessingError
impl From<Error> for BatchOutputProcessingError
Source§fn from(x: Error) -> BatchOutputProcessingError
fn from(x: Error) -> BatchOutputProcessingError
Source§impl From<Error> for BatchReconciliationError
impl From<Error> for BatchReconciliationError
Source§fn from(x: Error) -> BatchReconciliationError
fn from(x: Error) -> BatchReconciliationError
Source§impl From<Error> for BatchSuccessResponseHandlingError
impl From<Error> for BatchSuccessResponseHandlingError
Source§fn from(x: Error) -> BatchSuccessResponseHandlingError
fn from(x: Error) -> BatchSuccessResponseHandlingError
Source§impl From<Error> for BatchWorkspaceError
impl From<Error> for BatchWorkspaceError
Source§fn from(x: Error) -> BatchWorkspaceError
fn from(x: Error) -> BatchWorkspaceError
Source§impl From<Error> for ContentParseError
impl From<Error> for ContentParseError
Source§fn from(x: Error) -> ContentParseError
fn from(x: Error) -> ContentParseError
Source§impl From<Error> for Error
impl From<Error> for Error
Source§fn from(j: Error) -> Error
fn from(j: Error) -> Error
Convert a serde_json::Error
into an io::Error
.
JSON syntax and data errors are turned into InvalidData
I/O errors.
EOF errors are turned into UnexpectedEof
I/O errors.
use std::io;
enum MyError {
Io(io::Error),
Json(serde_json::Error),
}
impl From<serde_json::Error> for MyError {
fn from(err: serde_json::Error) -> MyError {
use serde_json::error::Category;
match err.classify() {
Category::Io => {
MyError::Io(err.into())
}
Category::Syntax | Category::Data | Category::Eof => {
MyError::Json(err)
}
}
}
}
Source§impl From<Error> for ErrorSavingFailedBatchEntries
impl From<Error> for ErrorSavingFailedBatchEntries
Source§fn from(x: Error) -> ErrorSavingFailedBatchEntries
fn from(x: Error) -> ErrorSavingFailedBatchEntries
Source§impl From<Error> for FileMoveError
impl From<Error> for FileMoveError
Source§fn from(x: Error) -> FileMoveError
fn from(x: Error) -> FileMoveError
Source§impl From<Error> for JsonParseError
impl From<Error> for JsonParseError
Source§fn from(x: Error) -> JsonParseError
fn from(x: Error) -> JsonParseError
Source§impl From<Error> for LanguageModelBatchCreationError
impl From<Error> for LanguageModelBatchCreationError
Source§fn from(x: Error) -> LanguageModelBatchCreationError
fn from(x: Error) -> LanguageModelBatchCreationError
Source§impl From<Error> for LanguageModelBatchWorkflowError
impl From<Error> for LanguageModelBatchWorkflowError
Source§fn from(x: Error) -> LanguageModelBatchWorkflowError
fn from(x: Error) -> LanguageModelBatchWorkflowError
Source§impl From<Error> for MockBatchClientError
impl From<Error> for MockBatchClientError
Source§fn from(x: Error) -> MockBatchClientError
fn from(x: Error) -> MockBatchClientError
Source§impl From<Error> for SaveLoadError
impl From<Error> for SaveLoadError
Source§fn from(x: Error) -> SaveLoadError
fn from(x: Error) -> SaveLoadError
Source§impl From<Error> for TokenExpanderError
impl From<Error> for TokenExpanderError
Source§fn from(x: Error) -> TokenExpanderError
fn from(x: Error) -> TokenExpanderError
Source§impl From<Error> for TokenParseError
impl From<Error> for TokenParseError
Source§fn from(x: Error) -> TokenParseError
fn from(x: Error) -> TokenParseError
Source§impl From<Error> for UuidParseError
impl From<Error> for UuidParseError
Source§fn from(x: Error) -> UuidParseError
fn from(x: Error) -> UuidParseError
1.14.0 · Source§impl From<ErrorKind> for Error
Intended for use for errors not exposed to the user, where allocating onto
the heap (for normal construction via Error::new) is too costly.
impl From<ErrorKind> for Error
Intended for use for errors not exposed to the user, where allocating onto the heap (for normal construction via Error::new) is too costly.
1.0.0 · Source§impl<W> From<IntoInnerError<W>> for Error
impl<W> From<IntoInnerError<W>> for Error
Source§fn from(iie: IntoInnerError<W>) -> Error
fn from(iie: IntoInnerError<W>) -> Error
Source§impl From<PathPersistError> for Error
impl From<PathPersistError> for Error
Source§fn from(error: PathPersistError) -> Error
fn from(error: PathPersistError) -> Error
Source§impl<F> From<PersistError<F>> for Error
impl<F> From<PersistError<F>> for Error
Source§fn from(error: PersistError<F>) -> Error
fn from(error: PersistError<F>) -> Error
Source§impl From<TryGetError> for Error
impl From<TryGetError> for Error
Source§fn from(error: TryGetError) -> Error
fn from(error: TryGetError) -> Error
1.78.0 · Source§impl From<TryReserveError> for Error
impl From<TryReserveError> for Error
Source§fn from(_: TryReserveError) -> Error
fn from(_: TryReserveError) -> Error
Converts TryReserveError
to an error with ErrorKind::OutOfMemory
.
TryReserveError
won’t be available as the error source()
,
but this may change in the future.
Auto Trait Implementations§
impl Freeze for Error
impl !RefUnwindSafe for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl !UnwindSafe for Error
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
§Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute
value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
§Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
Quirk
value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition
value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);