Title: async-wrap: no way to catch errors without changing the throw origin · Issue #669 · nodejs/node · GitHub
Open Graph Title: async-wrap: no way to catch errors without changing the throw origin · Issue #669 · nodejs/node
X Title: async-wrap: no way to catch errors without changing the throw origin · Issue #669 · nodejs/node
Description: Consider some long stack trace module. It can use async_wrap to manage the the callSite objects correctly and use the v8 Error hooks to modify the .stack property. However because v8 sets the .stack property in a lazy way, it is necessar...
Open Graph Description: Consider some long stack trace module. It can use async_wrap to manage the the callSite objects correctly and use the v8 Error hooks to modify the .stack property. However because v8 sets the .stac...
X Description: Consider some long stack trace module. It can use async_wrap to manage the the callSite objects correctly and use the v8 Error hooks to modify the .stack property. However because v8 sets the .stac...
Opengraph URL: https://github.com/nodejs/node/issues/669
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"async-wrap: no way to catch errors without changing the throw origin","articleBody":"Consider some long stack trace module. It can use `async_wrap` to manage the the callSite objects correctly and use the v8 `Error` hooks to modify the `.stack` property. However because v8 sets the `.stack` property in a lazy way, it is necessary to do a `try {} finally {}` around the callback.\n\nSee https://github.com/AndreasMadsen/trace/blob/master/trace.js#L53 for an example with the `tracing` module.\n\nAn okay solution solution is to use the `uncaughtException` event like this:\n\n``` js\nvar asyncWrap = process.binding('async_wrap');\n\n// Enable asyncWrap and call init hook function on async initialization\nvar asyncHooksObject = {};\nvar kCallInitHook = 0;\nasyncWrap.setupHooks(\n asyncHooksObject,\n asyncFunctionInitialized,\n asyncCallbackBefore,\n asyncCallbackAfter);\nasyncHooksObject[kCallInitHook] = 1;\n\nfunction asyncFunctionInitialized() {}\n\nfunction asyncCallbackBefore() {\n process.once('uncaughtException', asyncCallbackError);\n}\n\nfunction asyncCallbackError(error) {\n // Set stack by v8 magic\n error.stack;\n\n // changes throw origin, should not be necessary\n throw error;\n}\n\nfunction asyncCallbackAfter() {\n process.removeListener('uncaughtException', asyncCallbackError);\n}\n\nsetTimeout(function () {\n badluck();\n}, 10);\n```\n\nHowever this changes the throw origin:\n\n```\n/Users/Andreas/Sites/node_modules/trace/test.js:26\n throw error;\n ^\nReferenceError: badluck is not defined\n at null._onTimeout (/Users/Andreas/Sites/node_modules/trace/test.js:34:3)\n at Timer.listOnTimeout (timers.js:90:15)\n```\n\nIf `uncaughtException` isn't used then this is the error:\n\n```\n/Users/Andreas/Sites/node_modules/trace/test.js:34\n badluck();\n ^\nReferenceError: badluck is not defined\n at null._onTimeout (/Users/Andreas/Sites/node_modules/trace/test.js:34:3)\n at Timer.listOnTimeout (timers.js:90:15)\n```\n\nThis is much more informative. It would be really nice if `async_wrap` or some other mechanism allowed something similar to the old `tracing.addAsyncListener({ error: handler })` behaviour. Such that the throw origin can be preserved.\n\n_issue tracking: https://github.com/AndreasMadsen/trace/issues/12_\n_issue tracking: https://github.com/iojs/tracing-wg/issues/7_\n","author":{"url":"https://github.com/AndreasMadsen","@type":"Person","name":"AndreasMadsen"},"datePublished":"2015-01-30T17:47:08.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":18},"url":"https://github.com/669/node/issues/669"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:4e60036e-8bb5-f0c4-2fa2-79725b844e7c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E89E:20853D:357D8AC:4913A61:6A4F14FF |
| html-safe-nonce | eaabe14df0a13f788e16e1e594e94dd25c2d1ab13dd46b98490a1ac18da966d3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFODlFOjIwODUzRDozNTdEOEFDOjQ5MTNBNjE6NkE0RjE0RkYiLCJ2aXNpdG9yX2lkIjoiMTk3MTY5MzY2MDg4NzUwMzM1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 0d04016b19f1cc4aa2ec85e401a95a096d2e0983be0eb22e47c697a087d53de8 |
| hovercard-subject-tag | issue:56063090 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/669/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f02049cadf500d16e8ed9b369619cdadd6b70233f047d4b9c6131062111a17bb/nodejs/node/issues/669 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f02049cadf500d16e8ed9b369619cdadd6b70233f047d4b9c6131062111a17bb/nodejs/node/issues/669 |
| og:image:alt | Consider some long stack trace module. It can use async_wrap to manage the the callSite objects correctly and use the v8 Error hooks to modify the .stack property. However because v8 sets the .stac... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | AndreasMadsen |
| hostname | github.com |
| expected-hostname | github.com |
| None | b92d11c0aa4a77d54ef4af1078b6a15fb5a70a215b30c4ecf28889d5a8e656d9 |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2b8f23afb982271f1b22258a94aede67a6b77760 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width