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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#[cfg(ghost)]
use lib3h::error::Lib3hError;
use rusoto_core::RusotoError;
use rusoto_dynamodb::CreateTableError;
use rusoto_dynamodb::DeleteTableError;
use rusoto_dynamodb::DescribeLimitsError;
use rusoto_dynamodb::DescribeTableError;
use rusoto_dynamodb::GetItemError;
use rusoto_dynamodb::ListTablesError;
use rusoto_dynamodb::PutItemError;
use rusoto_dynamodb::ScanError;
use rusoto_dynamodb::UpdateItemError;
use std::num::ParseIntError;

#[derive(Debug, PartialEq)]
pub enum BbDhtError {
    // Rusoto mappings
    InternalServerError(String),
    ItemCollectionSizeLimitExceeded(String),
    ProvisionedThroughputExceeded(String),
    ConditionalCheckFailed(String),
    TransactionConflict(String),
    RequestLimitExceeded(String),
    LimitExceeded(String),
    ResourceNotFound(String),
    ResourceInUse(String),
    HttpDispatch(String),
    Credentials(String),
    Validation(String),
    ParseError(String),
    Unknown(String),
    // data handling
    MissingData(String),
    CorruptData(String),
}

impl std::fmt::Display for BbDhtError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let (error_type, error_string) = match self {
            BbDhtError::InternalServerError(s) => ("Internal server error", s),
            BbDhtError::ProvisionedThroughputExceeded(s) => ("Provisioned throughput exceeded", s),
            BbDhtError::ItemCollectionSizeLimitExceeded(s) => {
                ("Item collection size limit exceeded", s)
            }
            BbDhtError::ConditionalCheckFailed(s) => ("Conditional check failed", s),
            BbDhtError::TransactionConflict(s) => ("Transaction conflict", s),
            BbDhtError::RequestLimitExceeded(s) => ("Request limit exceeded", s),
            BbDhtError::LimitExceeded(s) => ("Limit exceeded", s),
            BbDhtError::ResourceNotFound(s) => ("Resource not found", s),
            BbDhtError::ResourceInUse(s) => ("Resource in use", s),
            BbDhtError::HttpDispatch(s) => ("HTTP dispatch", s),
            BbDhtError::Credentials(s) => ("Credentials", s),
            BbDhtError::Validation(s) => ("Validation", s),
            BbDhtError::ParseError(s) => ("Parse error", s),
            BbDhtError::Unknown(s) => ("Unknown", s),
            BbDhtError::MissingData(s) => ("Missing data", s),
            BbDhtError::CorruptData(s) => ("Corrupt data", s),
        };
        // Use `self.number` to refer to each positional data point.
        write!(f, "BbDhtError [{}]: {}", error_type, error_string)
    }
}

pub type BbDhtResult<T> = Result<T, BbDhtError>;

impl From<ParseIntError> for BbDhtError {
    fn from(int_error: ParseIntError) -> Self {
        BbDhtError::CorruptData(int_error.to_string())
    }
}

#[cfg(ghost)]
impl From<BbDhtError> for Lib3hError {
    fn from(bb_dht_error: BbDhtError) -> Self {
        Lib3hError::from(bb_dht_error.to_string())
    }
}

impl From<RusotoError<GetItemError>> for BbDhtError {
    fn from(rusoto_error: RusotoError<GetItemError>) -> Self {
        match rusoto_error {
            RusotoError::Service(service_error) => match service_error {
                GetItemError::InternalServerError(err) => BbDhtError::InternalServerError(err),
                GetItemError::RequestLimitExceeded(err) => BbDhtError::RequestLimitExceeded(err),
                GetItemError::ProvisionedThroughputExceeded(err) => {
                    BbDhtError::ProvisionedThroughputExceeded(err)
                }
                GetItemError::ResourceNotFound(err) => BbDhtError::ResourceNotFound(err),
            },
            RusotoError::HttpDispatch(err) => BbDhtError::HttpDispatch(err.to_string()),
            RusotoError::Credentials(err) => BbDhtError::Credentials(err.to_string()),
            RusotoError::Validation(err) => BbDhtError::Validation(err.to_string()),
            RusotoError::ParseError(err) => BbDhtError::ParseError(err.to_string()),
            RusotoError::Unknown(err) => BbDhtError::Unknown(format!("{:?}", err)),
        }
    }
}

