XAMPPRocky / fluent-templates

Easily add Fluent to your Rust project.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Better error handling semanitcs

jamolnng opened this issue · comments

I will say #33 leaves a little ugly way of handling any errors given off by FluentLoader::call as this is my current solution

let fl = FluentLoader::new(loader).with_default_lang(langid!("en-US"));
tera.register_function(
  "fluent",
  move |args: &HashMap<String, serde_json::Value>| match fl.call(&args) {
    Ok(r) => Ok(r),
    Err(e) => match args.get("key").and_then(serde_json::Value::as_str) {
      Some(key) => Ok(key.into()),
      None => Err(e),
    },
  },
);

a solution to this might be to have something like

pub fn call_wrapper(...) -> Result<Json, loader::tera::Error> {
}

pub fn call(...) -> Result<Json, tera::Error> {
  call_wrapper(...)
}

which would change

let fl = FluentLoader::new(loader).with_default_lang(langid!("en-US"));
tera.register_function(
  "fluent",
  move |args: &HashMap<String, serde_json::Value>| match fl.call_wrapper(&args) {
    Ok(s) => Ok(s),
    Err(e) {
      match e {
        NoLangArgument => ...,
        LangArgumentInvalid => ...,
        NoFluentArgument => ...,
        JsonToFluentFail => ...,
        /* new */
        UnknownKey(key) => ...,
      }
    }
  },
);

thoughts?

Thank you for your issue! Sorry about the late reply, overall this seems like a solid approach, having fluent-templates handle errors for you in the templating languages integrations is what makes the most sense IMO.