stripe-node
stripe-node copied to clipboard
Listen for errors globally
Is your feature request related to a problem? Please describe.
I would like to listen for errors in my Stripe calls so that I can log to my error tracking service.
I see there is stripe.on("response", (response) => {}) but it doesn't have the same details as the StripeError object.
I also noticed while upgrading my package version that you provide nice details like request_log_url (https://github.com/stripe/stripe-node/blob/master/CHANGELOG.md#10140---2022-10-13).
Is there a way to listen for all stripe response errors?
Describe the solution you'd like
const stripe = new Stripe(key, config);
stripe.on("error", (stripeError) => {
logError(stripeError.request_log_url);
});
Describe alternatives you've considered
No response
Additional context
No response
Hello @davidbielik, thank you for the feature request.
There is currently no good way to do this. You might be able to accomplish something close to what you need by providing a custom HttpClient.
import Stripe from "stripe";
const myHttpClient = Stripe.createNodeHttpClient()
const makeRequest = myHttpClient.makeRequest.bind(myHttpClient)
myHttpClient.makeRequest =(...args) => {
return makeRequest(...args).then((res) => {
if (res.getStatusCode() >= 400) {
callYourCustomErrorHandlerHere(res)
}
return res
})
}}
const stripe = new Stripe("sk_test_xyz", { httpClient: myHttpClient });
This will let you register an error handler using the raw HTTP responses received by stripe-node, but it will just be raw JSON here, not an instance of StripeError like you get when you .catch the result of a stripe method. You'll have access to all the data returned by the server though, so you can (await res.toJSON()).error.last_request_url if needed.
I'll leave this open as a feature request. I think a more thoughtfully-designed mechanism for "globally" extending the library / the ability to intercept/handle errors and successes at a higher level than raw http would be a good addition, but it's not something on the team's immediate roadmap.
HI @richardm-stripe, I am interested in adding this to our codebase. Is it something that sounds interesting to you? I would need some help with the design though.
A few days ago I raised my first PR for another feature addition. You can check that here. #2151