bun icon indicating copy to clipboard operation
bun copied to clipboard

Implement a `Error.captureStackTrace` polyfill

Open 0xedb opened this issue 2 years ago • 9 comments

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

0xedb avatar Jul 06 '22 10:07 0xedb

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 avatar Jul 06 '22 11:07 Jarred-Sumner

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).

wbourne0 avatar Jul 07 '22 06:07 wbourne0

guys any updates ?.

prolaxu avatar Jul 08 '22 14:07 prolaxu

any updates ?

BeamNawapat avatar Jul 09 '22 16:07 BeamNawapat

Yes. @evanwashere is actively working on express support as I write this.

On Sat, Jul 9, 2022 at 9:11 AM Nawapat Buakoet @.***> wrote:

any updates ?

— Reply to this email directly, view it on GitHub https://github.com/Jarred-Sumner/bun/issues/258#issuecomment-1179567726, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFNGS4YRWZR54VTJFE657DVTGQBLANCNFSM52ZKPEVA . You are receiving this because you commented.Message ID: @.***>

Jarred-Sumner avatar Jul 09 '22 16:07 Jarred-Sumner

Good to know :)

dzineer avatar Jul 11 '22 18:07 dzineer

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

ianFar96 avatar Sep 16 '22 22:09 ianFar96

Hey folks, any updates on this?

rishavs avatar Oct 17 '22 09:10 rishavs

Here is a reference implementation in JSC from a few years back:

https://github.com/mceSystems/node-jsc/blob/90634f3064fab8e89a85b3942f0cc5054acc86fa/deps/jscshim/src/shim/GlobalObject.cpp#L350-L464

Jarred-Sumner avatar Oct 18 '22 05:10 Jarred-Sumner

Coming soon!

Here is a PR: https://github.com/oven-sh/bun/pull/1476

Jarred-Sumner avatar Nov 09 '22 06:11 Jarred-Sumner

Fixed by @dylan-conway in #1476

Jarred-Sumner avatar Nov 09 '22 09:11 Jarred-Sumner

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

rishavs avatar Nov 09 '22 20:11 rishavs