impl From<RusotoError<PutItemError>> for BbDhtError {
    fn from(rusoto_error: RusotoError<PutItemError>) -> Self {
        match rusoto_error {
            RusotoError::Service(service_error) => match service_error {
                PutItemError::InternalServerError(err) => BbDhtError::InternalServerError(err),
                PutItemError::RequestLimitExceeded(err) => BbDhtError::RequestLimitExceeded(err),
                PutItemError::ProvisionedThroughputExceeded(err) => {
                    BbDhtError::ProvisionedThroughputExceeded(err)
                }
                PutItemError::ResourceNotFound(err) => BbDhtError::ResourceNotFound(err),
                PutItemError::ConditionalCheckFailed(err) => {
                    BbDhtError::ConditionalCheckFailed(err)
                }
                PutItemError::TransactionConflict(err) => BbDhtError::TransactionConflict(err),
                PutItemError::ItemCollectionSizeLimitExceeded(err) => {
                    BbDhtError::ItemCollectionSizeLimitExceeded(err)
                }
            },
            RusotoError::HttpDispatch(err) => BbDhtError::HttpDispatch(err.to_string()),
            RusotoError::Credentials(err) => BbDhtError::Credentials(err.to_string()),
            RusotoError::Validation(err) => BbDhtError::Validation(err.to_string()),
            RusotoError::ParseError(err) => BbDhtError::ParseError(err.to_string()),
            RusotoError::Unknown(err) => BbDhtError::Unknown(format!("{:?}", err)),
        }
    }
}

impl From<PutItemError> for BbDhtError {
    fn from(put_item_error: PutItemError) -> BbDhtError {
        BbDhtError::from(RusotoError::Service(put_item_error))
    }
}

impl From<RusotoError<UpdateItemError>> for BbDhtError {
    fn from(rusoto_error: RusotoError<UpdateItemError>) -> Self {
        match rusoto_error {
            RusotoError::Service(service_error) => match service_error {
                UpdateItemError::InternalServerError(err) => BbDhtError::InternalServerError(err),
                UpdateItemError::RequestLimitExceeded(err) => BbDhtError::RequestLimitExceeded(err),
                UpdateItemError::ProvisionedThroughputExceeded(err) => {
                    BbDhtError::ProvisionedThroughputExceeded(err)
                }
                UpdateItemError::ResourceNotFound(err) => BbDhtError::ResourceNotFound(err),
                UpdateItemError::ConditionalCheckFailed(err) => {
                    BbDhtError::ConditionalCheckFailed(err)
                }
                UpdateItemError::TransactionConflict(err) => BbDhtError::TransactionConflict(err),
                UpdateItemError::ItemCollectionSizeLimitExceeded(err) => {
                    BbDhtError::ItemCollectionSizeLimitExceeded(err)
                }
            },
            RusotoError::HttpDispatch(err) => BbDhtError::HttpDispatch(err.to_string()),
            RusotoError::Credentials(err) => BbDhtError::Credentials(err.to_string()),
            RusotoError::Validation(err) => BbDhtError::Validation(err.to_string()),
            RusotoError::ParseError(err) => BbDhtError::ParseError(err.to_string()),
            RusotoError::Unknown(err) => BbDhtError::Unknown(format!("{:?}", err)),
        }
    }
}

impl From<RusotoError<DescribeTableError>> for BbDhtError {
    fn from(rusoto_error: RusotoError<DescribeTableError>) -> Self {
        match rusoto_error {
            RusotoError::Service(service_error) => match service_error {
                DescribeTableError::InternalServerError(err) => {
                    BbDhtError::InternalServerError(err)
                }
                DescribeTableError::ResourceNotFound(err) => BbDhtError::ResourceNotFound(err),
            },
            RusotoError::HttpDispatch(err) => BbDhtError::HttpDispatch(err.to_string()),
            RusotoError::Credentials(err) => BbDhtError::Credentials(err.to_string()),
            RusotoError::Validation(err) => BbDhtError::Validation(err.to_string()),
            RusotoError::ParseError(err) => BbDhtError::ParseError(err.to_string()),
            RusotoError::Unknown(err) => BbDhtError::Unknown(format!("{:?}", err)),
        }
    }
}

