Better error handling added to walkable

This commit is contained in:
Tom Alexander 2020-04-12 20:52:30 -04:00
parent a647ed6d43
commit 9c7883358a
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 39 additions and 21 deletions

View File

@ -88,19 +88,32 @@ impl Renderable for serde_json::Value {
impl Walkable for serde_json::Value { impl Walkable for serde_json::Value {
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> { fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> {
match self { match self {
serde_json::Value::Null => panic!("Attempted to walk to {} on a null value", segment), serde_json::Value::Null => Err(RenderError::CantWalk {
serde_json::Value::Bool(_boolean) => { segment: segment.to_string(),
panic!("Attempted to walk to {} on a boolean value", segment) elem: self,
} }),
serde_json::Value::Number(_num) => { serde_json::Value::Bool(_boolean) => Err(RenderError::CantWalk {
panic!("Attempted to walk to {} on a number value", segment) segment: segment.to_string(),
} elem: self,
serde_json::Value::String(_string) => { }),
panic!("Attempted to walk to {} on a string value", segment) serde_json::Value::Number(_num) => Err(RenderError::CantWalk {
} segment: segment.to_string(),
elem: self,
}),
serde_json::Value::String(_string) => Err(RenderError::CantWalk {
segment: segment.to_string(),
elem: self,
}),
serde_json::Value::Array(_arr) => todo!("Arrays not supported yet"), serde_json::Value::Array(_arr) => todo!("Arrays not supported yet"),
// TODO: Handle this error better // TODO: Handle this error better
serde_json::Value::Object(obj) => Ok(obj.get(segment).unwrap()), serde_json::Value::Object(obj) => {
obj.get(segment)
.map(|val| val as _)
.ok_or(RenderError::WontWalk {
segment: segment.to_string(),
elem: self,
})
}
} }
} }
} }

View File

@ -139,24 +139,29 @@ mod tests {
impl<I: ContextElement> Walkable for HashMap<&str, I> { impl<I: ContextElement> Walkable for HashMap<&str, I> {
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> { fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> {
Ok(self.get(segment).unwrap()) let child = self.get(segment).ok_or(RenderError::WontWalk {
// TODO: Handle error here better segment: segment.to_string(),
// self.get(segment).ok_or(RenderError::WontWalk { elem: self,
// segment: segment.to_string(), })?;
// elem: self, Ok(child)
// })
} }
} }
impl Walkable for &str { impl Walkable for &str {
fn walk(&self, _segment: &str) -> Result<&dyn ContextElement, RenderError> { fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> {
panic!("Tried to walk down a str"); Err(RenderError::CantWalk {
segment: segment.to_string(),
elem: self,
})
} }
} }
impl Walkable for u32 { impl Walkable for u32 {
fn walk(&self, _segment: &str) -> Result<&dyn ContextElement, RenderError> { fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> {
panic!("Tried to walk down a str"); Err(RenderError::CantWalk {
segment: segment.to_string(),
elem: self,
})
} }
} }