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

View file

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

View file

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

View file

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

View file

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