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
|
use std::fmt::Display;
use poem::{
error::{ParseMultipartError, ResponseError},
http::StatusCode,
};
use sqlx::postgres::PgDatabaseError;
#[derive(Debug)]
pub enum Error {
IOError(std::io::Error),
TOMLError(toml::de::Error),
SQLError(String),
DatabaseError(sqlx::Error),
NotFound,
MissingField,
Unauthorized,
MultipartError,
BadRequest,
UnsupportedFileType(String),
UploadDirectory(String),
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::SQLError(error) => write!(f, "SQL Error: {}", error),
Error::IOError(error) => write!(f, "IO Error: {}", error),
Error::TOMLError(error) => write!(f, "TOML deserialization error: {}", error),
Error::DatabaseError(error) => write!(f, "database error: {}", error),
Error::NotFound => write!(f, "not found"),
Error::MissingField => write!(f, "missing field in row"),
Error::Unauthorized => write!(f, "user unauthorized"),
Error::MultipartError => write!(f, "error parsing multipart form data"),
Error::BadRequest => write!(f, "bad request"),
Error::UnsupportedFileType(filetype) => {
write!(f, "unsupported file upload type: {}", filetype)
}
Error::UploadDirectory(directory) => {
write!(f, "invalid uploads directory: {}", directory)
}
}
}
}
impl std::error::Error for Error {}
impl ResponseError for Error {
fn status(&self) -> poem::http::StatusCode {
match self {
Error::IOError(_) => StatusCode::INTERNAL_SERVER_ERROR,
Error::TOMLError(_) => StatusCode::INTERNAL_SERVER_ERROR,
Error::DatabaseError(_) => StatusCode::INTERNAL_SERVER_ERROR,
Error::NotFound => StatusCode::NOT_FOUND,
Error::SQLError(_) => StatusCode::INTERNAL_SERVER_ERROR,
Error::MissingField => StatusCode::INTERNAL_SERVER_ERROR,
Error::Unauthorized => StatusCode::UNAUTHORIZED,
Error::MultipartError => StatusCode::BAD_REQUEST,
Error::BadRequest => StatusCode::BAD_REQUEST,
Error::UnsupportedFileType(_) => StatusCode::BAD_REQUEST,
Error::UploadDirectory(_) => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Self::IOError(e)
}
}
impl From<toml::de::Error> for Error {
fn from(e: toml::de::Error) -> Self {
Self::TOMLError(e)
}
}
impl From<sqlx::Error> for Error {
fn from(e: sqlx::Error) -> Self {
match e {
sqlx::Error::Database(database_error) => {
let error = database_error.downcast::<PgDatabaseError>();
match error.code() {
code => Error::SQLError(code.to_string()),
}
}
sqlx::Error::RowNotFound => Error::NotFound,
_ => Self::DatabaseError(e),
}
}
}
impl From<ParseMultipartError> for Error {
fn from(e: ParseMultipartError) -> Self {
Error::MultipartError
}
}
|