Skip to content

Preserve stack traces for async operations#2688

Merged
xavdid merged 3 commits intomasterfrom
node-stacktraces
Apr 17, 2026
Merged

Preserve stack traces for async operations#2688
xavdid merged 3 commits intomasterfrom
node-stacktraces

Conversation

@xavdid
Copy link
Copy Markdown
Member

@xavdid xavdid commented Apr 17, 2026

Why?

Because of the way the internals of the SDK's async HTTP calling code are structured, when we throw an error, it loses its original stack trace. Users have only been able to trace issues back to where our HTTP calls originate from, not where in their code caused the HTTP call that failed.

The best solution is to use promises all the way down our HTTP chain, which will let node resolve stack traces as we'd expect. But that's a pretty big internal change and is part of why the team hadn't prioritized it in the past.

We talked it over this week and decided to take a more tactical approach: by manually instantiating an unthrown error before doing any async operations, we can reference its call stack if the async operation later fails. That gives us pretty usable tracebacks (seen below). I've also manually confirmed that it works for autoPagingEach and autoPagingToArray.

As far as I can tell, we didn't try this before because of perceived performance implications, but based on local testing with real API calls, the difference is negligible (approx. 30ms / request, which is indistinguishable from network jitter). We'll still probably go through and simplify this code in the future, but this is a reasonable stopgap until we get to that.

before:

Error: Invalid API Key provided: sk_test_fake
    at generateV1Error (/Users/xavdid/stripe/hello-sdks/node/node_modules/stripe/src/Error.ts:27:12)
    at <anonymous> (/Users/xavdid/stripe/hello-sdks/node/node_modules/stripe/src/RequestSender.ts:194:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:103:5)

after:

Error: Invalid API Key provided: sk_test_fake
    at generateV1Error (/Users/xavdid/stripe/hello-sdks/node/node_modules/stripe/src/Error.ts:27:12)
    at <anonymous> (/Users/xavdid/stripe/hello-sdks/node/node_modules/stripe/src/RequestSender.ts:194:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:103:5)
Originating from:
    at CustomerResource._makeRequest (/Users/xavdid/stripe/hello-sdks/node/node_modules/stripe/src/StripeResource.ts:97:27)
    at CustomerResource.list (/Users/xavdid/stripe/hello-sdks/node/node_modules/stripe/src/resources/Customers.ts:207:17)
    at listCustomers (/Users/xavdid/stripe/hello-sdks/node/index.ts:9:52)
    at <anonymous> (/Users/xavdid/stripe/hello-sdks/node/index.ts:20:7)
    at ModuleJob.run (node:internal/modules/esm/module_job:430:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:661:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5)

What?

  • manually capture tracebacks before performing async operations. Then, bind that stack to the error if one is thrown (in 3 places)
  • tests

See Also

@xavdid xavdid marked this pull request as ready for review April 17, 2026 00:38
@xavdid xavdid requested a review from a team as a code owner April 17, 2026 00:38
@xavdid xavdid requested review from jar-stripe and prathmesh-stripe and removed request for a team and prathmesh-stripe April 17, 2026 00:38
Copy link
Copy Markdown
Contributor

@jar-stripe jar-stripe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have a utility for getting the stack, given there are a few places use that. That's the biggest requested change, the others would be nice to consider/test for.

Comment thread src/autoPagination.ts
Comment thread src/autoPagination.ts Outdated
Comment thread src/utils.ts Outdated
@xavdid xavdid changed the title generate stack traces before async operations Preserve stack traces for async operations Apr 17, 2026
@xavdid xavdid enabled auto-merge (squash) April 17, 2026 17:32
@xavdid xavdid merged commit b7d6f88 into master Apr 17, 2026
9 checks passed
@xavdid xavdid deleted the node-stacktraces branch April 17, 2026 21:33
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

Successfully merging this pull request may close these issues.

2 participants