FG
๐Ÿ”Œ APIs & SDKsStripe

Requests not Throwing Errors in NestJS App or AWS Lambda Node 20/18.x

Freshabout 21 hours ago
Mar 14, 20260 views
Confidence Score54%
54%

Problem

Describe the bug Within NestJS, when performing calls such as `StripeClient.invoices.create` or other operations with bad data, requests hang indefinitely and don't throw errors. To Reproduce 1. Instantiate Stripe Client with `new Stripe(<API_KEY>)` 2. Wrap API call in `try/catch` block 3. Call `invoices.create` method with an invalid payload (Such as the one in the snippet) 4. Call will hang indefinitely neither erroring or succeeding Expected behavior When an API call fails on any non `2xx` status, it should throw an exception that can be caught by a `try/catch` block or a `.catch`. Code snippets [code block] OS Running on Docker `linux/amd64 node:20-alpine` Node version Node v20.18.0 Library version stripe node version 17.3.0 API version Default (Whatever you get when you don't specify any) Additional context Noticed running in a NestJS Application running in docker. I've tried: - Running requests through an Injectable Service that instantiates Stripe - Running the client directly in the Controller, or Service - Attempting to pass `httpClient` property in `new Stripe` with Axios and NestJS default HTTP Clients, to no avail. I also tried writing a script with the exact same code on the exact same version of node on a loose `.ts` file and running it with `ts-node` and it worked. I'll keep plowing through as it is critical for me to know when Stripe operations fail. If I find a solution, I will post it here.

Error Output

exception that can be caught by a `try/catch` block or a `.catch`.

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Implement Timeout for Stripe API Calls in NestJS

Medium Risk

The issue arises because the Stripe API calls do not handle certain types of invalid payloads correctly, leading to indefinite hanging without throwing errors. This can happen due to unhandled promise rejections or network issues. By implementing a timeout mechanism, we can ensure that the requests do not hang indefinitely and can throw an error after a specified duration.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Install Axios Timeout Package

    To implement a timeout for the requests, we will use the Axios library, which allows us to set a timeout for HTTP requests. First, ensure that Axios is installed in your NestJS project.

    bash
    npm install axios
  2. 2

    Create a Custom Axios Instance with Timeout

    Create a custom Axios instance that includes a timeout setting. This instance will be used for making Stripe API calls.

    typescript
    import axios from 'axios';
    
    const axiosInstance = axios.create({
      timeout: 5000 // Set timeout to 5 seconds
    });
  3. 3

    Integrate Axios Instance with Stripe Client

    Pass the custom Axios instance to the Stripe client during instantiation, ensuring that all API calls made through this client will respect the timeout setting.

    typescript
    import Stripe from 'stripe';
    
    const stripe = new Stripe('<API_KEY>', {
      httpClient: axiosInstance
    });
  4. 4

    Handle Errors in API Calls

    Wrap your API calls in a try/catch block to handle errors properly. This will ensure that any errors thrown due to timeouts or invalid payloads are caught and can be managed accordingly.

    typescript
    try {
      const invoice = await stripe.invoices.create(invalidPayload);
    } catch (error) {
      console.error('Error creating invoice:', error);
      // Handle error appropriately
    }
  5. 5

    Test the Implementation

    Run your NestJS application and test the Stripe API calls with invalid payloads to ensure that errors are thrown and handled correctly. Verify that the application no longer hangs indefinitely.

Validation

Confirm that the application no longer hangs when making Stripe API calls with invalid payloads. Ensure that errors are logged and handled as expected within the try/catch block.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

stripepaymentsapibug