javascripttypescriptcommercetools

Intermittent undefined response in commercetools SDK v3 inventory API endpoint


I'm running into a strange bug where the res value expected inside an internal function in the commercetools v3 sdk is coming back as undefined. It happens roughly half the time when making a basic get call:

const existingItem = (await getApiRoot()).inventory().withKey({ key }).get().execute()

When successful, it returns the expected { body } response with all expected values present. But randomly the execution will fail instead with the error message "Cannot read property 'error' of undefined". There's no difference between the calls or the values being passed, this is executing the code locally using the exact same key every time and it's a dice roll whether it comes back successfully or throws the error when running it multiple times in a row.

The same code using the (soon to be end-of-lifed) v2 API doesn't seem to trigger this issue, it's only after to moving to v3 that it started happening.

The error is throwing from this file within the SDK library node_modules\@commercetools\ts-client\dist\commercetools-ts-client.cjs.dev.js at line 1461, this is the block of code. Somehow res is coming back as undefined in the then statement:

const dispatch = compose(middlewares)(resolver.resolve);
  return {
    process: process$1,
    execute(request) {
      validate('exec', request);
      return new Promise((resolve, reject) => {
        dispatch({
          reject,
          resolve,
          ...request
        }).then(res => {
          if (res.error) {
            reject(res.error);
          } else {
            resolve(res);
          }
        }).catch(reject);
      });
    }
  };

After adding error logging to that block of code:

 dispatch({
      reject,
      resolve,
      ...request
    }).then(res => {
      console.log('this was the request: ', request)
      console.log('this is the res: ', typeof res, res)
      if (res.error) {
        reject(res.error);
      } else {
        resolve(res);
      }
    }).catch(reject);

we receive these logs when the error happens (sensitive values removed from request object here):

[2024-08-01T14:48:32.211Z] this was the request:  {
[2024-08-01T14:48:32.214Z]   baseUri,
[2024-08-01T14:48:32.216Z]   method: 'GET',
[2024-08-01T14:48:32.218Z]   uriTemplate: '/{projectKey}/inventory/key={key}',
[2024-08-01T14:48:32.219Z]   pathVariables: { projectKey, key },
[2024-08-01T14:48:32.221Z]   headers: {},
[2024-08-01T14:48:32.222Z]   queryParams: undefined,
[2024-08-01T14:48:32.222Z]   uri
[2024-08-01T14:48:32.223Z] }
[2024-08-01T14:48:32.223Z] this is the res:  undefined undefined
[2024-08-01T14:48:32.224Z] an issue occurred
[2024-08-01T14:48:32.225Z] Problem getting existing item Cannot read properties of undefined (reading 'error')

Has anyone else seen these intermittent failures with their inventory API or any others? The payment API call made in the same codebase seems to work consistently, the only difference being that this one queries by .withKey({ key }) and the payment one uses .withId({ ID: paymentId})


Solution

  • Are you running these Get inventory with Key requests in parallel? There seems to be an issue when running this request in parallel, where it sometimes returns as undefined. Could you reach out to commercetools support to take a deeper look into this?