refactor(Google Sheets Node): Prevent reporting to Sentry failure to find spreadsheet (no-changelog) (#7518)

To silence this very frequently reported error:
https://n8nio.sentry.io/issues/3823103947/?project=4503924908883968&query=&referrer=issue-stream&statsPeriod=90d&stream_index=0
This commit is contained in:
Iván Ovejero 2023-10-26 11:33:11 +02:00 committed by GitHub
parent 65e5593233
commit d885daa6a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 7 deletions

View file

@ -20,7 +20,11 @@ export async function router(this: IExecuteFunctions): Promise<INodeExecutionDat
let results: INodeExecutionData[] | undefined; let results: INodeExecutionData[] | undefined;
if (googleSheets.resource === 'sheet') { if (googleSheets.resource === 'sheet') {
const { mode, value } = this.getNodeParameter('documentId', 0) as IDataObject; const { mode, value } = this.getNodeParameter('documentId', 0) as IDataObject;
const spreadsheetId = getSpreadsheetId(mode as ResourceLocator, value as string); const spreadsheetId = getSpreadsheetId(
this.getNode(),
mode as ResourceLocator,
value as string,
);
const googleSheet = new GoogleSheet(spreadsheetId, this); const googleSheet = new GoogleSheet(spreadsheetId, this);

View file

@ -4,6 +4,7 @@ import type {
INodeExecutionData, INodeExecutionData,
INodeListSearchItems, INodeListSearchItems,
INodePropertyOptions, INodePropertyOptions,
INode,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow'; import { NodeOperationError } from 'n8n-workflow';
import type { GoogleSheet } from './GoogleSheet'; import type { GoogleSheet } from './GoogleSheet';
@ -18,10 +19,16 @@ import { ResourceLocatorUiNames, ROW_NUMBER } from './GoogleSheets.types';
export const untilSheetSelected = { sheetName: [''] }; export const untilSheetSelected = { sheetName: [''] };
// Used to extract the ID from the URL // Used to extract the ID from the URL
export function getSpreadsheetId(documentIdType: ResourceLocator, value: string): string { export function getSpreadsheetId(
node: INode,
documentIdType: ResourceLocator,
value: string,
): string {
if (!value) { if (!value) {
throw new Error( throw new NodeOperationError(
node,
`Can not get sheet '${ResourceLocatorUiNames[documentIdType]}' with a value of '${value}'`, `Can not get sheet '${ResourceLocatorUiNames[documentIdType]}' with a value of '${value}'`,
{ severity: 'warning' },
); );
} }
if (documentIdType === 'url') { if (documentIdType === 'url') {

View file

@ -52,7 +52,7 @@ export async function sheetsSearch(
_filter?: string, _filter?: string,
): Promise<INodeListSearchResult> { ): Promise<INodeListSearchResult> {
const { mode, value } = this.getNodeParameter('documentId', 0) as IDataObject; const { mode, value } = this.getNodeParameter('documentId', 0) as IDataObject;
const spreadsheetId = getSpreadsheetId(mode as ResourceLocator, value as string); const spreadsheetId = getSpreadsheetId(this.getNode(), mode as ResourceLocator, value as string);
const query = { const query = {
fields: 'sheets.properties', fields: 'sheets.properties',

View file

@ -6,7 +6,7 @@ import type { ResourceLocator } from '../helpers/GoogleSheets.types';
export async function getSheets(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> { export async function getSheets(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const { mode, value } = this.getNodeParameter('documentId', 0) as IDataObject; const { mode, value } = this.getNodeParameter('documentId', 0) as IDataObject;
const spreadsheetId = getSpreadsheetId(mode as ResourceLocator, value as string); const spreadsheetId = getSpreadsheetId(this.getNode(), mode as ResourceLocator, value as string);
const sheet = new GoogleSheet(spreadsheetId, this); const sheet = new GoogleSheet(spreadsheetId, this);
const responseData = await sheet.spreadsheetGetSheets(); const responseData = await sheet.spreadsheetGetSheets();
@ -34,7 +34,7 @@ export async function getSheetHeaderRow(
this: ILoadOptionsFunctions, this: ILoadOptionsFunctions,
): Promise<INodePropertyOptions[]> { ): Promise<INodePropertyOptions[]> {
const { mode, value } = this.getNodeParameter('documentId', 0) as IDataObject; const { mode, value } = this.getNodeParameter('documentId', 0) as IDataObject;
const spreadsheetId = getSpreadsheetId(mode as ResourceLocator, value as string); const spreadsheetId = getSpreadsheetId(this.getNode(), mode as ResourceLocator, value as string);
const sheet = new GoogleSheet(spreadsheetId, this); const sheet = new GoogleSheet(spreadsheetId, this);
let sheetWithinDocument = this.getNodeParameter('sheetName', undefined, { let sheetWithinDocument = this.getNodeParameter('sheetName', undefined, {

View file

@ -12,7 +12,7 @@ export async function getMappingColumns(
this: ILoadOptionsFunctions, this: ILoadOptionsFunctions,
): Promise<ResourceMapperFields> { ): Promise<ResourceMapperFields> {
const { mode, value } = this.getNodeParameter('documentId', 0) as IDataObject; const { mode, value } = this.getNodeParameter('documentId', 0) as IDataObject;
const spreadsheetId = getSpreadsheetId(mode as ResourceLocator, value as string); const spreadsheetId = getSpreadsheetId(this.getNode(), mode as ResourceLocator, value as string);
const sheet = new GoogleSheet(spreadsheetId, this); const sheet = new GoogleSheet(spreadsheetId, this);
let sheetWithinDocument = this.getNodeParameter('sheetName', undefined, { let sheetWithinDocument = this.getNodeParameter('sheetName', undefined, {