From 3b61126d15e732ce3b9012f9dd680fe16436dccc Mon Sep 17 00:00:00 2001 From: John Barker Date: Mon, 23 May 2022 15:42:22 +0800 Subject: [PATCH] Expose try_build which returns Result<> --- src/debug/manager.rs | 11 ++++++++++- src/release/manager.rs | 12 +++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/debug/manager.rs b/src/debug/manager.rs index 16b75e4..b7a4f7c 100644 --- a/src/debug/manager.rs +++ b/src/debug/manager.rs @@ -25,6 +25,16 @@ impl StaticContextManager { etag_if_none_match: &EtagIfNoneMatch<'_>, name: S, ) -> StaticResponse { + self.try_build(etag_if_none_match, name).unwrap() + } + + /// Attempt to build a `StaticResponse`. + #[inline] + pub fn try_build>( + &self, + etag_if_none_match: &EtagIfNoneMatch<'_>, + name: S, + ) -> Result { self.resources .lock() .unwrap_or_else(PoisonError::into_inner) @@ -36,6 +46,5 @@ impl StaticContextManager { StaticResponse::build(&resource.0, resource.1.clone(), resource.2) } }) - .unwrap() } } diff --git a/src/release/manager.rs b/src/release/manager.rs index 97b6a18..62fcebc 100644 --- a/src/release/manager.rs +++ b/src/release/manager.rs @@ -23,6 +23,16 @@ impl StaticContextManager { etag_if_none_match: &EtagIfNoneMatch<'_>, name: S, ) -> StaticResponse { + self.try_build(etag_if_none_match, name).unwrap() + } + + /// Attempt to build a `StaticResponse`. + #[inline] + pub fn try_build>( + &self, + etag_if_none_match: &EtagIfNoneMatch<'_>, + name: S, + ) -> Result { self.resources .get_resource(name.as_ref()) .map(|resource| { @@ -32,6 +42,6 @@ impl StaticContextManager { StaticResponse::build(&resource.0, resource.1, resource.2) } }) - .unwrap() + .ok_or(std::io::Error::new(std::io::ErrorKind::NotFound, format!("{} not found", name.as_ref()))) } }