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

Errors thrown by global functions get stacktrace swallowed unless dev is set #1430

Open
pezholio opened this issue Jan 18, 2023 · 0 comments

Comments

@pezholio
Copy link

Hi,

I've been trying to diagnose an issue where I'm using a global function within a Nunjucks template. By default, I just get the errror:

Error:

Template render error: (/path/to/project/dist/server/views/view.njk) [Line 122, Column 58]
  Error: 
    at Object._prettifyError (/path/to/project/node_modules/nunjucks/src/lib.js:36:11)
    at /path/to/project/node_modules/nunjucks/src/environment.js:563:19
    at eval (eval at _compile (/path/to/project/node_modules/nunjucks/src/environment.js:633:18), <anonymous>:148:12)
    at eval (eval at _compile (/path/to/project/node_modules/nunjucks/src/environment.js:633:18), <anonymous>:287:12)
    at b_content (eval at _compile (/path/to/project/node_modules/nunjucks/src/environment.js:633:18), <anonymous>:181:3)
    at eval (eval at _compile (/path/to/project/node_modules/nunjucks/src/environment.js:633:18), <anonymous>:286:28)
    at b_beforeContent (eval at _compile (/path/to/project/node_modules/nunjucks/src/environment.js:633:18), <anonymous>:302:1)
    at b_main (eval at _compile (/path/to/project/node_modules/nunjucks/src/environment.js:633:18), <anonymous>:273:34)
    at eval (eval at _compile (/path/to/project/node_modules/nunjucks/src/environment.js:633:18), <anonymous>:147:83)
    at eval (eval at _compile (/path/to/project/node_modules/nunjucks/src/environment.js:633:18), <anonymous>:108:1)

Obviously, this is not helpful, as it doesn't dig down into the actual root cause of the error.

After digging around in the source code, I discovered these lines of code:

    // The dev flag determines the trace that'll be shown on errors.
    // If set to true, returns the full trace from the error point,
    // otherwise will return trace starting from Template.render
    // (the full trace from within nunjucks may confuse developers using
    //  the library)
    // defaults to false
    opts = this.opts = opts || {};
    this.opts.dev = !!opts.dev;

This seems to suggest that if I set the (undocumented) dev option to true. I will see a more detailed stacktrace telling me the source of the error. And lo and behold, it works!

It would be nice to change this option to showTrace or similar, as folks (like me!) may want to get a fuller trace in production so tools like Sentry etc will pick this up. I'm happy to contribute, but I don't want to leave the tests uncovered and am unsure how to even test this.

