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

fix(createError): preserve original error stack #161

Merged
merged 4 commits into from Aug 10, 2022
Merged

fix(createError): preserve original error stack #161

merged 4 commits into from Aug 10, 2022

Conversation

danielroe
Copy link
Member

Currently errors logged by h3 always point to h3 itself rather than the source of the error, e.g.:

[h3] [unhandled] H3Error: window is not defined
    at createError (file:///private/tmp/vueqr/node_modules/.pnpm/h3@0.7.14/node_modules/h3/dist/index.mjs:238:15)
    at Server.nodeHandler (file:///private/tmp/vueqr/node_modules/.pnpm/h3@0.7.14/node_modules/h3/dist/index.mjs:431:21) {
  statusCode: 500,
  fatal: false,
  unhandled: true,
  statusMessage: 'Internal Server Error'
}
[nuxt] [request error] window is not defined
  at createError (./node_modules/.pnpm/h3@0.7.14/node_modules/h3/dist/index.mjs:238:15)  
  at Server.nodeHandler (./node_modules/.pnpm/h3@0.7.14/node_modules/h3/dist/index.mjs:431:21)

This PR changes it to the original stack trace:

[h3] [unhandled] ReferenceError: window is not defined
    at $id_aX1WMBYGcH (file:///private/tmp/vueqr/.nuxt/dist/server/server.mjs:4520:5)
    at async __instantiateModule__ (file:///private/tmp/vueqr/.nuxt/dist/server/server.mjs:8426:3) {
  statusCode: 500,
  fatal: false,
  unhandled: true,
  statusMessage: 'Internal Server Error'
}
[nuxt] [request error] window is not defined
  at $id_aX1WMBYGcH (./.nuxt/dist/server/server.mjs:4520:5)  
  at async __instantiateModule__ (./.nuxt/dist/server/server.mjs:8426:3)

@danielroe danielroe added the bug Something isn't working label Aug 10, 2022
@danielroe danielroe requested a review from pi0 August 10, 2022 13:35
@danielroe danielroe self-assigned this Aug 10, 2022
@codecov
Copy link

codecov bot commented Aug 10, 2022

Codecov Report

Merging #161 (a8a0d98) into main (ccc9c7e) will decrease coverage by 0.13%.
The diff coverage is 50.00%.

❗ Current head a8a0d98 differs from pull request most recent head 19baa61. Consider uploading reports for the commit 19baa61 to get more accurate results

@@            Coverage Diff             @@
##             main     #161      +/-   ##
==========================================
- Coverage   65.47%   65.33%   -0.14%     
==========================================
  Files          14       14              
  Lines         921      929       +8     
  Branches      200      202       +2     
==========================================
+ Hits          603      607       +4     
- Misses        123      125       +2     
- Partials      195      197       +2     
Impacted Files Coverage Δ
src/error.ts 68.08% <50.00%> (-1.69%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@pi0
Copy link
Member

pi0 commented Aug 10, 2022

Would you please provide a (nitro) reproduction how do you throw errors? I think we might use a simpler method but need to try.

@danielroe
Copy link
Member Author

https://stackblitz.com/edit/github-p8e2d2?file=routes%2Findex.ts

@pi0 pi0 changed the title fix: preserve original error stack fix(createError): preserve original error stack Aug 10, 2022
@pi0
Copy link
Member

pi0 commented Aug 10, 2022

Update:

  • I tried with v8 native Error.captureStacktrace with no luck (might try again later)
  • Added implementation to use a lazy getter for input.stack also try catch to avoid runtime errors. V8 at least, generates stack trace string when this property is being accessed which is why doing this to delay and only access when needed by runtime

@pi0 pi0 merged commit 8213421 into main Aug 10, 2022
@pi0 pi0 deleted the fix/error-stack branch August 10, 2022 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants