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

Implement a Error.captureStackTrace polyfill #258

Closed
0xedb opened this issue Jul 6, 2022 · 12 comments
Closed

Implement a Error.captureStackTrace polyfill #258

0xedb opened this issue Jul 6, 2022 · 12 comments
Labels
enhancement New feature or request

Comments

@0xedb
Copy link

0xedb commented Jul 6, 2022

installed express via bun add express then tried running the hello-world express example and got the following error

// express.js

// express.js
const express = require("express");
const app = express();
const port = 3000;

app.get("/", (req, res) => {
  res.send("Hello World!");
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

Error:

bun run express.js 
385 |
386 |   Error.prepareStackTrace = prepareObjectStackTrace
387 |   Error.stackTraceLimit = Math.max(10, limit)
388 |
389 |   // capture the stack
390 |   Error.captureStackTrace(obj)
      ^
 TypeError: Error.captureStackTrace is not a function. (In 'Error.captureStackTrace(obj)', 'Error.captureStackTrace' is undefined)
      at getStack (/home/b/.buffer/bun/node_modules/depd/index.js:390:2)
      at depd (/home/b/.buffer/bun/node_modules/depd/index.js:108:14)
      at /home/b/.buffer/bun/node_modules/body-parser/index.js:14:24
      at bun:wrap:1:16354
      at /home/b/.buffer/bun/node_modules/express/lib/express.js:15:25
      at bun:wrap:1:16354
      at /home/b/.buffer/bun/node_modules/express/index.js:11:25
      at bun:wrap:1:16354
      at /home/b/.buffer/bun/express.js:1:24
@Jarred-Sumner
Copy link
Collaborator

Bun needs to add a polyfill for V8's Error.captureStackTrace API. Error.captureStackTrace is specific to V8 (not a JS standard), but enough npm packages depend on it that not polyfilling it would just waste a bunch of people's time

I will rename this issue to reflect that this polyfill is necessary

@Jarred-Sumner Jarred-Sumner changed the title Error.captureStackTrace is not a function Implement a Error.captureStackTrace polyfill Jul 6, 2022
@Jarred-Sumner Jarred-Sumner added enhancement New feature or request help wanted labels Jul 6, 2022
@wbourne0
Copy link

wbourne0 commented Jul 7, 2022

Also worth noting: Error.captureStackTrace is only a part of v8's full stack trace api which also allows you to manipulate how the stack traces are rendered via Error.stackTraceLimit and Error.prepareStackTrace.

Some examples of where this is useful:

  • Applying source maps (before and after node added the --enable-source-maps option). Likely not needed with bun (which, iiuc will eventually support this natively)
  • In the nodejs REPL logic to hide callsites from the repl itself from the user (here is where that is defined; this doesn't directly modify Error.prepareStackTrace to avoid interfering with userland code, as noted here)
  • Getting references to parent functions (and also their this value) (via getThis, getFunction). It's also possible to modify the calling functions (& their this values) through this, however I haven't seen that done outside of personal projects.

The full API is documented here: https://v8.dev/docs/stack-trace-api.

On a side note, ideally bun will replicate the error formatting v8 has exactly - several error tracking libraries rely on errors being formatted this way (e.g. sentry).

@prolaxu
Copy link

prolaxu commented Jul 8, 2022

guys any updates ?.

@BeamNawapat
Copy link

any updates ?

@Jarred-Sumner
Copy link
Collaborator

Jarred-Sumner commented Jul 9, 2022 via email

@dzineer
Copy link

dzineer commented Jul 11, 2022

Good to know :)

@ianFar96
Copy link

Any updates now? I'm facing the same problem with the Kafka JS npm package

@rishavs
Copy link

rishavs commented Oct 17, 2022

Hey folks, any updates on this?

@Jarred-Sumner
Copy link
Collaborator

@Jarred-Sumner
Copy link
Collaborator

Jarred-Sumner commented Nov 9, 2022

Coming soon!

Here is a PR: #1476

@Jarred-Sumner
Copy link
Collaborator

Fixed by @dylan-conway in #1476

@rishavs
Copy link

rishavs commented Nov 9, 2022

Thank you @dylan-conway and @Jarred-Sumner <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

10 participants