summaryrefslogtreecommitdiffstats
path: root/src/error.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs
index 03cad93..ef8ddd3 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -1,22 +1,46 @@
use std::fmt::Display;
+use poem::{error::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,
}
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"),
}
}
}
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,
+ }
+ }
+}
+
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Self::IOError(e)
@@ -28,3 +52,18 @@ impl From<toml::de::Error> for Error {
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),
+ }
+ }
+}