WIP: More restructuring to have ApiService be a future.

This commit is contained in:
Luke Anderson 2019-09-10 15:35:54 +10:00
parent 405a59e8b9
commit 965d6f1df9
No known key found for this signature in database
GPG Key ID: 44408169EC61E228
3 changed files with 40 additions and 21 deletions

View File

@ -36,4 +36,5 @@ lighthouse_metrics = { path = "../../eth2/utils/lighthouse_metrics" }
slot_clock = { path = "../../eth2/utils/slot_clock" }
hex = "0.3.2"
parking_lot = "0.9"
futures = "0.1.25"

View File

@ -1,15 +1,8 @@
use hyper::{Body, Method, Request, Response, Server, StatusCode};
use std::error::Error as StdError;
type Cause = Box<dyn StdErr + Send + Sync>;
pub struct ApiError {
kind: ApiErrorKind,
cause: Option<Cause>,
}
#[derive(PartialEq, Debug)]
pub enum ApiErrorKind {
pub enum ApiError {
MethodNotAllowed(String),
ServerError(String),
NotImplemented(String),
@ -20,21 +13,27 @@ pub enum ApiErrorKind {
pub type ApiResult = Result<Response<Body>, ApiError>;
impl Into<Response<Body>> for ApiError {
fn into(self) -> Response<Body> {
let status_code: (StatusCode, String) = match self {
impl ApiError {
pub fn status_code(&self) -> (StatusCode, &String) {
match self {
ApiError::MethodNotAllowed(desc) => (StatusCode::METHOD_NOT_ALLOWED, desc),
ApiError::ServerError(desc) => (StatusCode::INTERNAL_SERVER_ERROR, desc),
ApiError::NotImplemented(desc) => (StatusCode::NOT_IMPLEMENTED, desc),
ApiError::InvalidQueryParams(desc) => (StatusCode::BAD_REQUEST, desc),
ApiError::NotFound(desc) => (StatusCode::NOT_FOUND, desc),
ApiError::ImATeapot(desc) => (StatusCode::IM_A_TEAPOT, desc),
};
}
}
}
impl Into<Response<Body>> for ApiError {
fn into(self) -> Response<Body> {
let status_code = self.status_code();
Response::builder()
.status(status_code.0)
.header("content-type", "text/plain")
.body(Body::from(status_code.1))
.expect("Response should always be created.")
.status(status_code.0)
.header("content-type", "text/plain")
.body(Body::from(*status_code.1))
.expect("Response should always be created.")
}
}
@ -56,6 +55,15 @@ impl From<state_processing::per_slot_processing::Error> for ApiError {
}
}
impl std::error::Error for ApiError {
fn cause(&self) -> Option<&Error> {}
impl StdError for ApiError {
fn cause(&self) -> Option<&StdError> {
None
}
}
impl std::fmt::Display for ApiError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let status = self.status_code();
write!(f, "{:?}: {:?}", status.0, status.1)
}
}

View File

@ -14,6 +14,7 @@ mod spec;
mod url_query;
mod validator;
use error::{ApiError, ApiResult};
use beacon_chain::{BeaconChain, BeaconChainTypes};
use client_network::NetworkMessage;
use client_network::Service as NetworkService;
@ -34,7 +35,6 @@ use url_query::UrlQuery;
pub use beacon::{BlockResponse, HeadResponse, StateResponse};
pub use config::Config as ApiConfig;
use eth2_libp2p::rpc::RequestId;
use serde::ser::StdError;
type BoxFut = Box<dyn Future<Item = Response<Body>, Error = ApiError> + Send>;
@ -158,18 +158,19 @@ impl<T: BeaconChainTypes> Service for ApiService<T> {
Ok(response) => {
metrics::inc_counter(&metrics::SUCCESS_COUNT);
slog::debug!(self.log, "Request successful: {:?}", path);
Box::new(response)
response
}
// Map the `ApiError` into `hyper::Response`.
Err(e) => {
slog::debug!(self.log, "Request failure: {:?}", path);
Box::new(e.into())
e.into()
}
};
metrics::stop_timer(timer);
Box::new(futures::future::ok(response))
}
}
@ -236,6 +237,15 @@ pub fn start_server<T: BeaconChainTypes>(
Ok(exit_signal)
}
impl<T: BeaconChainTypes> Future for ApiService<T> {
type Item = Result<Response<Body>, ApiError>;
type Error = ApiError;
fn poll(&mut self) -> Result<Async<Self::Item>, Self::Error> {
unimplemented!()
}
}
fn success_response(body: Body) -> Response<Body> {
Response::builder()
.status(StatusCode::OK)