From ed4d7aa44a03d32cceaabc26c1e4017a6da4987e Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 27 May 2019 17:09:16 +1000 Subject: [PATCH] Replace `http_server` unwrap with 500 error --- beacon_node/http_server/src/api.rs | 7 ++++--- beacon_node/http_server/src/lib.rs | 8 ++++++++ beacon_node/http_server/src/metrics.rs | 6 ++++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/beacon_node/http_server/src/api.rs b/beacon_node/http_server/src/api.rs index 11afdb9df3..a910808998 100644 --- a/beacon_node/http_server/src/api.rs +++ b/beacon_node/http_server/src/api.rs @@ -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(req: &mut Request) -> IronResult { - // TODO: investigate unwrap - I'm _guessing_ we'll never hit it but we should check to be sure. - let beacon_chain = req.get::>>().unwrap(); + let beacon_chain = req + .get::>>() + .map_err(map_persistent_err_to_500)?; let response = json!({ "fork": beacon_chain.head().beacon_state.fork, diff --git a/beacon_node/http_server/src/lib.rs b/beacon_node/http_server/src/lib.rs index 77665db8dd..486badaff2 100644 --- a/beacon_node/http_server/src/lib.rs +++ b/beacon_node/http_server/src/lib.rs @@ -108,3 +108,11 @@ pub fn start_service( 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), + } +} diff --git a/beacon_node/http_server/src/metrics.rs b/beacon_node/http_server/src/metrics.rs index 30acb88531..eb7816d0eb 100644 --- a/beacon_node/http_server/src/metrics.rs +++ b/beacon_node/http_server/src/metrics.rs @@ -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( /// /// Returns a text string containing all metrics. fn handle_metrics(req: &mut Request) -> IronResult { - let beacon_chain = req.get::>>().unwrap(); + let beacon_chain = req + .get::>>() + .map_err(map_persistent_err_to_500)?; let r = Registry::new();