impl From<RusotoError<DescribeLimitsError>> for BbDhtError {
    fn from(rusoto_error: RusotoError<DescribeLimitsError>) -> Self {
        match rusoto_error {
            RusotoError::Service(service_error) => match service_error {
                DescribeLimitsError::InternalServerError(err) => {
                    BbDhtError::InternalServerError(err)
                }
            },
            RusotoError::HttpDispatch(err) => BbDhtError::HttpDispatch(err.to_string()),
            RusotoError::Credentials(err) => BbDhtError::Credentials(err.to_string()),
            RusotoError::Validation(err) => BbDhtError::Validation(err.to_string()),
            RusotoError::ParseError(err) => BbDhtError::ParseError(err.to_string()),
            RusotoError::Unknown(err) => BbDhtError::Unknown(format!("{:?}", err)),
        }
    }
}

impl From<RusotoError<ListTablesError>> for BbDhtError {
    fn from(rusoto_error: RusotoError<ListTablesError>) -> Self {
        match rusoto_error {
            RusotoError::Service(service_error) => match service_error {
                ListTablesError::InternalServerError(err) => BbDhtError::InternalServerError(err),
            },
            RusotoError::HttpDispatch(err) => BbDhtError::HttpDispatch(err.to_string()),
            RusotoError::Credentials(err) => BbDhtError::Credentials(err.to_string()),
            RusotoError::Validation(err) => BbDhtError::Validation(err.to_string()),
            RusotoError::ParseError(err) => BbDhtError::ParseError(err.to_string()),
            RusotoError::Unknown(err) => BbDhtError::Unknown(format!("{:?}", err)),
        }
    }
}

impl From<RusotoError<CreateTableError>> for BbDhtError {
    fn from(rusoto_error: RusotoError<CreateTableError>) -> Self {
        match rusoto_error {
            RusotoError::Service(service_error) => match service_error {
                CreateTableError::InternalServerError(err) => BbDhtError::InternalServerError(err),
                CreateTableError::LimitExceeded(err) => BbDhtError::LimitExceeded(err),
                CreateTableError::ResourceInUse(err) => BbDhtError::ResourceInUse(err),
            },
            RusotoError::HttpDispatch(err) => BbDhtError::HttpDispatch(err.to_string()),
            RusotoError::Credentials(err) => BbDhtError::Credentials(err.to_string()),
            RusotoError::Validation(err) => BbDhtError::Validation(err.to_string()),
            RusotoError::ParseError(err) => BbDhtError::ParseError(err.to_string()),
            RusotoError::Unknown(err) => BbDhtError::Unknown(format!("{:?}", err)),
        }
    }
}

impl From<RusotoError<DeleteTableError>> for BbDhtError {
    fn from(rusoto_error: RusotoError<DeleteTableError>) -> Self {
        match rusoto_error {
            RusotoError::Service(service_error) => match service_error {
                DeleteTableError::InternalServerError(err) => BbDhtError::InternalServerError(err),
                DeleteTableError::LimitExceeded(err) => BbDhtError::LimitExceeded(err),
                DeleteTableError::ResourceInUse(err) => BbDhtError::ResourceInUse(err),
                DeleteTableError::ResourceNotFound(err) => BbDhtError::ResourceNotFound(err),
            },
            RusotoError::HttpDispatch(err) => BbDhtError::HttpDispatch(err.to_string()),
            RusotoError::Credentials(err) => BbDhtError::Credentials(err.to_string()),
            RusotoError::Validation(err) => BbDhtError::Validation(err.to_string()),
            RusotoError::ParseError(err) => BbDhtError::ParseError(err.to_string()),
            RusotoError::Unknown(err) => BbDhtError::Unknown(format!("{:?}", err)),
        }
    }
}

impl From<RusotoError<ScanError>> for BbDhtError {
    fn from(rusoto_error: RusotoError<ScanError>) -> Self {
        match rusoto_error {
            RusotoError::Service(service_error) => match service_error {
                ScanError::InternalServerError(err) => BbDhtError::InternalServerError(err),
                ScanError::RequestLimitExceeded(err) => BbDhtError::RequestLimitExceeded(err),
                ScanError::ProvisionedThroughputExceeded(err) => {
                    BbDhtError::ProvisionedThroughputExceeded(err)
                }
                ScanError::ResourceNotFound(err) => BbDhtError::ResourceNotFound(err),
            },
            RusotoError::HttpDispatch(err) => BbDhtError::HttpDispatch(err.to_string()),
            RusotoError::Credentials(err) => BbDhtError::Credentials(err.to_string()),
            RusotoError::Validation(err) => BbDhtError::Validation(err.to_string()),
            RusotoError::ParseError(err) => BbDhtError::ParseError(err.to_string()),
            RusotoError::Unknown(err) => BbDhtError::Unknown(format!("{:?}", err)),
        }
    }
}

impl std::error::Error for BbDhtError {}