move http error class to router.rs

This commit is contained in:
Dietmar Maurer 2019-01-31 13:22:30 +01:00
parent b9903d6331
commit 5996577ab6
2 changed files with 26 additions and 26 deletions

View File

@ -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<Value, Error>;
type ApiAsyncHandlerFn = fn(Parts, Body, Value, &ApiAsyncMethod, &mut dyn RpcEnvironment) -> Result<BoxFut, Error>;

View File

@ -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,