Skip to content

Commit 526ef1d

Browse files
authored
Merge pull request #66 from yoshuawuyts/err-helper
feat: add Error::new_from_str
2 parents aebbfd9 + 431d829 commit 526ef1d

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/error.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,24 @@ impl Error {
173173
}
174174
}
175175

176+
/// Create a new error object from static string.
177+
pub fn from_str<M>(kind: ErrorKind, msg: M, status: StatusCode) -> Self
178+
where
179+
M: Display + Debug + Send + Sync + 'static,
180+
{
181+
Self {
182+
kind,
183+
repr: Repr::Custom(anyhow::format_err!(msg)),
184+
status,
185+
}
186+
}
187+
176188
/// Create a new error from a message.
177189
pub(crate) fn new_adhoc<M>(message: M) -> Error
178190
where
179191
M: Display + Debug + Send + Sync + 'static,
180192
{
181-
Error {
182-
kind: ErrorKind::Other,
183-
repr: Repr::Custom(anyhow::format_err!(message)),
184-
status: StatusCode::InternalServerError,
185-
}
193+
Self::from_str(ErrorKind::Other, message, StatusCode::InternalServerError)
186194
}
187195

188196
/// Create a new error object from an I/O error.

0 commit comments

Comments
 (0)