aboutsummaryrefslogtreecommitdiff
path: root/src/model/helper/error.rs
blob: e2ffdd6884cab47799252e99b32633a9f833b4fa (plain) (blame)
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
use err_derive::Error;
use serde::{Deserialize, Serialize};

use garage_util::error::Error as GarageError;

#[derive(Debug, Error, Serialize, Deserialize)]
pub enum Error {
	#[error(display = "Internal error: {}", _0)]
	Internal(#[error(source)] GarageError),

	#[error(display = "Bad request: {}", _0)]
	BadRequest(String),

	/// Bucket name is not valid according to AWS S3 specs
	#[error(display = "Invalid bucket name: {}", _0)]
	InvalidBucketName(String),

	#[error(display = "Access key not found: {}", _0)]
	NoSuchAccessKey(String),

	#[error(display = "Bucket not found: {}", _0)]
	NoSuchBucket(String),
}

impl From<garage_net::error::Error> for Error {
	fn from(e: garage_net::error::Error) -> Self {
		Error::Internal(GarageError::Net(e))
	}
}

pub trait OkOrBadRequest {
	type S;
	fn ok_or_bad_request<M: AsRef<str>>(self, reason: M) -> Result<Self::S, Error>;
}

impl<T, E> OkOrBadRequest for Result<T, E>
where
	E: std::fmt::Display,
{
	type S = T;
	fn ok_or_bad_request<M: AsRef<str>>(self, reason: M) -> Result<T, Error> {
		match self {
			Ok(x) => Ok(x),
			Err(e) => Err(Error::BadRequest(format!("{}: {}", reason.as_ref(), e))),
		}
	}
}

impl<T> OkOrBadRequest for Option<T> {
	type S = T;
	fn ok_or_bad_request<M: AsRef<str>>(self, reason: M) -> Result<T, Error> {
		match self {
			Some(x) => Ok(x),
			None => Err(Error::BadRequest(reason.as_ref().to_string())),
		}
	}
}