n8n/packages/nodes-base/nodes/Airtable/AirtableTrigger.node.ts
Iván Ovejero 1d27a9e87e
Improve node error handling (#1309)
* Add path mapping and response error interfaces

* Add error handling and throwing functionality

* Refactor error handling into a single function

* Re-implement error handling in Hacker News node

* Fix linting details

* Re-implement error handling in Spotify node

* Re-implement error handling in G Suite Admin node

* 🚧 create basic setup NodeError

* 🚧 add httpCodes

* 🚧 add path priolist

* 🚧 handle statusCode in error, adjust interfaces

* 🚧 fixing type issues w/Ivan

* 🚧 add error exploration

* 👔 fix linter issues

* 🔧 improve object check

* 🚧 remove path passing from NodeApiError

* 🚧 add multi error + refactor findProperty method

* 👔 allow any

* 🔧 handle multi error message callback

*  change return type of callback

*  add customCallback to MultiError

* 🚧 refactor to use INode

* 🔨 handle arrays, continue search after first null property found

* 🚫 refactor method access

* 🚧 setup NodeErrorView

*  change timestamp to Date.now

* 📚 Add documentation for methods and constants

* 🚧 change message setting

* 🚚 move NodeErrors to workflow

*  add new ErrorView for Nodes

* 🎨 improve error notification

* 🎨 refactor interfaces

*  add WorkflowOperationError, refactor error throwing

* 👕 fix linter issues

* 🎨 rename param

* 🐛 fix handling normal errors

*  add usage of NodeApiError

* 🎨 fix throw new error instead of constructor

* 🎨 remove unnecessary code/comments

* 🎨 adjusted spacing + updated status messages

* 🎨 fix tab indentation

*  Replace current errors with custom errors (#1576)

*  Introduce NodeApiError in catch blocks

*  Introduce NodeOperationError in nodes

*  Add missing errors and remove incompatible

*  Fix NodeOperationError in incompatible nodes

* 🔧 Adjust error handling in missed nodes

PayPal, FileMaker, Reddit, Taiga and Facebook Graph API nodes

* 🔨 Adjust Strava Trigger node error handling

* 🔨 Adjust AWS nodes error handling

* 🔨 Remove duplicate instantiation of NodeApiError

* 🐛 fix strava trigger node error handling

* Add XML parsing to NodeApiError constructor (#1633)

* 🐛 Remove type annotation from catch variable

*  Add XML parsing to NodeApiError

*  Simplify error handling in Rekognition node

*  Pass in XML flag in generic functions

* 🔥 Remove try/catch wrappers at call sites

* 🔨 Refactor setting description from XML

* 🔨 Refactor let to const in resource loaders

*  Find property in parsed XML

*  Change let to const

* 🔥 Remove unneeded try/catch block

* 👕 Fix linting issues

* 🐛 Fix errors from merge conflict resolution

*  Add custom errors to latest contributions

* 👕 Fix linting issues

*  Refactor MongoDB helpers for custom errors

* 🐛 Correct custom error type

*  Apply feedback to A nodes

*  Apply feedback to missed A node

*  Apply feedback to B-D nodes

*  Apply feedback to E-F nodes

*  Apply feedback to G nodes

*  Apply feedback to H-L nodes

*  Apply feedback to M nodes

*  Apply feedback to P nodes

*  Apply feedback to R nodes

*  Apply feedback to S nodes

*  Apply feedback to T nodes

*  Apply feedback to V-Z nodes

*  Add HTTP code to iterable node error

* 🔨 Standardize e as error

* 🔨 Standardize err as error

*  Fix error handling for non-standard nodes

Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com>

Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com>
Co-authored-by: Ben Hesseldieck <1849459+BHesseldieck@users.noreply.github.com>
2021-04-16 18:33:36 +02:00

189 lines
5.1 KiB
TypeScript

import {
IPollFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
NodeOperationError,
} from 'n8n-workflow';
import {
apiRequestAllItems,
downloadRecordAttachments,
} from './GenericFunctions';
import * as moment from 'moment';
export class AirtableTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Airtable Trigger',
name: 'airtableTrigger',
icon: 'file:airtable.svg',
group: ['trigger'],
version: 1,
description: 'Starts the workflow when Airtable events occur',
subtitle: '={{$parameter["event"]}}',
defaults: {
name: 'Airtable Trigger',
color: '#445599',
},
credentials: [
{
name: 'airtableApi',
required: true,
},
],
polling: true,
inputs: [],
outputs: ['main'],
properties: [
{
displayName: 'Base ID',
name: 'baseId',
type: 'string',
default: '',
required: true,
description: 'The ID of this base.',
},
{
displayName: 'Table',
name: 'tableId',
type: 'string',
default: '',
description: 'The name of table to access.',
required: true,
},
{
displayName: 'Trigger Field',
name: 'triggerField',
type: 'string',
default: '',
description: `A Created Time or Last Modified Time field that will be used to sort records. <br>
If you do not have a Created Time or Last Modified Time field in your schema, please create one,<br>
because without this field trigger will not work correctly.`,
required: true,
},
{
displayName: 'Download Attachments',
name: 'downloadAttachments',
type: 'boolean',
default: false,
description: `When set to true the attachment fields define in 'Download Fields' will be downloaded.`,
},
{
displayName: 'Download Fields',
name: 'downloadFieldNames',
type: 'string',
required: true,
displayOptions: {
show: {
downloadAttachments: [
true,
],
},
},
default: '',
description: `Name of the fields of type 'attachment' that should be downloaded. Multiple ones can be defined separated by comma. Case sensitive.`,
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
options: [
{
displayName: 'Fields',
name: 'fields',
type: 'string',
default: '',
description: `Fields to be included in the response.<br>
Multiple ones can be set separated by comma. Example: name,id.<br>
By default just the trigger field will be included.`,
},
{
displayName: 'Formula',
name: 'formula',
type: 'string',
default: '',
description: `Formulas may involve functions, numeric operations, logical operations, and text operations that operate on fields. More info <a href="https://support.airtable.com/hc/en-us/articles/203255215-Formula-Field-Reference" target="_blank">here</a>.`,
},
{
displayName: 'View ID',
name: 'viewId',
type: 'string',
default: '',
description: 'The name or ID of a view in the table. If set, only the records in that view will be returned.',
},
],
},
],
};
async poll(this: IPollFunctions): Promise<INodeExecutionData[][] | null> {
const downloadAttachments = this.getNodeParameter('downloadAttachments', 0) as boolean;
const webhookData = this.getWorkflowStaticData('node');
const qs: IDataObject = {};
const additionalFields = this.getNodeParameter('additionalFields') as IDataObject;
const base = this.getNodeParameter('baseId') as string;
const table = this.getNodeParameter('tableId') as string;
const triggerField = this.getNodeParameter('triggerField') as string;
const endpoint = `${base}/${table}`;
const now = moment().utc().format();
const startDate = webhookData.lastTimeChecked as string || now;
const endDate = now;
if (additionalFields.viewId) {
qs.view = additionalFields.viewId;
}
if (additionalFields.fields) {
qs['fields[]'] = (additionalFields.fields as string).split(',');
}
qs.filterByFormula = `IS_AFTER({${triggerField}}, DATETIME_PARSE("${startDate}", "YYYY-MM-DD HH:mm:ss"))`;
if (additionalFields.formula) {
qs.filterByFormula = `AND(${qs.filterByFormula}, ${additionalFields.formula})`;
}
if (this.getMode() === 'manual') {
delete qs.filterByFormula;
qs.maxRecords = 1;
}
const { records } = await apiRequestAllItems.call(this, 'GET', endpoint, {}, qs);
webhookData.lastTimeChecked = endDate;
if (Array.isArray(records) && records.length) {
if (this.getMode() === 'manual' && records[0].fields[triggerField] === undefined) {
throw new NodeOperationError(this.getNode(), `The Field "${triggerField}" does not exist.`);
}
if (downloadAttachments === true) {
const downloadFieldNames = (this.getNodeParameter('downloadFieldNames', 0) as string).split(',');
const data = await downloadRecordAttachments.call(this, records, downloadFieldNames);
return [data];
}
return [this.helpers.returnJsonArray(records)];
}
return null;
}
}