WIP: More restructuring to have ApiService be a future.
This commit is contained in:
parent
405a59e8b9
commit
965d6f1df9
@ -36,4 +36,5 @@ lighthouse_metrics = { path = "../../eth2/utils/lighthouse_metrics" }
|
|||||||
slot_clock = { path = "../../eth2/utils/slot_clock" }
|
slot_clock = { path = "../../eth2/utils/slot_clock" }
|
||||||
hex = "0.3.2"
|
hex = "0.3.2"
|
||||||
parking_lot = "0.9"
|
parking_lot = "0.9"
|
||||||
|
futures = "0.1.25"
|
||||||
|
|
||||||
|
@ -1,15 +1,8 @@
|
|||||||
use hyper::{Body, Method, Request, Response, Server, StatusCode};
|
use hyper::{Body, Method, Request, Response, Server, StatusCode};
|
||||||
use std::error::Error as StdError;
|
use std::error::Error as StdError;
|
||||||
|
|
||||||
type Cause = Box<dyn StdErr + Send + Sync>;
|
|
||||||
|
|
||||||
pub struct ApiError {
|
|
||||||
kind: ApiErrorKind,
|
|
||||||
cause: Option<Cause>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
pub enum ApiErrorKind {
|
pub enum ApiError {
|
||||||
MethodNotAllowed(String),
|
MethodNotAllowed(String),
|
||||||
ServerError(String),
|
ServerError(String),
|
||||||
NotImplemented(String),
|
NotImplemented(String),
|
||||||
@ -20,20 +13,26 @@ pub enum ApiErrorKind {
|
|||||||
|
|
||||||
pub type ApiResult = Result<Response<Body>, ApiError>;
|
pub type ApiResult = Result<Response<Body>, ApiError>;
|
||||||
|
|
||||||
impl Into<Response<Body>> for ApiError {
|
impl ApiError {
|
||||||
fn into(self) -> Response<Body> {
|
pub fn status_code(&self) -> (StatusCode, &String) {
|
||||||
let status_code: (StatusCode, String) = match self {
|
match self {
|
||||||
ApiError::MethodNotAllowed(desc) => (StatusCode::METHOD_NOT_ALLOWED, desc),
|
ApiError::MethodNotAllowed(desc) => (StatusCode::METHOD_NOT_ALLOWED, desc),
|
||||||
ApiError::ServerError(desc) => (StatusCode::INTERNAL_SERVER_ERROR, desc),
|
ApiError::ServerError(desc) => (StatusCode::INTERNAL_SERVER_ERROR, desc),
|
||||||
ApiError::NotImplemented(desc) => (StatusCode::NOT_IMPLEMENTED, desc),
|
ApiError::NotImplemented(desc) => (StatusCode::NOT_IMPLEMENTED, desc),
|
||||||
ApiError::InvalidQueryParams(desc) => (StatusCode::BAD_REQUEST, desc),
|
ApiError::InvalidQueryParams(desc) => (StatusCode::BAD_REQUEST, desc),
|
||||||
ApiError::NotFound(desc) => (StatusCode::NOT_FOUND, desc),
|
ApiError::NotFound(desc) => (StatusCode::NOT_FOUND, desc),
|
||||||
ApiError::ImATeapot(desc) => (StatusCode::IM_A_TEAPOT, 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()
|
Response::builder()
|
||||||
.status(status_code.0)
|
.status(status_code.0)
|
||||||
.header("content-type", "text/plain")
|
.header("content-type", "text/plain")
|
||||||
.body(Body::from(status_code.1))
|
.body(Body::from(*status_code.1))
|
||||||
.expect("Response should always be created.")
|
.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 {
|
impl StdError for ApiError {
|
||||||
fn cause(&self) -> Option<&Error> {}
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ mod spec;
|
|||||||
mod url_query;
|
mod url_query;
|
||||||
mod validator;
|
mod validator;
|
||||||
|
|
||||||
|
use error::{ApiError, ApiResult};
|
||||||
use beacon_chain::{BeaconChain, BeaconChainTypes};
|
use beacon_chain::{BeaconChain, BeaconChainTypes};
|
||||||
use client_network::NetworkMessage;
|
use client_network::NetworkMessage;
|
||||||
use client_network::Service as NetworkService;
|
use client_network::Service as NetworkService;
|
||||||
@ -34,7 +35,6 @@ use url_query::UrlQuery;
|
|||||||
pub use beacon::{BlockResponse, HeadResponse, StateResponse};
|
pub use beacon::{BlockResponse, HeadResponse, StateResponse};
|
||||||
pub use config::Config as ApiConfig;
|
pub use config::Config as ApiConfig;
|
||||||
use eth2_libp2p::rpc::RequestId;
|
use eth2_libp2p::rpc::RequestId;
|
||||||
use serde::ser::StdError;
|
|
||||||
|
|
||||||
type BoxFut = Box<dyn Future<Item = Response<Body>, Error = ApiError> + Send>;
|
type BoxFut = Box<dyn Future<Item = Response<Body>, Error = ApiError> + Send>;
|
||||||
|
|
||||||
@ -158,18 +158,19 @@ impl<T: BeaconChainTypes> Service for ApiService<T> {
|
|||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
metrics::inc_counter(&metrics::SUCCESS_COUNT);
|
metrics::inc_counter(&metrics::SUCCESS_COUNT);
|
||||||
slog::debug!(self.log, "Request successful: {:?}", path);
|
slog::debug!(self.log, "Request successful: {:?}", path);
|
||||||
Box::new(response)
|
response
|
||||||
}
|
}
|
||||||
// Map the `ApiError` into `hyper::Response`.
|
// Map the `ApiError` into `hyper::Response`.
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::debug!(self.log, "Request failure: {:?}", path);
|
slog::debug!(self.log, "Request failure: {:?}", path);
|
||||||
Box::new(e.into())
|
e.into()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
metrics::stop_timer(timer);
|
metrics::stop_timer(timer);
|
||||||
|
|
||||||
Box::new(futures::future::ok(response))
|
Box::new(futures::future::ok(response))
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,6 +237,15 @@ pub fn start_server<T: BeaconChainTypes>(
|
|||||||
Ok(exit_signal)
|
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> {
|
fn success_response(body: Body) -> Response<Body> {
|
||||||
Response::builder()
|
Response::builder()
|
||||||
.status(StatusCode::OK)
|
.status(StatusCode::OK)
|
||||||
|
Loading…
Reference in New Issue
Block a user