Skip to content

Feature request: exhaustive_if, exhaustive_case, throw_error #843

Description

@hansottowirtz

We have internally implemented a throw_error tag, to make sure we make exhaustive "if" and "case" statements, and that we don't render invalid texts.

{%- if color == 'blue' -%}
bleu
{%- elsif color == 'green' -%}
vert
{%- elsif color == 'red' -%}
rouge
{%- else -%}
{% throw_error "unknown color: " | append: color %}
{%- endif -%}
class ThrowErrorTag extends Tag {
  value: Value;
  constructor(
    tagToken: TagToken,
    remainTokens: TopLevelToken[],
    liquid: Liquid
  ) {
    super(tagToken, remainTokens, liquid);
    this.value = new Value(tagToken.args, liquid);
  }
  *render(ctx: Context): Generator<unknown, string, Context> {
    const message = yield this.value.value(ctx);
    throw new Error(`throw_error: ${message}`);
  }
}

We like this feature, but would prefer some kind of exhaustive_if and exhaustive_case tag that automatically throws in the the "else" case. Would you be interested in a PR for this?
Or this could maybe be an exhaustiveIf, exhaustiveCase option in the Liquid constructor? Similar to lenientIf.
What do you think of the throw_error tag?
Or am I missing something and is there a way to do this already?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions