Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Better error handling semanitcs #34

Open
jamolnng opened this issue Mar 19, 2022 · 1 comment
Open

Better error handling semanitcs #34

jamolnng opened this issue Mar 19, 2022 · 1 comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@jamolnng
Copy link
Contributor

jamolnng commented Mar 19, 2022

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?

@XAMPPRocky
Copy link
Owner

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.

@XAMPPRocky XAMPPRocky added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels May 24, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants