Use bail rather than wrapping a string where possible. (#249)

* Use bail rather than wrapping a string where possible.

* Revert the cuda default bit.
This commit is contained in:
Laurent Mazare
2023-07-26 15:42:46 +01:00
committed by GitHub
parent f052ba76cb
commit 1235aa2536
3 changed files with 13 additions and 8 deletions

View File

@ -174,6 +174,7 @@ pub enum Error {
#[error("unsupported safetensor dtype {0:?}")]
UnsupportedSafeTensorDtype(safetensors::Dtype),
/// Arbitrary errors wrapping.
#[error(transparent)]
Wrapped(Box<dyn std::error::Error + Send + Sync>),
@ -182,6 +183,10 @@ pub enum Error {
inner: Box<Self>,
backtrace: Box<std::backtrace::Backtrace>,
},
/// User generated error message, typically created via `bail!`.
#[error("{0}")]
Msg(String),
}
pub type Result<T> = std::result::Result<T, Error>;
@ -207,12 +212,12 @@ impl Error {
#[macro_export]
macro_rules! bail {
($msg:literal $(,)?) => {
return Err($crate::Error::Wrapped(format!($msg).into()).bt())
return Err($crate::Error::Msg(format!($msg).into()).bt())
};
($err:expr $(,)?) => {
return Err($crate::Error::Wrapped(format!($err).into()).bt())
return Err($crate::Error::Msg(format!($err).into()).bt())
};
($fmt:expr, $($arg:tt)*) => {
return Err($crate::Error::Wrapped(format!($fmt, $($arg)*).into()).bt())
return Err($crate::Error::Msg(format!($fmt, $($arg)*).into()).bt())
};
}