facebook / facebook-nodejs-business-sdk

Node.js SDK for Meta Marketing APIs
https://developers.facebook.com/docs/business-sdk
Other
489 stars 226 forks source link

Fix issue with cursor params being reset before first succesful api request. #275

Closed gregleongf closed 6 months ago

gregleongf commented 6 months ago

Hey,

I've tried to add resiliency to the cursor requests and I've found a bug related to params being reset.

Consider the following scenario:

  1. A request is made to the server for data using a cursor object, including specific filters and fields in the params.
  2. The server responds with an error (e.g., "Service temporarily unavailable").
  3. The user attempts to retry the request by calling cursor.next(). However, instead of the request being retried with the original params (containing the specified filters and fields), the request is sent with default values. This occurs because the params associated with the cursor have been inadvertently reset after the initial request.

The root of the problem was that the cursor's params were not being properly preserved within the cursor's state, leading to the loss of specified filters and fields upon attempting a retry after an error. This made the retry attempt essentially different from the initial request, lacking the intended specificity.

To resolve this issue, I implemented changes to ensure that params are consistently retained within the cursor's state. The key modifications include:

  1. Ensuring that the paging object within the cursor is initialized with both next and params properties, the latter holding the initial request parameters.
  2. Modifying the _loadPage method to utilize this.paging.params for API calls, guaranteeing that the original parameters are used for retry requests.

By making these changes, we preserve the integrity of the cursor requests, maintaining the specified filters and fields across retry requests. The params value will be overwritten only after first successful request. This fix enhances the robustness of data fetching operations, ensuring that the cursor behaves as expected even in scenarios where server responses may be unreliable.

facebook-github-bot commented 6 months ago

Hi @gregleongf!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

facebook-github-bot commented 6 months ago

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

facebook-github-bot commented 6 months ago

@stcheng has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

stcheng commented 6 months ago

@gregleongf thank you for the pull request.

facebook-github-bot commented 6 months ago

@stcheng merged this pull request in facebook/facebook-nodejs-business-sdk@6a59270f89dbafbf45318b5829ff19f01eaca7d8.

gregleongf commented 6 months ago

@stcheng The code changes have been reversed by "Auto-generated nodejs SDK code update" https://github.com/facebook/facebook-nodejs-business-sdk/commit/3221b621833c5574a2aea74d4186e0f31c41839a