switch from custom_error to thiserror

This commit is contained in:
Samuel Lorch 2023-10-29 22:05:17 +01:00
parent c10f5529d6
commit 7fcf84f62e
5 changed files with 41 additions and 24 deletions

7
Cargo.lock generated
View file

@ -226,12 +226,6 @@ dependencies = [
"subtle",
]
[[package]]
name = "custom_error"
version = "1.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f8a51dd197fa6ba5b4dc98a990a43cc13693c23eb0089ebb0fcc1f04152bca6"
[[package]]
name = "deranged"
version = "0.3.9"
@ -696,7 +690,6 @@ version = "0.1.0"
dependencies = [
"async-trait",
"axum",
"custom_error",
"ipnet",
"jsonrpsee",
"macaddr",

View file

@ -8,7 +8,6 @@ edition = "2021"
[dependencies]
async-trait = "0.1.74"
axum = "0.6.20"
custom_error = "1.9.2"
ipnet = { version = "2.8.0", features = ["serde"] }
jsonrpsee = { version = "0.20.3", features = ["server"] }
macaddr = { version = "1.0.1", features = ["serde"] }

View file

@ -12,12 +12,19 @@ use jsonrpsee::{
RpcModule,
};
use custom_error::custom_error;
use thiserror::Error;
use tracing::info;
custom_error! { pub ApiError
InvalidParams = "Invalid Parameters",
Leet = "1337",
#[derive(Error, Debug)]
pub enum ApiError {
#[error("Unsupported config version")]
InvalidParams,
#[error("1337")]
Leet,
#[error(transparent)]
ConfigError(#[from] crate::config_manager::ConfigError),
}
impl Into<ErrorObject<'static>> for ApiError {

View file

@ -7,18 +7,30 @@ use std::fs;
use std::sync::{Arc, Mutex, MutexGuard};
use std::{io, result::Result};
use custom_error::custom_error;
use pwhash::sha512_crypt;
custom_error! { pub ConfigError
IoError{source: io::Error} = "io error",
SerdeError{source: serde_json::Error} = "serde json error",
ValidatonError{source: validator::ValidationErrors} = "validation failed",
HashError{source: pwhash::error::Error} = "password hash generation",
UnsupportedVersionError = "unsupported config version",
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ConfigError {
#[error("Json Error")]
SerdeError(#[from] serde_json::Error),
#[error("Validation Error")]
ValidatonError(#[from] validator::ValidationErrors),
#[error("Hash Error")]
HashError(#[from] pwhash::error::Error),
#[error("Unsupported config version")]
UnsupportedVersionError,
/// Represents all other cases of `std::io::Error`.
#[error(transparent)]
IOError(#[from] std::io::Error),
}
/*
// TODO do Error conversion
impl Into<ApiError> for ConfigError {
fn into(self) -> ApiError {
@ -26,6 +38,8 @@ impl Into<ApiError> for ConfigError {
}
}
*/
pub const CURRENT_CONFIG_PATH: &str = "config.json";
pub const PENDING_CONFIG_PATH: &str = "pending.json";

View file

@ -21,11 +21,15 @@ use axum::{
use pwhash::sha512_crypt;
use tracing::info;
use custom_error::custom_error;
use thiserror::Error;
custom_error! { AuthError
NoSessionCookie = "No Session Cookie Found",
InvalidSession = "Invalid Session"
#[derive(Error, Debug)]
pub enum AuthError {
#[error("No Session Cookie Found")]
NoSessionCookie,
#[error("Invalid Session")]
InvalidSession,
}
const SESSION_COOKIE: &str = "session";