XAMPPRocky/fluent-templates

Better error handling semanitcs

Open

#34 opened on Mar 19, 2022

View on GitHub
 (1 comment) (0 reactions) (0 assignees)Rust (38 forks)auto 404
enhancementgood first issuehelp wanted

Repository metrics

Stars
 (167 stars)
PR merge metrics
 (PR metrics pending)

Description

I will say https://github.com/XAMPPRocky/fluent-templates/pull/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?

Contributor guide