| 1 | extern crate env_loggerrust-analyzer cargo handlebars 5.1.2 env_logger/; |
| 2 | extern crate handlebarsrust-analyzer cargo handlebars 5.1.2 handlebars/; |
| 3 | #[macro_use] |
| 4 | extern crate serde_jsonrust-analyzer cargo handlebars 5.1.2 serde_json/; |
| 5 | |
| 6 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# as StdErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#; |
| 7 | |
| 8 | use handlebarsrust-analyzer cargo handlebars 5.1.2 crate/::{ |
| 9 | Contextrust-analyzer cargo handlebars 5.1.2 context/Context#, Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#, Helperrust-analyzer cargo handlebars 5.1.2 render/Helper#, Outputrust-analyzer cargo handlebars 5.1.2 output/Output#, RenderContextrust-analyzer cargo handlebars 5.1.2 render/RenderContext#, RenderErrorrust-analyzer cargo handlebars 5.1.2 error/RenderError#, RenderErrorReasonrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#, |
| 10 | }; |
| 11 | use thiserrorrust-analyzer cargo thiserror 1.0.58 crate/::Errorrust-analyzer cargo thiserror-impl 1.0.58 Error:; |
| 12 | |
| 13 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Errorrust-analyzer cargo thiserror-impl 1.0.58 Error:)] |
| 14 | pub enum HelperErrorrust-analyzer cargo handlebars 5.1.2 HelperError# { |
| 15 | #[errorrust-analyzer cargo thiserror-impl 1.0.58 error:("db error")] |
| 16 | DbErrorrust-analyzer cargo handlebars 5.1.2 HelperError#DbError#, |
| 17 | #[errorrust-analyzer cargo thiserror-impl 1.0.58 error:("api error")] |
| 18 | ApiErrorrust-analyzer cargo handlebars 5.1.2 HelperError#ApiError#, |
| 19 | } |
| 20 | |
| 21 | /// A helper that raise error according to parameters |
| 22 | pub fn error_helperrust-analyzer cargo handlebars 5.1.2 error_helper().( |
| 23 | h: &Helperrust-analyzer cargo handlebars 5.1.2 render/Helper#, |
| 24 | _: &Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#, |
| 25 | _: &Contextrust-analyzer cargo handlebars 5.1.2 context/Context#, |
| 26 | _: &mut RenderContextrust-analyzer cargo handlebars 5.1.2 render/RenderContext#, |
| 27 | _: &mut dyn Outputrust-analyzer cargo handlebars 5.1.2 output/Output#, |
| 28 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), RenderErrorrust-analyzer cargo handlebars 5.1.2 error/RenderError#> { |
| 29 | let param = h |
| 30 | .paramrust-analyzer cargo handlebars 5.1.2 render/impl#[`Helper<'rc>`]param().(0) |
| 31 | .ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(RenderErrorReasonrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#::ParamNotFoundForIndexrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#ParamNotFoundForIndex#("error", 0))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 32 | match param.valuerust-analyzer cargo handlebars 5.1.2 json/value/impl#[`PathAndJson<'rc>`]value().().as_strrust-analyzer cargo serde_json 1.0.114 value/impl#[Value]as_str().() { |
| 33 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("db") => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(RenderErrorReasonrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#::NestedErrorrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#NestedError#(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(HelperErrorrust-analyzer cargo handlebars 5.1.2 HelperError#::DbErrorrust-analyzer cargo handlebars 5.1.2 HelperError#DbError#)).intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), |
| 34 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("api") => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(RenderErrorReasonrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#::NestedErrorrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#NestedError#(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(HelperErrorrust-analyzer cargo handlebars 5.1.2 HelperError#::ApiErrorrust-analyzer cargo handlebars 5.1.2 HelperError#ApiError#)).intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), |
| 35 | _ => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()), |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | fn mainrust-analyzer cargo handlebars 5.1.2 main().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn StdErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#>> { |
| 40 | env_loggerrust-analyzer cargo env_logger 0.11.11 crate/::initrust-analyzer cargo env_logger 0.11.11 logger/init().(); |
| 41 | let mut handlebars = Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#::newrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]new().(); |
| 42 | |
| 43 | // template not found |
| 44 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!( |
| 45 | "{}", |
| 46 | handlebars |
| 47 | .register_template_filerust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]register_template_file().("notfound", "./examples/error/notfound.hbs") |
| 48 | .unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().() |
| 49 | ); |
| 50 | |
| 51 | // an invalid templat |
| 52 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!( |
| 53 | "{}", |
| 54 | handlebars |
| 55 | .register_template_filerust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]register_template_file().("error", "./examples/error/error.hbs") |
| 56 | .unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().() |
| 57 | ); |
| 58 | |
| 59 | // render error |
| 60 | let e1 = handlebars |
| 61 | .render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{#if}}", &jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!({})) |
| 62 | .unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().(); |
| 63 | let be1 = Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(e1); |
| 64 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{}", be1); |
| 65 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{}", be1.sourcerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<E>`][Error]source().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()); |
| 66 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{:?}", be1.sourcerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<E>`][Error]source().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().sourcerust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#source().()); |
| 67 | |
| 68 | // process error generated in helper |
| 69 | handlebars.register_helperrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]register_helper().("err", Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(error_helperrust-analyzer cargo handlebars 5.1.2 error_helper().)); |
| 70 | let e2 = handlebars |
| 71 | .render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{err \"db\"}}", &jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!({})) |
| 72 | .unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().(); |
| 73 | // get nested error to from `RenderError` |
| 74 | match e2.sourcerust-analyzer cargo handlebars 5.1.2 error/impl#[RenderError][Error]source().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|e| e.downcast_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/impl#[`dyn Error + 'static`]downcast_ref().::<HelperErrorrust-analyzer cargo handlebars 5.1.2 HelperError#>()) { |
| 75 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(HelperErrorrust-analyzer cargo handlebars 5.1.2 HelperError#::DbErrorrust-analyzer cargo handlebars 5.1.2 HelperError#DbError#) => { |
| 76 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Detected error from helper: db error",) |
| 77 | } |
| 78 | _ => {} |
| 79 | } |
| 80 | |
| 81 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 82 | } |