I imagine it'd be a case of something like this:

  init(loaders, opts) {
    // The showTrace flag determines the trace that'll be shown on errors.
    // If set to true, returns the full trace from the error point,
    // otherwise will return trace starting from Template.render
    // (the full trace from within nunjucks may confuse developers using
    //  the library)
    // defaults to the value of `opts.dev` or `false` if `opts.dev` is
    // not present
    opts = this.opts = opts || {};

    if (opts.showTrace) {
      this.opts.showTrace = opts.showTrace;
    } else {
      this.opts.showTrace = !!opts.dev;
    }
...

And then changing all instances of this.opts.dev to this.opts.showTrace

Thoughts welcome!

pezholio added a commit to pezholio/DefinitelyTyped that referenced this issue Jan 18, 2023
There is an undocumented `dev` argument that can be sent to
`nunjucks.configure`, which allows a full error trace to the thrown

(See: mozilla/nunjucks#1430)
pezholio added a commit to ministryofjustice/hmpps-approved-premises-ui that referenced this issue Jan 18, 2023
When a global function in a template throws an error, we don’t see the
full trace of the error, which makes diagnosing issues really hard.

This makes use of the [undocumented dev argument](mozilla/nunjucks#1430)
which allows us to see the cause of the error in our logs and in Sentry.

I’ve had to extend the nunjucks types to allow for this option to be
set, as it’s not in the type definition in `DefinitelyTyped`. However,
I’ve [opened a PR to add this](DefinitelyTyped/DefinitelyTyped#63968)
pezholio added a commit to ministryofjustice/hmpps-approved-premises-ui that referenced this issue Jan 18, 2023
When a global function in a template throws an error, we don’t see the
full trace of the error, which makes diagnosing issues really hard.

This makes use of the [undocumented dev argument](mozilla/nunjucks#1430)
which allows us to see the cause of the error in our logs and in Sentry.

I’ve had to extend the nunjucks types to allow for this option to be
set, as it’s not in the type definition in `DefinitelyTyped`. However,
I’ve [opened a PR to add this](DefinitelyTyped/DefinitelyTyped#63968)
typescript-bot pushed a commit to DefinitelyTyped/DefinitelyTyped that referenced this issue Feb 15, 2023
… by @pezholio

There is an undocumented `dev` argument that can be sent to
`nunjucks.configure`, which allows a full error trace to the thrown

(See: mozilla/nunjucks#1430)
miccehedin pushed a commit to miccehedin/DefinitelyTyped that referenced this issue Feb 15, 2023
…nfigureOptions` by @pezholio

There is an undocumented `dev` argument that can be sent to
`nunjucks.configure`, which allows a full error trace to the thrown

(See: mozilla/nunjucks#1430)
typescript-bot pushed a commit to DefinitelyTyped/DefinitelyTyped that referenced this issue Feb 20, 2023
* Add two missing mode bar buttons

* Add test with scattermapbox and new button name

* Add two more button names

* Add test for new button names

* 🤖 Merge PR #64342 feat: update @nginstack/engine definitions by @Henato

Co-authored-by: Renato <[email protected]>

* 🤖 Merge PR #64343 feat: update @nginstack/iquery definitions by @Henato

Co-authored-by: Renato <[email protected]>

* 🤖 Merge PR #64344 feat: update @nginstack/orm definitions by @Henato

Co-authored-by: Renato <[email protected]>

* 🤖 Merge PR #64345 feat: update @nginstack/jsunit definitions by @Henato

Co-authored-by: Renato <[email protected]>

* 🤖 Merge PR #64346 feat: update @nginstack/datasource definitions by @Henato

Co-authored-by: Renato <[email protected]>

* 🤖 Merge PR #64348 feat: update @nginstack/dev-tools definitions by @Henato

Co-authored-by: Renato <[email protected]>

* 🤖 Merge PR #64349 feat: update @nginstack/devops definitions by @Henato

Co-authored-by: Renato <[email protected]>

* 🤖 Merge PR #64240 barnard59-validate-shacl: added ValidationError export by @tpluscode

* added ValidationError export

* added test

* 🤖 Merge PR #64331 [oidc-provider] Expose as ESM module by @BendingBender

* 🤖 Merge PR #64198 [plotly.js] Add types for updatemenus by @gjulianm

* [plotly.js] Add types for updatemenus

Added types for the updatemenus property of layout. This includes two types, UpdateMenu and UpdateMenuButton

Reference: https://plotly.com/javascript/reference/layout/updatemenus/#layout-updatemenus-items-updatemenu-visible

* [plotly] run prettier

* 🤖 Merge PR #63976 update(anchor-js): version 5 bump by @peterblazejewicz

- version 5 is breaking (as per release notes)
- option for visiblity removed
- naming alinged with module documentation (options, visiblity,
  AnchorJS, etc) with aliases
- tests updated

https://github.com/bryanbraun/anchorjs/releases/tag/5.0.0

Thanks!

* Use GH_DT_MERGEBOT_TOKEN for starting action on dt-mergebot (#64357)

* [gapi.client.*] Bulk update Google API namespace (#63835)

* 🤖 Merge PR #63909 react-native-sqlite-storage: fixing typos and adding properties by @icehaunter

1. Fixed a typo in a function name
2. Fixed return type of a function
3. Added a missing property

* 🤖 Merge PR #62388 feat(geometric): Add lineRotate method to geometric module (geometric v2.4.0) by @tmcw

* feat(geometric): Add lineRotate method

* Add ScaleX and ScaleY as well

* Fix comment style

* 🤖 Merge PR #63487 fix : distribute key and restprops in react-table component's props by @Doooooring

* fix : distribute key and restprops in react-table component's props

* fix : add semicolon in react-table

* fix : add the semicolon

* [trusted-types] Properly augment global to prevent conflicts with lib… (#64360)

* [trusted-types] Properly augment global to prevent conflicts with lib.dom.d.ts et.all

This is the proper patern to augment global interfaces that are defined by the runtime.

The previous approach would break once `lib.dom.d.ts` declares these interfaces.

The previous approach also made it impossible for other libraries to use these types.
For example, #60691 had to be reverted.
With proper module augmentation we can land #60691 again.

* $ExpectError -> @ts-expect-error

* 🤖 Merge PR #63315 [ejs] adjust options.root parameter by @mshima

* [ejs] adjust root parameter

* Update index.d.ts

* [ejs] add test for root array

* Fix EJS v8 deps

---------

Co-authored-by: Piotr Błażejewicz <[email protected]>

* 🤖 Merge PR #63879 [paypal-checkout-components] Add missing `style` and callbacks by @phylor

* Add missing `style` and callbacks to paypal-checkout-components

* Fix prettier issues

* 🤖 Merge PR #64249 meteor: Add new async methods from 2.9 by @ebroder

* 🤖 Merge PR #64363 [relay-runtime] Add cacheConfig to subscription config by @merrywhether

* 🤖 Merge PR #64337 add zhoverformat to plotly data type by @BMJHayward

Co-authored-by: Brendan Hayward <email>

* 🤖 Merge PR #64019 Fixed types for types/onfleet__node-onfleet by @YokySantiago

Fixed: property types for worker.teams, function types for task.createBatch task.autoAssign

* 🤖 Merge PR #63929 [d3-sankey] add Node.fixedValue by @simPod

* 🤖 Merge PR #63979 blessed: ListElement.setItems allows string[] by @bennbollay

* blessed: ListElement.setItems allows string[]

* Update blessed-tests.ts

* 🤖 Merge PR #63517 [react-dom] [react-native] fix incorrect typedefs for unstable_batchedUpdates by @k-yle

* 🤖 Merge PR #63877 update "wordcloud" types to align upstream by @g-plane

* update "wordcloud" types to align upstream

* update properties of "wordcloud"

* update tests of "wordcloud"

* `minFontSize` should be readonly

* update library version

* 🤖 Merge PR #63926 Update select2 Options.dropdownParent to allow additional types by @jdufresne

Per the docs at: https://select2.org/configuration/options-api
dropdownParent is allowed to be of type "jQuery selector or DOM node".

This can be further seen by the select2 code at:
https://github.com/select2/select2/blob/0a30b0b3e67843c09b0bcc4d01e65b72d9b1279f/src/js/select2/dropdown/attachBody.js#L6

* 🤖 Merge PR #63794 [@types/async] Fix return types when callback not provided by @nwalters512

* [@types/async] Fix return types when callback not provided

* Remove self as owner

* 🤖 Merge PR #63658 [ref-napi] Replace Value<T> with Pointer<T>. by @yfwz100

According to library description, a buffer is always a pointer to the underly type.

* 🤖 Merge PR #64020 fix(react-native-background-downloader): correct types for DownloadTaskState by @fivecar

It turns out that react-native-background-downloader doesn't actually export this type. Things may seem fine with doing things like `DownloadTaskState.DOWNLOADING` in your code, until you use that in a package that you also then publish yourself. At that point, you'll find the transpiled JS in your package refers to things like react_native_background_downloader_1.DownloadTaskState.DOWNLOADING, which definitely does not exist and will throw at runtime.

* 🤖 Merge PR #63968 [Nunjucks] Add `dev` argument to `ConfigureOptions` by @pezholio

There is an undocumented `dev` argument that can be sent to
`nunjucks.configure`, which allows a full error trace to the thrown

(See: mozilla/nunjucks#1430)

* 🤖 Merge PR #64073 [canvasjs] Added missing ChartOptions properties by @sampie777

Co-authored-by: S. Jansen <[email protected]>

* 🤖 Merge PR #64358 Phoenix callbacks can be async by @flupke

All callback arguments can now return T | Promise<T>, to fix annoying eslint errors like:

    Promise returned in function argument where a void return was expected. (eslint @typescript-eslint/no-misused-promises)

* 🤖 Merge PR #63921 [d3-sankey] remove unused import from tests by @simPod

---------

Co-authored-by: Henato <[email protected]>
Co-authored-by: Renato <[email protected]>
Co-authored-by: Tomasz Pluskiewicz <[email protected]>
Co-authored-by: Dimitri B <[email protected]>
Co-authored-by: Guillermo Julián <[email protected]>
Co-authored-by: Piotr Błażejewicz (Peter Blazejewicz) <[email protected]>
Co-authored-by: Nathan Shively-Sanders <[email protected]>
Co-authored-by: Maxim Mazurok <[email protected]>
Co-authored-by: Ilia Borovitinov <[email protected]>
Co-authored-by: Tom MacWright <[email protected]>
Co-authored-by: Doooooring <[email protected]>
Co-authored-by: Sebastian Silbermann <[email protected]>
Co-authored-by: Marcelo Shima <[email protected]>
Co-authored-by: Serge Hänni <[email protected]>
Co-authored-by: Evan Broder <[email protected]>
Co-authored-by: Risto Keravuori <[email protected]>
Co-authored-by: Brendan Hayward <[email protected]>
Co-authored-by: Santiago Botero Ruiz <[email protected]>
Co-authored-by: Simon Podlipsky <[email protected]>
Co-authored-by: Benn Bollay <[email protected]>
Co-authored-by: Kyℓe Hensel <[email protected]>
Co-authored-by: Pig Fang <[email protected]>
Co-authored-by: Jon Dufresne <[email protected]>
Co-authored-by: Nathan Walters <[email protected]>
Co-authored-by: Wang Zhi <[email protected]>
Co-authored-by: Philip Su <[email protected]>
Co-authored-by: Stuart Harrison <[email protected]>
Co-authored-by: S. Jansen <[email protected]>
Co-authored-by: S. Jansen <[email protected]>
Co-authored-by: Luper Rouch <[email protected]>
patrickjfl added a commit to ministryofjustice/hmpps-community-accommodation-tier-2-ui that referenced this issue Jan 16, 2024
When a global function in a template throws an error, we don’t see the
full trace of the error, which makes diagnosing issues really hard.

This makes use of the [undocumented dev argument](mozilla/nunjucks#1430)
which allows us to see the cause of the error in our logs and in Sentry.

[Copied from CAS1.](ministryofjustice/hmpps-approved-premises-ui@8eed08b)
patrickjfl added a commit to ministryofjustice/hmpps-community-accommodation-tier-2-ui that referenced this issue Jan 17, 2024
When a global function in a template throws an error, we don’t see the
full trace of the error, which makes diagnosing issues really hard.

This makes use of the [undocumented dev argument](mozilla/nunjucks#1430)
which allows us to see the cause of the error in our logs and in Sentry.

[Copied from CAS1.](ministryofjustice/hmpps-approved-premises-ui@8eed08b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant