peach-dyndns-server/src/errors.rs

63 lines
2.2 KiB
Rust
Raw Normal View History

2021-05-22 17:36:34 +00:00
use std::{error, str};
use jsonrpc_core::{types::error::Error, ErrorCode};
use snafu::Snafu;
2021-05-18 07:25:18 +00:00
use std::string::FromUtf8Error;
2021-05-22 17:36:34 +00:00
pub type BoxError = Box<dyn error::Error>;
#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))]
pub enum PeachDynDnsError {
#[snafu(display("Missing expected parameters: {}", e))]
MissingParams { e: Error },
#[snafu(display("Domain is in invalid format: {}", domain))]
InvalidDomain { domain: String },
#[snafu(display("There was an error in the bind configuration"))]
BindConfigurationError,
#[snafu(display("This domain was already registered: {}", domain))]
DomainAlreadyExistsError { domain: String },
#[snafu(display("Error parsing key file: {}", source))]
KeyFileParseError { source: FromUtf8Error },
#[snafu(display("Error generating key: {}", source))]
2021-05-25 14:14:29 +00:00
KeyGenerationError { source: std::io::Error },
2021-05-18 07:25:18 +00:00
}
2021-05-22 17:36:34 +00:00
impl From<PeachDynDnsError> for Error {
fn from(err: PeachDynDnsError) -> Self {
match &err {
PeachDynDnsError::MissingParams { e } => e.clone(),
2021-05-25 14:14:29 +00:00
PeachDynDnsError::InvalidDomain { domain } => Error {
2021-05-22 17:36:34 +00:00
code: ErrorCode::ServerError(-32028),
message: format!("Domain is invalid format: {}", domain),
data: None,
},
PeachDynDnsError::BindConfigurationError => Error {
code: ErrorCode::ServerError(-32029),
2021-05-25 14:12:12 +00:00
message: "There was a bind configuration error".to_string(),
2021-05-22 17:36:34 +00:00
data: None,
},
2021-05-25 14:14:29 +00:00
PeachDynDnsError::DomainAlreadyExistsError { domain } => Error {
2021-05-22 17:36:34 +00:00
code: ErrorCode::ServerError(-32030),
message: format!("Can't register domain that already exists: {}", domain),
data: None,
},
2021-05-25 14:14:29 +00:00
PeachDynDnsError::KeyFileParseError { source: _ } => Error {
2021-05-22 17:36:34 +00:00
code: ErrorCode::ServerError(-32031),
2021-05-25 14:12:12 +00:00
message: "Error parsing key file".to_string(),
2021-05-22 17:36:34 +00:00
data: None,
},
2021-05-25 14:14:29 +00:00
PeachDynDnsError::KeyGenerationError { source: _ } => Error {
2021-05-22 17:36:34 +00:00
code: ErrorCode::ServerError(-32032),
2021-05-25 14:12:12 +00:00
message: "Key generation error".to_string(),
2021-05-22 17:36:34 +00:00
data: None,
},
}
2021-05-18 07:25:18 +00:00
}
2021-05-25 14:14:29 +00:00
}