move the error to thiserror
This commit is contained in:
parent
34b957332e
commit
583e9908bf
9
Cargo.lock
generated
9
Cargo.lock
generated
@ -2392,6 +2392,7 @@ dependencies = [
|
||||
"slog-term",
|
||||
"sqlx",
|
||||
"tera",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -3362,18 +3363,18 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.23"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "76cc616c6abf8c8928e2fdcc0dbfab37175edd8fb49a4641066ad1364fdab146"
|
||||
checksum = "e0f4a65597094d4483ddaed134f409b2cb7c1beccf25201a9f73c719254fa98e"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.23"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9be73a2caec27583d0046ef3796c3794f868a5bc813db689eed00c7631275cd1"
|
||||
checksum = "7765189610d8241a44529806d6fd1f2e0a08734313a35d5b3a556f92b381f3c0"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.24",
|
||||
"quote 1.0.8",
|
||||
|
@ -34,7 +34,8 @@ rand="0.8"
|
||||
rpassword = "5.0"
|
||||
clap = "2.33"
|
||||
fluent-templates = { version = "0.6", features = ["tera"] }
|
||||
fluent-langneg ="0.13"
|
||||
fluent-langneg = "0.13"
|
||||
thiserror = "1.0"
|
||||
|
||||
[build-dependencies]
|
||||
actix-web-static-files = "3.0"
|
||||
|
92
src/lib.rs
92
src/lib.rs
@ -24,47 +24,44 @@ use actix_web::HttpResponse;
|
||||
|
||||
use qrcode::types::QrError;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
|
||||
#[derive(Debug)]
|
||||
use thiserror::Error;
|
||||
#[derive(Error, Debug)]
|
||||
pub enum ServerError {
|
||||
Argonautic,
|
||||
Database(sqlx::Error),
|
||||
DatabaseMigration(sqlx::migrate::MigrateError),
|
||||
Environment,
|
||||
Template(tera::Error),
|
||||
Qr(QrError),
|
||||
Io(std::io::Error),
|
||||
#[error("Failed to encrypt the password {0} - aborting!")]
|
||||
Argonautica(argonautica::Error),
|
||||
#[error("The database could not be used: {0}")]
|
||||
Database(#[from] sqlx::Error),
|
||||
#[error("The database could not be migrated: {0}")]
|
||||
DatabaseMigration(#[from] sqlx::migrate::MigrateError),
|
||||
#[error("The environment file could not be read")]
|
||||
Environment(#[from] std::env::VarError),
|
||||
#[error("The templates could not be rendered correctly: {0}")]
|
||||
Template(#[from] tera::Error),
|
||||
#[error("The qr-code could not be generated: {0}")]
|
||||
Qr(#[from] QrError),
|
||||
#[error("Some error happened during input and output: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
#[error("Error: {0}")]
|
||||
User(String),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ServerError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Argonautic => write!(f, "Argonautica Error"),
|
||||
Self::Database(e) => write!(f, "Database Error: {}", e),
|
||||
Self::DatabaseMigration(e) => {
|
||||
write!(f, "Migration Error: {}", e)
|
||||
}
|
||||
Self::Environment => write!(f, "Environment Error"),
|
||||
Self::Template(e) => write!(f, "Template Error: {:?}", e),
|
||||
Self::Qr(e) => write!(f, "Qr Code Error: {:?}", e),
|
||||
Self::Io(e) => write!(f, "IO Error: {:?}", e),
|
||||
Self::User(data) => write!(f, "{}", data),
|
||||
}
|
||||
impl From<argonautica::Error> for ServerError {
|
||||
fn from(e: argonautica::Error) -> Self {
|
||||
Self::Argonautica(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl actix_web::error::ResponseError for ServerError {
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
match self {
|
||||
Self::Argonautic => HttpResponse::InternalServerError().json("Argonautica Error"),
|
||||
Self::Argonautica(_) => HttpResponse::InternalServerError().json("Argonautica Error"),
|
||||
Self::Database(e) => {
|
||||
HttpResponse::InternalServerError().json(format!("Database Error: {:?}", e))
|
||||
}
|
||||
Self::DatabaseMigration(_) => {
|
||||
unimplemented!("A migration error should never be rendered")
|
||||
}
|
||||
Self::Environment => HttpResponse::InternalServerError().json("Environment Error"),
|
||||
Self::Environment(_) => HttpResponse::InternalServerError().json("Environment Error"),
|
||||
Self::Template(e) => {
|
||||
HttpResponse::InternalServerError().json(format!("Template Error: {:?}", e))
|
||||
}
|
||||
@ -77,51 +74,6 @@ impl actix_web::error::ResponseError for ServerError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::env::VarError> for ServerError {
|
||||
fn from(e: std::env::VarError) -> Self {
|
||||
eprintln!("Environment error {:?}", e);
|
||||
Self::Environment
|
||||
}
|
||||
}
|
||||
|
||||
impl From<sqlx::Error> for ServerError {
|
||||
fn from(err: sqlx::Error) -> Self {
|
||||
eprintln!("Database error {:?}", err);
|
||||
Self::Database(err)
|
||||
}
|
||||
}
|
||||
impl From<sqlx::migrate::MigrateError> for ServerError {
|
||||
fn from(err: sqlx::migrate::MigrateError) -> Self {
|
||||
eprintln!("Database error {:?}", err);
|
||||
Self::DatabaseMigration(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<argonautica::Error> for ServerError {
|
||||
fn from(e: argonautica::Error) -> Self {
|
||||
eprintln!("Authentication error {:?}", e);
|
||||
Self::Argonautic
|
||||
}
|
||||
}
|
||||
impl From<tera::Error> for ServerError {
|
||||
fn from(e: tera::Error) -> Self {
|
||||
eprintln!("Template error {:?}", e);
|
||||
Self::Template(e)
|
||||
}
|
||||
}
|
||||
impl From<QrError> for ServerError {
|
||||
fn from(e: QrError) -> Self {
|
||||
eprintln!("Template error {:?}", e);
|
||||
Self::Qr(e)
|
||||
}
|
||||
}
|
||||
impl From<std::io::Error> for ServerError {
|
||||
fn from(e: std::io::Error) -> Self {
|
||||
eprintln!("IO error {:?}", e);
|
||||
Self::Io(e)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Protocol {
|
||||
Http,
|
||||
|
Loading…
Reference in New Issue
Block a user