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
use std::fmt;
mod castings;

#[derive(Debug, Clone)]
pub enum Error {
	GenericError,
	InternalError(InternalError),
	UserError(UserError),
	BlockError(BlockError),
}

impl fmt::Display for Error {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			Error::GenericError => write!(f, "[g] Something unspecified went wrong."),
			Error::InternalError(err) => write!(f, "{}", err.to_string()),
			Error::UserError(err) => write!(f, "{}", err.to_string()),
			Error::BlockError(err) => write!(f, "{}", err.to_string()),
		}
	}
}

#[derive(Debug, Clone)]
pub enum BlockError {
	TypeExist(String),
	TypeGenericError(String),
	InputParse,
	/// Error for when a block method does not exist
	/// for a certain block type. (Name, Type)
	MethodExist(String, String),
}

impl fmt::Display for BlockError {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			BlockError::TypeExist(name) => {
				write!(f, "[bte] A block type called '{}' was not found.", name)
			}
			BlockError::MethodExist(name, block_type) => {
				write!(
					f,
					"[bme] A method called '{}' was not found for the {} block type.",
					name, block_type
				)
			}
			BlockError::TypeGenericError(err) => {
				write!(f, "[btg] {}", err)
			}
			BlockError::InputParse => {
				write!(f, "[bip] The input string could not be parsed properly.")
			}
		}
	}
}

#[derive(Debug, Clone)]
pub enum UserError {
	PasswordTooShort,
	PasswordMatch,
	EmailConfirmError(EmailConfirmError),
	NameNonexist(String),
	NameConflict(String),
	NameTooShort(String),
	JWTGeneric,
	NoAccess(NoAccessSubject),
	NeedAuth,
}

impl fmt::Display for UserError {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			UserError::NameConflict(name) => {
				write!(f, "[unc] Username '{}' is already in use.", name)
			}
			UserError::NameNonexist(name) => {
				write!(
					f,
					"[une] A user with the username '{}' was not found.",
					name
				)
			}
			UserError::PasswordTooShort => write!(f, "[ups] The password provided was too short."),
			UserError::NameTooShort(name) => {
				write!(f, "[uns] The username `{}` provided was too short.", name)
			}
			UserError::PasswordMatch => write!(f, "[upm] The password provided is not correct."),
			UserError::EmailConfirmError(err) => write!(f, "{}", err.to_string()),
			UserError::JWTGeneric => write!(
				f,
				"[ujg] Something unspecified went wrong with user sessions."
			),
			UserError::NoAccess(scope) => {
				write!(f, "[uad] Access to {} was denied.", scope.to_string())
			}
			UserError::NeedAuth => write!(f, "[uar] Authentication headers are required."),
		}
	}
}

#[derive(Debug, Clone)]
pub enum NoAccessSubject {
	OtherUserCredits,
	DeleteBlock(i64),
}

impl fmt::Display for NoAccessSubject {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			NoAccessSubject::OtherUserCredits => write!(f, "another user's credits"),
			NoAccessSubject::DeleteBlock(id) => write!(f, "deleting block {}", id),
		}
	}
}

#[derive(Debug, Clone)]
pub enum EmailConfirmError {
	NotFound(String),
	Expired,
	Invalid,
}

impl fmt::Display for EmailConfirmError {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			EmailConfirmError::NotFound(name) => write!(
				f,
				"[uecn] An email confirmation with the username \"{}\" was not found.",
				name
			),
			EmailConfirmError::Invalid => write!(
				f,
				"[ueci] The verification code and/or session code were incorrect.",
			),
			EmailConfirmError::Expired => write!(
				f,
				"[uece] The email confirmation has expired because more than 5 minutes has passed since its creation.",
			),
		}
	}
}

#[derive(Debug, Clone)]
pub enum InternalError {
	DatabaseTimeout,
	GenericInternalError,
	EmailError,
}

impl fmt::Display for InternalError {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			InternalError::DatabaseTimeout => write!(
				f,
				"[idt] There was an issue with connecting to the database."
			),
			InternalError::GenericInternalError => {
				write!(f, "[ig] Something unspecified went wrong internally.",)
			}
			InternalError::EmailError => {
				write!(f, "[img] Something went wrong with Loop's emailing system.")
			}
		}
	}
}