1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//! Error types returned by Condow
use std::fmt;

use thiserror::Error;

/// The error type used by `condow`
///
/// Further information is encoded with [CondowErrorKind].
#[derive(Error, Debug)]
pub struct CondowError {
    msg: String,
    #[source]
    source: Option<anyhow::Error>,
    kind: CondowErrorKind,
}

impl CondowError {
    pub fn new<T: Into<String>>(msg: T, kind: CondowErrorKind) -> Self {
        Self {
            msg: msg.into(),
            source: None,
            kind,
        }
    }
    pub fn new_invalid_range<T: Into<String>>(msg: T) -> Self {
        Self::new(msg, CondowErrorKind::InvalidRange)
    }

    pub fn new_not_found<T: Into<String>>(msg: T) -> Self {
        Self::new(msg, CondowErrorKind::NotFound)
    }

    pub fn new_access_denied<T: Into<String>>(msg: T) -> Self {
        Self::new(msg, CondowErrorKind::AccessDenied)
    }

    pub fn new_remote<T: Into<String>>(msg: T) -> Self {
        Self::new(msg, CondowErrorKind::Remote)
    }

    pub fn new_io<T: Into<String>>(msg: T) -> Self {
        Self::new(msg, CondowErrorKind::Io)
    }

    pub fn new_other<T: Into<String>>(msg: T) -> Self {
        Self::new(msg, CondowErrorKind::Other)
    }

    pub fn with_source<E: Into<anyhow::Error>>(mut self, err: E) -> Self {
        self.source = Some(err.into());
        self
    }

    pub fn msg(&self) -> &str {
        &self.msg
    }

    pub fn kind(&self) -> CondowErrorKind {
        self.kind
    }

    pub fn is_retryable(&self) -> bool {
        self.kind.is_retryable()
    }
}

impl fmt::Display for CondowError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.msg)
    }
}

/// Specifies the kind of a [CondowError]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum CondowErrorKind {
    /// An inavlid range was encountered.
    ///
    /// Errors with this kind are **not retryable**
    InvalidRange,
    /// The BLOB could not be found at a given location.
    ///
    /// Errors with this kind are **not retryable**
    NotFound,
    /// Access was denied to the BLOB at a given location.
    ///
    /// Could be "unauthenticated", "unauthorized" or anything else
    /// regarding credentials
    ///
    /// Errors with this kind are **not retryable**
    AccessDenied,
    /// The resource providing the BLOB encountered an error
    ///
    /// Errors with this kind are **retryable**
    Remote,
    /// Something went wrong with our data "on the wire"
    ///
    /// Errors with this kind are **retryable**
    Io,
    /// Anything else which does not fall under one of the other categories
    ///
    /// Errors with this kind are **not retryable**
    Other,
}

impl CondowErrorKind {
    pub fn is_retryable(self) -> bool {
        use CondowErrorKind::*;

        match self {
            InvalidRange => false,
            NotFound => false,
            AccessDenied => false,
            Remote => true,
            Io => true,
            Other => false,
        }
    }
}

impl From<CondowErrorKind> for CondowError {
    fn from(error_kind: CondowErrorKind) -> Self {
        CondowError::new(format!("An error occured: {:?}", error_kind), error_kind)
    }
}

impl From<std::io::Error> for CondowError {
    fn from(io: std::io::Error) -> Self {
        CondowError::new_io("io error").with_source(io)
    }
}

impl From<IoError> for CondowError {
    fn from(io: IoError) -> Self {
        CondowError::new_io(io.0)
    }
}

#[derive(Error, Debug)]
#[error("io error: {0}")]
pub struct IoError(pub String);

impl From<std::io::Error> for IoError {
    fn from(io: std::io::Error) -> Self {
        IoError(io.to_string())
    }
}