mirror of
https://github.com/eyre-rs/eyre.git
synced 2025-09-29 22:11:55 +00:00
Rename Exception to Error
This commit is contained in:
parent
7afc190810
commit
5535ffc436
@ -1,6 +1,6 @@
|
||||
use std::error::Error as StdError;
|
||||
|
||||
use crate::Exception;
|
||||
use crate::Error;
|
||||
|
||||
/// View type as an error object.
|
||||
pub trait AsError {
|
||||
@ -20,7 +20,7 @@ impl AsError for dyn StdError + Send + Sync + 'static {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsError for Exception {
|
||||
impl AsError for Error {
|
||||
fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static) {
|
||||
&**self
|
||||
}
|
||||
|
@ -2,32 +2,32 @@ use std::backtrace::Backtrace;
|
||||
use std::error::Error as StdError;
|
||||
use std::fmt::{self, Debug, Display};
|
||||
|
||||
use crate::Exception;
|
||||
use crate::Error;
|
||||
|
||||
/// Provides the `context` method for `Result`.
|
||||
pub trait Context<T, E> {
|
||||
/// Wrap the error value with additional context.
|
||||
fn context<C: Display + Send + Sync + 'static>(self, context: C) -> Result<T, Exception>;
|
||||
fn context<C: Display + Send + Sync + 'static>(self, context: C) -> Result<T, Error>;
|
||||
|
||||
/// Wrap the error value with additional context lazily.
|
||||
fn with_context<C, F>(self, f: F) -> Result<T, Exception>
|
||||
fn with_context<C, F>(self, f: F) -> Result<T, Error>
|
||||
where
|
||||
C: Display + Send + Sync + 'static,
|
||||
F: FnOnce(&E) -> C;
|
||||
}
|
||||
|
||||
impl<T, E: StdError + Send + Sync + 'static> Context<T, E> for Result<T, E> {
|
||||
fn context<C: Display + Send + Sync + 'static>(self, context: C) -> Result<T, Exception> {
|
||||
self.map_err(|error| Exception::from(ContextError { error, context }))
|
||||
fn context<C: Display + Send + Sync + 'static>(self, context: C) -> Result<T, Error> {
|
||||
self.map_err(|error| Error::from(ContextError { error, context }))
|
||||
}
|
||||
|
||||
fn with_context<C, F>(self, f: F) -> Result<T, Exception>
|
||||
fn with_context<C, F>(self, f: F) -> Result<T, Error>
|
||||
where
|
||||
C: Display + Send + Sync + 'static,
|
||||
F: FnOnce(&E) -> C,
|
||||
{
|
||||
self.map_err(|error| {
|
||||
Exception::from(ContextError {
|
||||
Error::from(ContextError {
|
||||
context: f(&error),
|
||||
error,
|
||||
})
|
||||
@ -35,18 +35,18 @@ impl<T, E: StdError + Send + Sync + 'static> Context<T, E> for Result<T, E> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Context<T, Exception> for Result<T, Exception> {
|
||||
fn context<C: Display + Send + Sync + 'static>(self, context: C) -> Result<T, Exception> {
|
||||
self.map_err(|error| Exception::from(ContextError { error, context }))
|
||||
impl<T> Context<T, Error> for Result<T, Error> {
|
||||
fn context<C: Display + Send + Sync + 'static>(self, context: C) -> Result<T, Error> {
|
||||
self.map_err(|error| Error::from(ContextError { error, context }))
|
||||
}
|
||||
|
||||
fn with_context<C, F>(self, f: F) -> Result<T, Exception>
|
||||
fn with_context<C, F>(self, f: F) -> Result<T, Error>
|
||||
where
|
||||
C: Display + Send + Sync + 'static,
|
||||
F: FnOnce(&Exception) -> C,
|
||||
F: FnOnce(&Error) -> C,
|
||||
{
|
||||
self.map_err(|error| {
|
||||
Exception::from(ContextError {
|
||||
Error::from(ContextError {
|
||||
context: f(&error),
|
||||
error,
|
||||
})
|
||||
@ -85,7 +85,7 @@ impl<E: StdError + 'static, C: Display> StdError for ContextError<E, C> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: Display> StdError for ContextError<Exception, C> {
|
||||
impl<C: Display> StdError for ContextError<Error, C> {
|
||||
fn backtrace(&self) -> Option<&Backtrace> {
|
||||
Some(self.error.backtrace())
|
||||
}
|
||||
|
62
src/error.rs
62
src/error.rs
@ -6,42 +6,42 @@ use std::mem;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::ptr;
|
||||
|
||||
/// The `Exception type, a wrapper around a dynamic error type.
|
||||
/// The `Error` type, a wrapper around a dynamic error type.
|
||||
///
|
||||
/// `Exception` functions a lot like `Box<dyn Error>`, with these differences:
|
||||
/// `Error` functions a lot like `Box<dyn std::error::Error>`, with these differences:
|
||||
///
|
||||
/// - `Exception` requires that the error is `Send`, `Sync`, and `'static`
|
||||
/// - `Exception` guarantees that a backtrace will exist, even if the error type
|
||||
/// - `Error` requires that the error is `Send`, `Sync`, and `'static`
|
||||
/// - `Error` guarantees that a backtrace will exist, even if the error type
|
||||
/// did not provide one
|
||||
/// - `Exception` is represented as a narrow pointer - exactly one word in size,
|
||||
/// - `Error` is represented as a narrow pointer - exactly one word in size,
|
||||
/// instead of two.
|
||||
pub struct Exception {
|
||||
pub struct Error {
|
||||
inner: Box<InnerException<()>>,
|
||||
}
|
||||
|
||||
impl Exception {
|
||||
impl Error {
|
||||
/// Create a new exception from any error type.
|
||||
///
|
||||
/// The error type must be threadsafe and `'static`, so that the `Exception` will be as well.
|
||||
/// The error type must be threadsafe and `'static`, so that the `Error` will be as well.
|
||||
///
|
||||
/// If the error type does not provide a backtrace, a backtrace will be created here to ensure
|
||||
/// that a backtrace exists.
|
||||
pub fn new<E>(error: E) -> Exception
|
||||
pub fn new<E>(error: E) -> Error
|
||||
where
|
||||
E: StdError + Send + Sync + 'static,
|
||||
{
|
||||
Exception::construct(error, TypeId::of::<E>())
|
||||
Error::construct(error, TypeId::of::<E>())
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn new_adhoc<M>(message: M) -> Exception
|
||||
pub fn new_adhoc<M>(message: M) -> Error
|
||||
where
|
||||
M: Display + Debug + Send + Sync + 'static,
|
||||
{
|
||||
Exception::construct(MessageError(message), TypeId::of::<M>())
|
||||
Error::construct(MessageError(message), TypeId::of::<M>())
|
||||
}
|
||||
|
||||
fn construct<E>(error: E, type_id: TypeId) -> Exception
|
||||
fn construct<E>(error: E, type_id: TypeId) -> Error
|
||||
where
|
||||
E: StdError + Send + Sync + 'static,
|
||||
{
|
||||
@ -58,7 +58,7 @@ impl Exception {
|
||||
backtrace,
|
||||
error,
|
||||
};
|
||||
Exception {
|
||||
Error {
|
||||
inner: mem::transmute(Box::new(inner)),
|
||||
}
|
||||
}
|
||||
@ -74,7 +74,7 @@ impl Exception {
|
||||
&mut **self
|
||||
}
|
||||
|
||||
/// Get the backtrace for this Exception.
|
||||
/// Get the backtrace for this Error.
|
||||
pub fn backtrace(&self) -> &Backtrace {
|
||||
// NB: this unwrap can only fail if the underlying error's backtrace method is
|
||||
// nondeterministic, which would only happen in maliciously constructed code
|
||||
@ -85,7 +85,7 @@ impl Exception {
|
||||
.expect("exception backtrace capture failed")
|
||||
}
|
||||
|
||||
/// An iterator of errors contained by this Exception.
|
||||
/// An iterator of errors contained by this Error.
|
||||
///
|
||||
/// This iterator will visit every error in the "cause chain" of this exception, beginning with
|
||||
/// the error that this exception was created from.
|
||||
@ -101,7 +101,7 @@ impl Exception {
|
||||
}
|
||||
|
||||
/// Attempt to downcast the exception to a concrete type.
|
||||
pub fn downcast<E: Display + Debug + Send + Sync + 'static>(self) -> Result<E, Exception> {
|
||||
pub fn downcast<E: Display + Debug + Send + Sync + 'static>(self) -> Result<E, Error> {
|
||||
if let Some(error) = self.downcast_ref::<E>() {
|
||||
unsafe {
|
||||
let error = ptr::read(error);
|
||||
@ -133,26 +133,26 @@ impl Exception {
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: StdError + Send + Sync + 'static> From<E> for Exception {
|
||||
fn from(error: E) -> Exception {
|
||||
Exception::new(error)
|
||||
impl<E: StdError + Send + Sync + 'static> From<E> for Error {
|
||||
fn from(error: E) -> Error {
|
||||
Error::new(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Exception {
|
||||
impl Deref for Error {
|
||||
type Target = dyn StdError + Send + Sync + 'static;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.inner.error()
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for Exception {
|
||||
impl DerefMut for Error {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
self.inner.error_mut()
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for Exception {
|
||||
impl Debug for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
writeln!(f, "{}", self.inner.error())?;
|
||||
|
||||
@ -186,16 +186,16 @@ impl Debug for Exception {
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Exception {
|
||||
impl Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.inner.error())
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Send for Exception {}
|
||||
unsafe impl Sync for Exception {}
|
||||
unsafe impl Send for Error {}
|
||||
unsafe impl Sync for Error {}
|
||||
|
||||
impl Drop for Exception {
|
||||
impl Drop for Error {
|
||||
fn drop(&mut self) {
|
||||
unsafe { ptr::drop_in_place(self.inner.error_mut()) }
|
||||
}
|
||||
@ -254,7 +254,7 @@ impl InnerException<()> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterator of errors in an `Exception`.
|
||||
/// Iterator of errors in an `Error`.
|
||||
pub struct Errors<'a> {
|
||||
next: Option<&'a (dyn StdError + 'static)>,
|
||||
}
|
||||
@ -278,13 +278,13 @@ mod repr_correctness {
|
||||
|
||||
#[test]
|
||||
fn size_of_exception() {
|
||||
assert_eq!(mem::size_of::<Exception>(), mem::size_of::<usize>());
|
||||
assert_eq!(mem::size_of::<Error>(), mem::size_of::<usize>());
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn assert_exception_autotraits()
|
||||
where
|
||||
Exception: Unpin + Send + Sync + 'static,
|
||||
Error: Unpin + Send + Sync + 'static,
|
||||
{
|
||||
}
|
||||
|
||||
@ -310,7 +310,7 @@ mod repr_correctness {
|
||||
|
||||
let has_dropped = Arc::new(Mutex::new(false));
|
||||
|
||||
drop(Exception::from(HasDrop(Box::new(has_dropped.clone()))));
|
||||
drop(Error::from(HasDrop(Box::new(has_dropped.clone()))));
|
||||
|
||||
assert!(*has_dropped.lock().unwrap());
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ mod error;
|
||||
|
||||
pub use crate::as_error::AsError;
|
||||
pub use crate::context::Context;
|
||||
pub use crate::error::{Errors, Exception};
|
||||
pub use crate::error::{Error, Errors};
|
||||
|
||||
/// Return early with an error.
|
||||
///
|
||||
@ -20,10 +20,10 @@ macro_rules! bail {
|
||||
|
||||
/// Construct an ad-hoc exception from a string.
|
||||
///
|
||||
/// This evaluates to an `Exception`. It can take either just a string, or a format string with
|
||||
/// This evaluates to an `Error`. It can take either just a string, or a format string with
|
||||
/// arguments. It also can take any custom type which implements `Debug` and `Display`.
|
||||
#[macro_export]
|
||||
macro_rules! error {
|
||||
($e:expr) => { $crate::Exception::new_adhoc($e) };
|
||||
($($arg:tt)*) => { $crate::Exception::new_adhoc(format!($($arg)*)) };
|
||||
($e:expr) => { $crate::Error::new_adhoc($e) };
|
||||
($($arg:tt)*) => { $crate::Error::new_adhoc(format!($($arg)*)) };
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user