Skip to content

Configurable Exception Handling for custom context variables #237

@gethrok

Description

@gethrok

Hi,
first of all thanks for the great recent upgrades to the date handling and other improvements!

I am now dealing with specific scenario, where we are using expression for data transformation from incoming JSONs through endpoints. The issue is following:

Imagine we have following JSON:

[{
          "productId": "p-1",
          "name": "Laptop",
          "quantity": 1,
          "price": 1200.50,
          "discount": null,
          "non_existing" : "test"
        },
        {
          "productId": "p-2",
          "name": "Mouse",
          "quantity": 2,
          "price": 25.00,
          "discount": 5.00
        } ] 

For each record we provide the JSON as custom context, but in the second record the "non_existing" parameter is missing, which is expected behaviour, however the Evaluator will throw exception in case the custom context variable is missing.

So far I did very ugly fix, essentially disabling context variable validation

public Object visit(Expr.Variable variable) {
        if (env == null) {
            throw new Exceptions.RuntimeException(
                variable.name,
                'Error executing variable expression: no context was provided.'
            );
        }

        if (env.contains(variable.name.lexeme)) {
            return env.get(variable.name.lexeme);
        } else {
            return '';
        }
    }

Instead of throwing the exception. I have tried to explore the option to include new Configuration parameter, however the Interpreter class has no access to Configuration class and it's a lot of refactoring to propagate it all the way to the Interpreter plus I don't think it's a good pattern. I was thinking about having some flag in the Environment instead, but that also feels a bit dirty (like a custom Global Context variable for example).

Do you have some suggestion?
Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions