Skip to main content

huskarl_core/
error.rs

1//! Error types and the [`Error`] trait.
2//!
3//! All errors in this library implement the [`Error`] trait, which extends
4//! [`std::error::Error`] with retry semantics. [`BoxedError`] provides
5//! type-erased error handling while preserving retryability.
6
7use std::convert::Infallible;
8
9use http::uri::InvalidUri;
10use snafu::{AsErrorSource, Snafu};
11
12use crate::platform::MaybeSendSync;
13
14/// Errors that may occur in the library.
15pub trait Error: std::error::Error + AsErrorSource + MaybeSendSync + 'static {
16    /// If true, this indicates that a failed request may succeed if retried.
17    fn is_retryable(&self) -> bool;
18}
19
20impl Error for Infallible {
21    fn is_retryable(&self) -> bool {
22        false
23    }
24}
25
26/// A boxed error that can be used without type parameters.
27#[derive(Debug, Snafu)]
28#[snafu(transparent)]
29pub struct BoxedError {
30    source: Box<dyn Error>,
31}
32
33impl BoxedError {
34    /// Create a new boxed error from a generic `Error`.
35    pub fn from_err<E: Error + 'static>(err: E) -> Self {
36        Self {
37            source: Box::new(err),
38        }
39    }
40}
41
42impl Error for BoxedError {
43    fn is_retryable(&self) -> bool {
44        self.source.is_retryable()
45    }
46}
47
48impl Error for InvalidUri {
49    fn is_retryable(&self) -> bool {
50        false
51    }
52}