Browse Source

Trying to standardize errors

develop
Drew Short 6 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::config::model::Config;
use crate::server::error::APIError; use crate::server::error::APIError;
use crate::server::error::Result;
use crate::server::middleware::api_auth::APIAuthRootAndZone; use crate::server::middleware::api_auth::APIAuthRootAndZone;
use crate::server::router::AppState; use crate::server::router::AppState;
use crate::server::util; use crate::server::util;
@ -34,7 +35,7 @@ fn update_address(address: String) -> String {
address 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); let mut remote_address = String::from(remote_info);
if remote_address.contains(':') { if remote_address.contains(':') {
let last_colon_index = remote_address.rfind(':').unwrap(); let last_colon_index = remote_address.rfind(':').unwrap();
@ -59,7 +60,7 @@ fn get_request_address(req: &HttpRequest<AppState>) -> HttpResponse {
.content_type("text/plain") .content_type("text/plain")
.body(format!("{}", addr)), .body(format!("{}", addr)),
Err(e) => { Err(e) => {
error!("{}", e);
error!("{:?}", e);
HttpResponse::build(StatusCode::BAD_REQUEST).finish() HttpResponse::build(StatusCode::BAD_REQUEST).finish()
} }
} }
@ -117,7 +118,7 @@ fn update_address_automatically(req: &HttpRequest<AppState>) -> HttpResponse {
.content_type("text/plain") .content_type("text/plain")
.body(format!("{}", addr)), .body(format!("{}", addr)),
Err(e) => { Err(e) => {
error!("{}", e);
error!("{:?}", e);
HttpResponse::build(StatusCode::BAD_REQUEST).finish() 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::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)] #[derive(Debug)]
pub struct APIError { pub struct APIError {
description: String, description: String,
original_error: Option<Box<Error>>,
original_error: Option<Box<error::Error>>,
} }
impl APIError { 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), description: String::from(description),
original_error, original_error,
}
})
} }
} }
impl Error for APIError {}
impl error::Error for APIError {}
impl fmt::Display for APIError { impl fmt::Display for APIError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.original_error { match &self.original_error {
Some(original_error) => { Some(original_error) => {
write!(f, "{}: \"{}\"", self.description, original_error)
write!(f, "{}: \"{:?}\"", self.description, original_error)
} }
None => write!(f, "{}", self.description) None => write!(f, "{}", self.description)
} }

Loading…
Cancel
Save