Articles in this section

Handling errors in exports with preSavePage scripts

Managing errors as they arise in exports is crucial to ensuring that your data continues to flow smoothly. The preSavePage script allows you to customize how the Celigo platform handles errors before the export data moves downstream. Here are some common scenarios.

Scenario 1: Handling API call failures

If an API call fails, the data array is usually empty, and the errors array usually contains one or more errors that reflect the error returned by the export API. In such cases, you can modify the error message before sending the data forward.

Here’s an example of how to modify a specific error message within the preSavePage script:

function preSavePage(options) {
  if (options.errors.length > 0) {
    for (let e of options.errors) {
      if (
        e.code == "404" &&
        e.message ==
          '{"errors":["The requested URL or resource could not be found."],"error_type":"not_found"}'
      ) {
        e.message = "Insert new error message here.";
      }
    }
  }
  return {
    data: options.data,
    errors: options.errors,
    abort: false,
    newErrorsAndRetryData: [],
  };
}

The script above replaces a 404 error that contains a specific message with your customized error message text. This is useful for logging or user communication purposes.

Scenario 2: Handling errors while allowing the flow to continue processing data

If the API call is successful but some records have errors, instead of allowing a single record's error to cause the entire page of records to fail, you can use the newErrorsAndRetryData and data arrays to handle the errant records without disrupting the entire flow. .

  • data: This array contains the records that do not have errors and should continue processing.

  • newErrorsAndRetryData: This array contains the records that encountered errors and their associated error messages. You can retry the errant records later without preventing the other records from processing.

Here’s an example script that separates errant records from those that should continue processing:

function preSavePage(options) {
  let data = [];
  let newErrorsAndRetryData = [];

  for (let d of options.data) {
    if (d.isBad) {
      newErrorsAndRetryData.push({
        retryData: d,
        errors: [
          {
            message: "This record has errors.",
          },
        ],
      });
    } else {
      data.push(d);
    }
  }

  return {
    data: data,
    errors: options.errors,
    abort: false,
    newErrorsAndRetryData: newErrorsAndRetryData,
  };
}

The above script:

  • pushes records that encounter errors into the newErrorsAndRetryData array along with the corresponding error message. You can retry them later without disrupting the flow.

  • pushes records that do not have issues to the data array so they can continue processing through the flow.

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.