extern crate env_logger; extern crate handbar; #[macro_use] extern crate serde_json; use std::error::Error as StdError; use handbar::{Context, Handbar, Helper, Output, RenderContext, RenderError}; use thiserror::Error; #[derive(Debug, Error)] pub enum HelperError { #[error("db error")] DbError, #[error("api error")] ApiError, } /// A helper that raise error according to parameters pub fn error_helper( h: &Helper, _: &Handbar, _: &Context, _: &mut RenderContext, _: &mut dyn Output, ) -> Result<(), RenderError> { let param = h .param(0) .ok_or(RenderError::new("Param 0 is required for error helper."))?; match param.value().as_str() { Some("db") => Err(RenderError::from_error( "helper error", HelperError::DbError, )), Some("api") => Err(RenderError::from_error( "helper error", HelperError::ApiError, )), _ => Ok(()), } } fn main() -> Result<(), Box> { env_logger::init(); let mut handbar = Handbar::new(); // template not found println!( "{}", handbar .register_template_file("notfound", "./examples/error/notfound.hbs") .unwrap_err() ); // an invalid templat println!( "{}", handbar .register_template_file("error", "./examples/error/error.hbs") .unwrap_err() ); // render error let e1 = handbar .render_template("{{#if}}", &json!({})) .unwrap_err(); let be1 = Box::new(e1); println!("{}", be1); println!("{}", be1.source().unwrap()); println!("{:?}", be1.source().unwrap().source()); // process error generated in helper handbar.register_helper("err", Box::new(error_helper)); let e2 = handbar .render_template("{{err \"db\"}}", &json!({})) .unwrap_err(); // down-casting the error to user defined type match e2.source().and_then(|e| e.downcast_ref::()) { Some(HelperError::DbError) => { println!("Detected error from helper: db error",) } _ => {} } Ok(()) }