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

Listen for errors globally #1846

Open
davidbielik opened this issue Jul 16, 2023 · 2 comments
Open

Listen for errors globally #1846

davidbielik opened this issue Jul 16, 2023 · 2 comments
Assignees

Comments

@davidbielik
Copy link
Contributor

davidbielik commented Jul 16, 2023

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

@richardm-stripe
Copy link
Contributor

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.

@richardm-stripe
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants