Skip to content

Commit

Permalink
fix: adding throttled status code for server unavailable error in sal…
Browse files Browse the repository at this point in the history
…esforce (#3862)

* fix: adding throttled status code for server unavailable in salesforce

* fix: adding doc ref

* fix: adding doc ref

* fix: adding error message dynamically
  • Loading branch information
shrouti1507 authored Nov 11, 2024
1 parent f959a7d commit fa93f09
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
45 changes: 36 additions & 9 deletions src/v0/destinations/salesforce/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@ const {

const ACCESS_TOKEN_CACHE = new Cache(ACCESS_TOKEN_CACHE_TTL);

/**
* Extracts and returns the error message from a response object.
* If the response is an array and contains a message in the first element,
* it returns that message. Otherwise, it returns the stringified response.
* Error Message Format Example: ref: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/errorcodes.htm#:~:text=Incorrect%20ID%20example
[
{
"fields" : [ "Id" ],
"message" : "Account ID: id value of incorrect type: 001900K0001pPuOAAU",
"errorCode" : "MALFORMED_ID"
}
]
* @param {Object|Array} response - The response object or array to extract the message from.
* @returns {string} The extracted error message or the stringified response.
*/

const getErrorMessage = (response) => {
if (Array.isArray(response) && response?.[0]?.message && response?.[0]?.message?.length > 0) {
return response[0].message;
}
return JSON.stringify(response);
};

/**
* ref: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/errorcodes.htm
* handles Salesforce application level failures
Expand Down Expand Up @@ -77,15 +100,19 @@ const salesforceResponseHandler = (destResponse, sourceMessage, authKey, authori
} else if (status === 503 || status === 500) {
// The salesforce server is unavailable to handle the request. Typically this occurs if the server is down
// for maintenance or is currently overloaded.
throw new RetryableError(
`${DESTINATION} Request Failed - due to "${
response && Array.isArray(response) && response[0]?.message?.length > 0
? response[0].message
: JSON.stringify(response)
}", (Retryable) ${sourceMessage}`,
500,
destResponse,
);
// ref : https://help.salesforce.com/s/articleView?id=000387190&type=1
if (matchErrorCode('SERVER_UNAVAILABLE')) {
throw new ThrottledError(
`${DESTINATION} Request Failed: ${status} - due to ${getErrorMessage(response)}, ${sourceMessage}`,
destResponse,
);
} else {
throw new RetryableError(
`${DESTINATION} Request Failed: ${status} - due to "${getErrorMessage(response)}", (Retryable) ${sourceMessage}`,
500,
destResponse,
);
}
}
// check the error message
let errorMessage = '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export const testScenariosForV1API: ProxyV1TestData[] = [
output: {
status: 500,
message:
'Salesforce Request Failed - due to "Session expired or invalid", (Retryable) during Salesforce Response Handling',
'Salesforce Request Failed: 500 - due to "Session expired or invalid", (Retryable) during Salesforce Response Handling',
response: [
{
error:
Expand Down Expand Up @@ -277,16 +277,16 @@ export const testScenariosForV1API: ProxyV1TestData[] = [
body: {
output: {
message:
'Salesforce Request Failed - due to "Server Unavailable", (Retryable) during Salesforce Response Handling',
'Salesforce Request Failed: 503 - due to Server Unavailable, during Salesforce Response Handling',
response: [
{
error: '[{"message":"Server Unavailable","errorCode":"SERVER_UNAVAILABLE"}]',
metadata: proxyMetdata,
statusCode: 500,
statusCode: 429,
},
],
statTags: statTags.retryable,
status: 500,
statTags: statTags.throttled,
status: 429,
},
},
},
Expand Down
14 changes: 7 additions & 7 deletions test/integrations/destinations/salesforce/dataDelivery/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,12 @@ const legacyTests = [
},
output: {
response: {
status: 500,
status: 429,
body: {
output: {
status: 500,
status: 429,
message:
'Salesforce Request Failed - due to "Server Unavailable", (Retryable) during Salesforce Response Handling',
'Salesforce Request Failed: 503 - due to Server Unavailable, during Salesforce Response Handling',
destinationResponse: {
response: [
{
Expand All @@ -334,7 +334,7 @@ const legacyTests = [
errorCategory: 'network',
destinationId: 'Non-determininable',
workspaceId: 'Non-determininable',
errorType: 'retryable',
errorType: 'throttled',
feature: 'dataDelivery',
implementation: 'native',
module: 'destination',
Expand Down Expand Up @@ -461,7 +461,7 @@ const legacyTests = [
status: 503,
},
message:
'Salesforce Request Failed - due to "{"message":"Server Unavailable","errorCode":"SERVER_UNAVAILABLE"}", (Retryable) during Salesforce Response Handling',
'Salesforce Request Failed: 503 - due to "{"message":"Server Unavailable","errorCode":"SERVER_UNAVAILABLE"}", (Retryable) during Salesforce Response Handling',
statTags: {
destType: 'SALESFORCE',
errorCategory: 'network',
Expand Down Expand Up @@ -603,7 +603,7 @@ const legacyTests = [
output: {
status: 500,
message:
'Salesforce Request Failed - due to ""[ECONNABORTED] :: Connection aborted"", (Retryable) during Salesforce Response Handling',
'Salesforce Request Failed: 500 - due to ""[ECONNABORTED] :: Connection aborted"", (Retryable) during Salesforce Response Handling',
destinationResponse: {
response: '[ECONNABORTED] :: Connection aborted',
status: 500,
Expand Down Expand Up @@ -696,7 +696,7 @@ const legacyTests = [
output: {
status: 500,
message:
'Salesforce Request Failed - due to ""[EAI_AGAIN] :: Temporary failure in name resolution"", (Retryable) during Salesforce Response Handling',
'Salesforce Request Failed: 500 - due to ""[EAI_AGAIN] :: Temporary failure in name resolution"", (Retryable) during Salesforce Response Handling',
destinationResponse: {
response: '[EAI_AGAIN] :: Temporary failure in name resolution',
status: 500,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const otherSalesforceScenariosV1: ProxyV1TestData[] = [
],
statTags,
message:
'Salesforce Request Failed - due to "{"error":{"message":"Service Unavailable","description":"The server is currently unable to handle the request due to temporary overloading or maintenance of the server. Please try again later."}}", (Retryable) during Salesforce Response Handling',
'Salesforce Request Failed: 503 - due to "{"error":{"message":"Service Unavailable","description":"The server is currently unable to handle the request due to temporary overloading or maintenance of the server. Please try again later."}}", (Retryable) during Salesforce Response Handling',
status: 500,
},
},
Expand Down Expand Up @@ -96,7 +96,7 @@ export const otherSalesforceScenariosV1: ProxyV1TestData[] = [
],
statTags,
message:
'Salesforce Request Failed - due to ""Internal Server Error"", (Retryable) during Salesforce Response Handling',
'Salesforce Request Failed: 500 - due to ""Internal Server Error"", (Retryable) during Salesforce Response Handling',
status: 500,
},
},
Expand Down

0 comments on commit fa93f09

Please sign in to comment.