Browse Source

Trying to standardize errors

develop
Drew Short 5 years ago
parent
commit
7e69b9f29e
  1. 7
      src/server/api.rs
  2. 43
      src/server/error.rs

7
src/server/api.rs

@ -11,6 +11,7 @@ use futures::future::Future;
use crate::config::model::Config;
use crate::server::error::APIError;
use crate::server::error::Result;
use crate::server::middleware::api_auth::APIAuthRootAndZone;
use crate::server::router::AppState;
use crate::server::util;
@ -34,7 +35,7 @@ fn update_address(address: String) -> String {
address
}
fn parse_remote_info(remote_info: &str) -> Result<IpAddr, APIError> {
fn parse_remote_info(remote_info: &str) -> Result<IpAddr> {
let mut remote_address = String::from(remote_info);
if remote_address.contains(':') {
let last_colon_index = remote_address.rfind(':').unwrap();
@ -59,7 +60,7 @@ fn get_request_address(req: &HttpRequest<AppState>) -> HttpResponse {
.content_type("text/plain")
.body(format!("{}", addr)),
Err(e) => {
error!("{}", e);
error!("{:?}", e);
HttpResponse::build(StatusCode::BAD_REQUEST).finish()
}
}
@ -117,7 +118,7 @@ fn update_address_automatically(req: &HttpRequest<AppState>) -> HttpResponse {
.content_type("text/plain")
.body(format!("{}", addr)),
Err(e) => {
error!("{}", e);
error!("{:?}", e);
HttpResponse::build(StatusCode::BAD_REQUEST).finish()
}
}

43
src/server/error.rs

@ -1,28 +1,57 @@
use std::error::Error;
use std::error;
use std::fmt;
use std::net;
use std::result;
pub type Result<T> = result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
APIError(APIError),
Cloudflare(cloudflare::errors::Error),
AddrParseError(net::AddrParseError)
}
impl From<APIError> for Error {
fn from(err: APIError) -> Error {
Error::APIError(err)
}
}
impl From<cloudflare::errors::Error> for Error {
fn from(err: cloudflare::errors::Error) -> Error {
Error::Cloudflare(err)
}
}
impl From<net::AddrParseError> for Error {
fn from(err: net::AddrParseError) -> Error {
Error::AddrParseError(err)
}
}
#[derive(Debug)]
pub struct APIError {
description: String,
original_error: Option<Box<Error>>,
original_error: Option<Box<error::Error>>,
}
impl APIError {
pub fn new(description: &str, original_error: Option<Box<Error>>) -> APIError {
APIError {
pub fn new(description: &str, original_error: Option<Box<error::Error>>) -> Error {
Error::from(APIError {
description: String::from(description),
original_error,
}
})
}
}
impl Error for APIError {}
impl error::Error for APIError {}
impl fmt::Display for APIError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.original_error {
Some(original_error) => {
write!(f, "{}: \"{}\"", self.description, original_error)
write!(f, "{}: \"{:?}\"", self.description, original_error)
}
None => write!(f, "{}", self.description)
}

Loading…
Cancel
Save