stripe / stripe-node

Node.js library for the Stripe API.
https://stripe.com
MIT License
3.9k stars 756 forks source link

error.type does not survive minification #2042

Closed sdsanders closed 8 months ago

sdsanders commented 8 months ago

Describe the bug

When bundling and minifying using a tool like esbuild, the type included in errors thrown by Stripe is also minified into a shorter string. Strings are usually safe from minification so this behavior is unexpected.

To Reproduce

Minimal reproduction: https://github.com/sdsanders/stripe-minify-error Run npm run build && npm start - incorrect error.type will be logged Run npm run build:unminified && npm start - correct error.type will be logged

Expected behavior

Error type string should remain intact after minification.

Code snippets

No response

OS

macOs

Node version

Node v18.19.0

Library version

stripe-node

API version

2023-10-16

Additional context

Minifying might not be a super common practice server-side but it can have some advantages. We run on AWS Lambda which is usually resource constrained and minification can improve cold start times.

anniel-stripe commented 8 months ago

Hi there @sdsanders ! Thanks for the bug report and for the minimal repro. This is happening because in the error object constructor, type is set to the constructor name: https://github.com/stripe/stripe-node/blob/master/src/Error.ts#L54

A fix on our side would be to explicitly specify the type as a string in the StripeError constructor. I'll have a PR up for this soon, as it seems like it would be relatively non-intrusive.

anniel-stripe commented 8 months ago

The fix has been merged! The changes will be included in today's release.

Update: Included in v14.22.0

sdsanders commented 8 months ago

The fix has been merged! The changes will be included in today's release.

Update: Included in v14.22.0

Thanks so much! Can confirm that this fixes the issue in our setup.