From 5996577ab6f52a4ad16a8b530161022caa182ac5 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 31 Jan 2019 13:22:30 +0100 Subject: [PATCH] move http error class to router.rs --- src/api/router.rs | 27 ++++++++++++++++++++++++++- src/server/rest.rs | 25 ------------------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/api/router.rs b/src/api/router.rs index 0aaeec8f..581c5ae0 100644 --- a/src/api/router.rs +++ b/src/api/router.rs @@ -4,8 +4,9 @@ use crate::api::schema::*; use serde_json::{Value}; use std::collections::HashMap; use std::sync::Arc; +use std::fmt; -use hyper::{Body, Response}; +use hyper::{Body, Response, StatusCode}; use hyper::rt::Future; use hyper::http::request::Parts; @@ -34,6 +35,30 @@ pub enum RpcEnvironmentType { PRIVILEDGED, } +#[derive(Debug, Fail)] +pub struct HttpError { + pub code: StatusCode, + pub message: String, +} + +impl HttpError { + pub fn new(code: StatusCode, message: String) -> Self { + HttpError { code, message } + } +} + +impl fmt::Display for HttpError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Error {}: {}", self.code, self.message) + } +} + +macro_rules! http_err { + ($status:ident, $msg:expr) => {{ + Error::from(HttpError::new(StatusCode::$status, $msg)) + }} +} + type ApiHandlerFn = fn(Value, &ApiMethod, &mut dyn RpcEnvironment) -> Result; type ApiAsyncHandlerFn = fn(Parts, Body, Value, &ApiAsyncMethod, &mut dyn RpcEnvironment) -> Result; diff --git a/src/server/rest.rs b/src/server/rest.rs index 787bf20f..12010e46 100644 --- a/src/server/rest.rs +++ b/src/server/rest.rs @@ -6,7 +6,6 @@ use crate::auth_helpers::*; use super::environment::RestEnvironment; use super::formatter::*; -use std::fmt; use std::path::{Path, PathBuf}; use std::sync::Arc; use std::collections::HashMap; @@ -84,30 +83,6 @@ impl Service for ApiService { } } -#[derive(Debug, Fail)] -pub struct HttpError { - pub code: StatusCode, - pub message: String, -} - -impl HttpError { - pub fn new(code: StatusCode, message: String) -> Self { - HttpError { code, message } - } -} - -impl fmt::Display for HttpError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Error {}: {}", self.code, self.message) - } -} - -macro_rules! http_err { - ($status:ident, $msg:expr) => {{ - Error::from(HttpError::new(StatusCode::$status, $msg)) - }} -} - fn get_request_parameters_async( info: &'static ApiMethod, parts: Parts,