Skip to content

Commit

Permalink
fix(core): Deduplicate error handling in nodes (#4319)
Browse files Browse the repository at this point in the history
* Fix issue with isAxios property

* 馃 Deduplicate errors handeling improve errors for credentials

* 馃帹 correctly throws error
  • Loading branch information
agobrech committed Nov 11, 2022
1 parent d35d63a commit c7133ec
Show file tree
Hide file tree
Showing 35 changed files with 50 additions and 187 deletions.
6 changes: 1 addition & 5 deletions packages/nodes-base/nodes/ActionNetwork/GenericFunctions.ts
Expand Up @@ -39,11 +39,7 @@ export async function actionNetworkApiRequest(
delete options.qs;
}

try {
return await this.helpers.requestWithAuthentication.call(this, 'actionNetworkApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
return await this.helpers.requestWithAuthentication.call(this, 'actionNetworkApi', options);
}

export async function handleListing(
Expand Down
6 changes: 1 addition & 5 deletions packages/nodes-base/nodes/Airtable/GenericFunctions.ts
Expand Up @@ -61,11 +61,7 @@ export async function apiRequest(
delete options.body;
}

try {
return await this.helpers.requestWithAuthentication.call(this, 'airtableApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
return await this.helpers.requestWithAuthentication.call(this, 'airtableApi', options);
}

/**
Expand Down
7 changes: 2 additions & 5 deletions packages/nodes-base/nodes/Aws/Comprehend/GenericFunctions.ts
Expand Up @@ -7,6 +7,7 @@ import {
IWebhookFunctions,
} from 'n8n-core';


import { IHttpRequestOptions, NodeApiError } from 'n8n-workflow';

export async function awsApiRequest(
Expand All @@ -31,11 +32,7 @@ export async function awsApiRequest(
headers,
region: credentials?.region as string,
} as IHttpRequestOptions;
try {
return await this.helpers.requestWithAuthentication.call(this, 'aws', requestOptions);
} catch (error) {
throw new NodeApiError(this.getNode(), error); // no XML parsing needed
}
return await this.helpers.requestWithAuthentication.call(this, 'aws', requestOptions);
}

export async function awsApiRequestREST(
Expand Down
Expand Up @@ -42,11 +42,7 @@ export async function awsApiRequest(
if (Object.keys(option).length !== 0) {
Object.assign(requestOptions, option);
}
try {
return await this.helpers.requestWithAuthentication.call(this, 'aws', requestOptions);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
return await this.helpers.requestWithAuthentication.call(this, 'aws', requestOptions);
}

export async function awsApiRequestREST(
Expand Down
6 changes: 1 addition & 5 deletions packages/nodes-base/nodes/Aws/S3/GenericFunctions.ts
Expand Up @@ -40,11 +40,7 @@ export async function awsApiRequest(
if (Object.keys(option).length !== 0) {
Object.assign(requestOptions, option);
}
try {
return await this.helpers.requestWithAuthentication.call(this, 'aws', requestOptions);
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
}
return await this.helpers.requestWithAuthentication.call(this, 'aws', requestOptions);
}

export async function awsApiRequestREST(
Expand Down
6 changes: 1 addition & 5 deletions packages/nodes-base/nodes/Beeminder/GenericFunctions.ts
Expand Up @@ -31,11 +31,7 @@ export async function beeminderApiRequest(
delete options.qs;
}

try {
return await this.helpers.requestWithAuthentication.call(this, 'beeminderApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
return await this.helpers.requestWithAuthentication.call(this, 'beeminderApi', options);
}

export async function beeminderApiRequestAllItems(
Expand Down
6 changes: 1 addition & 5 deletions packages/nodes-base/nodes/Calendly/GenericFunctions.ts
Expand Up @@ -53,11 +53,7 @@ export async function calendlyApiRequest(
delete options.qs;
}
options = Object.assign({}, options, option);
try {
return await this.helpers.requestWithAuthentication.call(this, 'calendlyApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
return await this.helpers.requestWithAuthentication.call(this, 'calendlyApi', options);
}

export function getAuthenticationType(data: string): 'accessToken' | 'apiKey' {
Expand Down
7 changes: 1 addition & 6 deletions packages/nodes-base/nodes/Clockify/GenericFunctions.ts
Expand Up @@ -28,12 +28,7 @@ export async function clockifyApiRequest(
json: true,
useQuerystring: true,
};

try {
return await this.helpers.requestWithAuthentication.call(this, 'clockifyApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
return await this.helpers.requestWithAuthentication.call(this, 'clockifyApi', options);
}

export async function clockifyApiRequestAllItems(
Expand Down
6 changes: 1 addition & 5 deletions packages/nodes-base/nodes/Cortex/GenericFunctions.ts
Expand Up @@ -42,11 +42,7 @@ export async function cortexApiRequest(
delete options.qs;
}

try {
return await this.helpers.requestWithAuthentication.call(this, 'cortexApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
return await this.helpers.requestWithAuthentication.call(this, 'cortexApi', options);
}

export function getEntityLabel(entity: IDataObject): string {
Expand Down
6 changes: 1 addition & 5 deletions packages/nodes-base/nodes/CustomerIo/GenericFunctions.ts
Expand Up @@ -32,11 +32,7 @@ export async function customerIoApiRequest(
options.url = `https://beta-api.customer.io/v1/api${endpoint}`;
}

try {
return await this.helpers.requestWithAuthentication.call(this, 'customerIoApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
return await this.helpers.requestWithAuthentication.call(this, 'customerIoApi', options);
}

export function eventExists(currentEvents: string[], webhookEvents: IDataObject) {
Expand Down
6 changes: 1 addition & 5 deletions packages/nodes-base/nodes/Dropcontact/GenericFunction.ts
Expand Up @@ -30,9 +30,5 @@ export async function dropcontactApiRequest(
delete options.qs;
}

try {
return await this.helpers.requestWithAuthentication.call(this, 'dropcontactApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
return await this.helpers.requestWithAuthentication.call(this, 'dropcontactApi', options);
}
3 changes: 1 addition & 2 deletions packages/nodes-base/nodes/ERPNext/GenericFunctions.ts
Expand Up @@ -50,8 +50,7 @@ export async function erpNextApiRequest(
message: 'Please ensure the subdomain is correct.',
});
}

throw new NodeApiError(this.getNode(), error);
throw error;
}
}

Expand Down
6 changes: 1 addition & 5 deletions packages/nodes-base/nodes/Ghost/GenericFunctions.ts
Expand Up @@ -44,11 +44,7 @@ export async function ghostApiRequest(
json: true,
};

try {
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
}
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
}

export async function ghostApiRequestAllItems(
Expand Down
8 changes: 1 addition & 7 deletions packages/nodes-base/nodes/HighLevel/GenericFunctions.ts
Expand Up @@ -232,13 +232,7 @@ export async function highLevelApiRequest(
delete options.qs;
}
options = Object.assign({}, options, option);
try {
return await this.helpers.requestWithAuthentication.call(this, 'highLevelApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error, {
message: error.message,
});
}
return await this.helpers.requestWithAuthentication.call(this, 'highLevelApi', options);
}

export async function getPipelineStages(
Expand Down
6 changes: 1 addition & 5 deletions packages/nodes-base/nodes/Jira/GenericFunctions.ts
Expand Up @@ -58,11 +58,7 @@ export async function jiraSoftwareCloudApiRequest(
delete options.qs;
}

try {
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
}
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
}

export async function jiraSoftwareCloudApiRequestAllItems(
Expand Down
10 changes: 2 additions & 8 deletions packages/nodes-base/nodes/Lemlist/GenericFunctions.ts
Expand Up @@ -17,10 +17,8 @@ export async function lemlistApiRequest(
qs: IDataObject = {},
option: IDataObject = {},
) {

const options: OptionsWithUri = {
headers: {
},
headers: {},
method,
uri: `https://api.lemlist.com/api${endpoint}`,
qs,
Expand All @@ -40,11 +38,7 @@ export async function lemlistApiRequest(
Object.assign(options, option);
}

try {
return await this.helpers.requestWithAuthentication.call(this,'lemlistApi',options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
return await this.helpers.requestWithAuthentication.call(this, 'lemlistApi', options);
}

/**
Expand Down
6 changes: 1 addition & 5 deletions packages/nodes-base/nodes/Mailjet/GenericFunctions.ts
Expand Up @@ -48,11 +48,7 @@ export async function mailjetApiRequest(
delete options.body;
}

try {
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
}
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
}

export async function mailjetApiRequestAllItems(
Expand Down
7 changes: 1 addition & 6 deletions packages/nodes-base/nodes/NextCloud/GenericFunctions.ts
Expand Up @@ -54,10 +54,5 @@ export async function nextCloudApiRequest(

const credentialType =
authenticationMethod === 'accessToken' ? 'nextCloudApi' : 'nextCloudOAuth2Api';

try {
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
} catch (error) {
throw new NodeApiError(this.getNode(), error as JsonObject);
}
return await this.helpers.requestWithAuthentication.call(this, credentialType, options);
}
6 changes: 1 addition & 5 deletions packages/nodes-base/nodes/NocoDB/GenericFunctions.ts
Expand Up @@ -61,11 +61,7 @@ export async function apiRequest(
delete options.body;
}

try {
return await this.helpers.requestWithAuthentication.call(this, authenticationMethod, options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
return await this.helpers.requestWithAuthentication.call(this, authenticationMethod, options);
}

/**
Expand Down
6 changes: 1 addition & 5 deletions packages/nodes-base/nodes/Rocketchat/GenericFunctions.ts
Expand Up @@ -25,11 +25,7 @@ export async function rocketchatApiRequest(
if (Object.keys(options.body).length === 0) {
delete options.body;
}
try {
return await this.helpers.requestWithAuthentication.call(this, 'rocketchatApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
return await this.helpers.requestWithAuthentication.call(this, 'rocketchatApi', options);
}

// tslint:disable-next-line:no-any
Expand Down
6 changes: 1 addition & 5 deletions packages/nodes-base/nodes/Segment/GenericFunctions.ts
Expand Up @@ -36,9 +36,5 @@ export async function segmentApiRequest(
if (!Object.keys(body).length) {
delete options.body;
}
try {
return await this.helpers.requestWithAuthentication.call(this, 'segmentApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
return await this.helpers.requestWithAuthentication.call(this, 'segmentApi', options);
}
6 changes: 1 addition & 5 deletions packages/nodes-base/nodes/SendGrid/GenericFunctions.ts
Expand Up @@ -37,11 +37,7 @@ export async function sendGridApiRequest(
Object.assign(options, option);
}

try {
return await this.helpers.requestWithAuthentication.call(this, 'sendGridApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
return await this.helpers.requestWithAuthentication.call(this, 'sendGridApi', options);
}

export async function sendGridApiRequestAllItems(
Expand Down
10 changes: 3 additions & 7 deletions packages/nodes-base/nodes/Shopify/GenericFunctions.ts
Expand Up @@ -66,13 +66,9 @@ export async function shopifyApiRequest(
delete options.qs;
}

try {
return await this.helpers.requestWithAuthentication.call(this, credentialType, options, {
oauth2: oAuth2Options,
});
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
return await this.helpers.requestWithAuthentication.call(this, credentialType, options, {
oauth2: oAuth2Options,
});
}

export async function shopifyApiRequestAllItems(
Expand Down
6 changes: 1 addition & 5 deletions packages/nodes-base/nodes/Stripe/helpers.ts
Expand Up @@ -29,11 +29,7 @@ export async function stripeApiRequest(
delete options.qs;
}

try {
return await this.helpers.requestWithAuthentication.call(this, 'stripeApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
return await this.helpers.requestWithAuthentication.call(this, 'stripeApi', options);
}

/**
Expand Down
6 changes: 1 addition & 5 deletions packages/nodes-base/nodes/TheHive/GenericFunctions.ts
Expand Up @@ -40,11 +40,7 @@ export async function theHiveApiRequest(
if (Object.keys(query).length === 0) {
delete options.qs;
}
try {
return await this.helpers.requestWithAuthentication.call(this, 'theHiveApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
return await this.helpers.requestWithAuthentication.call(this, 'theHiveApi', options);
}

// Helpers functions
Expand Down
9 changes: 1 addition & 8 deletions packages/nodes-base/nodes/Trello/GenericFunctions.ts
Expand Up @@ -26,14 +26,7 @@ export async function apiRequest(
json: true,
};

try {
return await this.helpers.requestWithAuthentication.call(this, 'trelloApi', options);
} catch (error) {
if (error instanceof NodeApiError) {
throw error;
}
throw new NodeApiError(this.getNode(), error as JsonObject);
}
return await this.helpers.requestWithAuthentication.call(this, 'trelloApi', options);
}

export async function apiRequestAllItems(
Expand Down
9 changes: 1 addition & 8 deletions packages/nodes-base/nodes/Twake/GenericFunctions.ts
Expand Up @@ -31,12 +31,5 @@ export async function twakeApiRequest(
// options.uri = `${credentials!.hostUrl}/api/v1${resource}`;
// }

try {
return await this.helpers.requestWithAuthentication.call(this, 'twakeCloudApi', options);
} catch (error) {
if (error.error?.code === 'ECONNREFUSED') {
throw new NodeApiError(this.getNode(), error, { message: 'Twake host is not accessible!' });
}
throw new NodeApiError(this.getNode(), error);
}
return await this.helpers.requestWithAuthentication.call(this, 'twakeCloudApi', options);
}
6 changes: 1 addition & 5 deletions packages/nodes-base/nodes/Twilio/GenericFunctions.ts
Expand Up @@ -36,11 +36,7 @@ export async function twilioApiRequest(
json: true,
};

try {
return await this.helpers.requestWithAuthentication.call(this, 'twilioApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
return await this.helpers.requestWithAuthentication.call(this, 'twilioApi', options);
}

const XML_CHAR_MAP: { [key: string]: string } = {
Expand Down
6 changes: 1 addition & 5 deletions packages/nodes-base/nodes/UrlScanIo/GenericFunctions.ts
Expand Up @@ -27,11 +27,7 @@ export async function urlScanIoApiRequest(
delete options.qs;
}

try {
return await this.helpers.requestWithAuthentication.call(this, 'urlScanIoApi', options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
return await this.helpers.requestWithAuthentication.call(this, 'urlScanIoApi', options);
}

export async function handleListing(
Expand Down

0 comments on commit c7133ec

Please sign in to comment.