generator: Improve error stack trace for errors in filters.

Reason/Context

When developing templates sometimes you make an implementation mistake in a filter could be a simple cannot call method of undefined, however when that happens we receive no information about where in the filter the error occurred. It sometimes takes a lot of time to figure out where the error is really located, and sometimes the only approach is step by step console log and comment out functionality.

As an example with the html template if you add a check to the following line such as obj.oneOf().length > 0 || it gives you the stack trace:

Something went wrong:
Template render error: (PATH@asyncapi\generator\node_modules\@asyncapi\html-template\template\index.html)
  Template render error: (PATH@asyncapi\generator\node_modules\@asyncapi\html-template\partials\content.html)
  Template render error: (PATH@asyncapi\generator\node_modules\@asyncapi\html-template\partials\operations.html)
  TypeError: Cannot read property 'length' of null
    at Object._prettifyError (PATH@asyncapi\generator\node_modules\nunjucks\src\lib.js:36:11)
    at PATH@asyncapi\generator\node_modules\nunjucks\src\environment.js:561:19
    at eval (eval at _compile (PATH@asyncapi\generator\node_modules\nunjucks\src\environment.js:631:18), <anonymous>:49:11)
    at PATH@asyncapi\generator\node_modules\nunjucks\src\environment.js:569:11
    at eval (eval at _compile (PATH@asyncapi\generator\node_modules\nunjucks\src\environment.js:631:18), <anonymous>:64:12)
    at PATH@asyncapi\generator\node_modules\nunjucks\src\environment.js:569:11
    at eval (eval at _compile (PATH@asyncapi\generator\node_modules\nunjucks\src\environment.js:631:18), <anonymous>:10:11)
    at PATH@asyncapi\generator\node_modules\nunjucks\src\environment.js:611:9
    at eval (eval at _compile (PATH@asyncapi\generator\node_modules\nunjucks\src\environment.js:631:18), <anonymous>:54:12)
    at PATH@asyncapi\generator\node_modules\nunjucks\src\environment.js:611:9
    at eval (eval at _compile (PATH@asyncapi\generator\node_modules\nunjucks\src\environment.js:631:18), <anonymous>:10:11)
    at PATH@asyncapi\generator\node_modules\nunjucks\src\environment.js:611:9
    at Template.root [as rootRenderFunc] (eval at _compile (PATH@asyncapi\generator\node_modules\nunjucks\src\environment.js:631:18), <anonymous>:75:3)
    at Template.getExported (PATH@asyncapi\generator\node_modules\nunjucks\src\environment.js:609:10)
    at eval (eval at _compile (PATH@asyncapi\generator\node_modules\nunjucks\src\environment.js:631:18), <anonymous>:9:5)
    at Environment.getTemplate (PATH@asyncapi\generator\node_modules\nunjucks\src\environment.js:277:9)

Which does not give you any information about where in the filter the error is located. This issue is based on the following Slack discussion: https://asyncapi.slack.com/archives/CQVJXFNQL/p1588245326200400

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 20 (20 by maintainers)

Most upvoted comments

Closing. Nunjucks render engine is not something we will invest in imho and the new React engine has everything that is needed already

@jonaslagoni debug merged in, you can add issues šŸ˜‰

@jonaslagoni yeap, this is why I’m working on it at the moment šŸ˜„

So far found https://github.com/mozilla/nunjucks/issues/1137 and https://github.com/mozilla/nunjucks/issues/975

I’m able to throw proper errors only if I do it from inside the filter, and console.log out the errors

function isExpandable(obj) {
    try {


        if (
            obj.type() === 'object' ||
            obj.oneOf().length > 0 ||
            obj.type() === 'array' ||
            (obj.oneOf() && obj.oneOf().length) ||
            (obj.anyOf() && obj.anyOf().length) ||
            (obj.allOf() && obj.allOf().length) ||
            obj.items() ||
            obj.additionalItems() ||
            (obj.properties() && Object.keys(obj.properties()).length) ||
            obj.additionalProperties() ||
            (obj.extensions() && Object.keys(obj.extensions()).filter(e => !e.startsWith('x-parser-')).length) ||
            obj.patternProperties()
        ) return true;

        return false;
    } catch (error) {
        console.log(error)
        process.exit(1)
    }
}

Then I get error:

TypeError: Cannot read property 'length' of null
    at Context.isExpandable (/Users/wookiee/Documents/sources/html-template/filters/all.js:9:16)
    at Object.eval (eval at _compile (/Users/wookiee/Documents/sources/generator/node_modules/nunjucks/src/environment.js:631:18), <anonymous>:49:34)
    at Context.<anonymous> (/Users/wookiee/Documents/sources/generator/node_modules/nunjucks/src/runtime.js:131:17)
    at Object.callWrap (/Users/wookiee/Documents/sources/generator/node_modules/nunjucks/src/runtime.js:273:14)
    at Object.eval (eval at _compile (/Users/wookiee/Documents/sources/generator/node_modules/nunjucks/src/environment.js:631:18), <anonymous>:139:87)
    at Context.<anonymous> (/Users/wookiee/Documents/sources/generator/node_modules/nunjucks/src/runtime.js:131:17)
    at Object.callWrap (/Users/wookiee/Documents/sources/generator/node_modules/nunjucks/src/runtime.js:273:14)
    at eval (eval at _compile (/Users/wookiee/Documents/sources/generator/node_modules/nunjucks/src/environment.js:631:18), <anonymous>:71:88)
    at /Users/wookiee/Documents/sources/generator/node_modules/nunjucks/src/environment.js:613:9
    at eval (eval at _compile (/Users/wookiee/Documents/sources/generator/node_modules/nunjucks/src/environment.js:631:18), <anonymous>:228:1)

Pretty much what we need, but yeah, you have to manually add it to a specific filter šŸ¤”