Replace http_server unwrap with 500 error

This commit is contained in:
Paul Hauner 2019-05-27 17:09:16 +10:00
parent 3a65f84b12
commit ed4d7aa44a
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
3 changed files with 16 additions and 5 deletions

View File

@ -1,4 +1,4 @@
use crate::key::BeaconChainKey;
use crate::{key::BeaconChainKey, map_persistent_err_to_500};
use beacon_chain::{BeaconChain, BeaconChainTypes};
use iron::prelude::*;
use iron::{
@ -58,8 +58,9 @@ impl AfterMiddleware for SetJsonContentType {
}
fn handle_fork<T: BeaconChainTypes + 'static>(req: &mut Request) -> IronResult<Response> {
// TODO: investigate unwrap - I'm _guessing_ we'll never hit it but we should check to be sure.
let beacon_chain = req.get::<Read<BeaconChainKey<T>>>().unwrap();
let beacon_chain = req
.get::<Read<BeaconChainKey<T>>>()
.map_err(map_persistent_err_to_500)?;
let response = json!({
"fork": beacon_chain.head().beacon_state.fork,

View File

@ -108,3 +108,11 @@ pub fn start_service<T: BeaconChainTypes + 'static>(
shutdown_trigger
}
/// Helper function for mapping a failure to read state to a 500 server error.
fn map_persistent_err_to_500(e: persistent::PersistentError) -> iron::error::IronError {
iron::error::IronError {
error: Box::new(e),
response: iron::Response::with(iron::status::Status::InternalServerError),
}
}

View File

@ -1,4 +1,4 @@
use crate::key::BeaconChainKey;
use crate::{key::BeaconChainKey, map_persistent_err_to_500};
use beacon_chain::{BeaconChain, BeaconChainTypes};
use iron::prelude::*;
use iron::{status::Status, Handler, IronResult, Request, Response};
@ -23,7 +23,9 @@ pub fn build_handler<T: BeaconChainTypes + 'static>(
///
/// Returns a text string containing all metrics.
fn handle_metrics<T: BeaconChainTypes + 'static>(req: &mut Request) -> IronResult<Response> {
let beacon_chain = req.get::<Read<BeaconChainKey<T>>>().unwrap();
let beacon_chain = req
.get::<Read<BeaconChainKey<T>>>()
.map_err(map_persistent_err_to_500)?;
let r = Registry::new();