Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Exposing originalError in LiquidError #742

Closed
mohas opened this issue Aug 28, 2024 · 2 comments
Closed

Exposing originalError in LiquidError #742

mohas opened this issue Aug 28, 2024 · 2 comments

Comments

@mohas
Copy link

mohas commented Aug 28, 2024

Hi, in a recent project I'm using liquidjs heavily and this is one of the scenarios that I'm dealing with, I have custom tags and I need to stop render process if some rule breaking errors occur, I have a custom assert tag:

{% assert condition=.... action=... %}

Now, I need any further rendering to stop if condition is falsy also I need to inform the code that called render that what went wrong, so one very intuitive way is to throw a custom error in the assert tag

if(!Boolean(renderedCondition)) throw new AssertTagAssertionErrror('something went wrong', ErrorCodes.Foo)

and in the render:

engine.parseAndRender(template, {}).catch(e => {
   if('originalError' in e && e.originalError instanceof AssertTagAssertionErrror){
      // we know the exact scenario
   }
})

right now the parseAndRender only returns RenderError and originalError while present is protected and private and we don't have access to it in the catch block, we could write a special error code in the message and use that, but it is far from elegant, please provide a way to throw custom known Error types in filters and tags and drops, and catch and identify them when calling render function, thanks

if this is approved and you would like a PR please tell me I'm more than happy to make the changes

@jg-rp
Copy link
Contributor

jg-rp commented Aug 28, 2024

Could AssertTagAssertionError inherit from LiquidError instead? Like this..

import { Liquid, Tag, LiquidError } from "liquidjs";

class AssertTagAssertionError extends LiquidError {
  errorCode;

  constructor(err, errorCode, token) {
    super(err, token);
    this.errorCode = errorCode;
  }
}

class AssertTag extends Tag {
  constructor(token, remainTokens, liquid) {
    super(token, remainTokens, liquid);
    // Argument parsing omitted.
  }

  *render(ctx, emitter) {
    // Always throw, for demonstration purposes.
    throw new AssertTagAssertionError("something went wrong", 400, this.token);
  }
}

const liquid = new Liquid();
liquid.registerTag("assert", AssertTag);

liquid.parseAndRender("{% assert foo %}").catch((e) => {
  if (e instanceof AssertTagAssertionError) {
    console.error(e.message, e.errorCode);
  } else {
    throw e;
  }
});

output

something went wrong 400

github-actions bot pushed a commit that referenced this issue Aug 29, 2024
## [10.16.6](v10.16.5...v10.16.6) (2024-08-29)

### Bug Fixes

* expose originalError from LiquidError, [#742](#742) ([86f6bf0](86f6bf0))
@mohas
Copy link
Author

mohas commented Aug 29, 2024

yes this would be perfect

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

No branches or pull requests

3 participants