stripe / stripe-node

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

Export StripeError class #2044

Closed alko89 closed 8 months ago

alko89 commented 8 months ago

Is your feature request related to a problem? Please describe.

I'm not sure then this was changes. I'm trying to update my code to the latest stripe version and update API. Currently I'm on 11.6.0 and would like to bump to 14.21.0 and API version 2023-10-16.

I have a nest.js app where I use the error filter middleware to catch potential Stripe errors, seems like the new version doesn't export the StripeError class anymore:

import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common';
import { Response } from 'express';
import { StripeError } from 'stripe/lib/Error';

@Catch(StripeError)
export class StripeErrorFilter implements ExceptionFilter {
  catch(exception: StripeError, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();

    response.status(exception.statusCode).json({
      name: exception.code,
      message: exception.message,
    });
  }
}

Describe the solution you'd like

Export StripeError to allow catching Stripe errors thrown inside the library.

Describe alternatives you've considered

Remain on the old version.

Additional context

No response

pakrym-stripe commented 8 months ago

In the latest version of the library, you can reference the error type via Stripe.errors.StripeError.

alko89 commented 8 months ago

Perfect! Seems to be just what I need. Thanks @pakrym-stripe