refactor(core): Switch plain errors in cli to ApplicationError (#7857)

Ensure all errors in `cli` are `ApplicationError` or children of it and
contain no variables in the message, to continue normalizing all the
errors we report to Sentry

Follow-up to: https://github.com/n8n-io/n8n/pull/7839
This commit is contained in:
Iván Ovejero
2023-11-29 12:25:10 +01:00
committed by GitHub
parent 87def60979
commit c08c5cc37b
58 changed files with 277 additions and 195 deletions

View File

@@ -2,6 +2,7 @@ import type { TableForeignKeyOptions, TableIndexOptions, QueryRunner } from 'typ
import { Table, TableColumn } from 'typeorm';
import LazyPromise from 'p-lazy';
import { Column } from './Column';
import { ApplicationError } from 'n8n-workflow';
abstract class TableOperation<R = void> extends LazyPromise<R> {
abstract execute(queryRunner: QueryRunner): Promise<R>;
@@ -131,7 +132,7 @@ class ModifyNotNull extends TableOperation {
async execute(queryRunner: QueryRunner) {
const { tableName, prefix, columnName, isNullable } = this;
const table = await queryRunner.getTable(`${prefix}${tableName}`);
if (!table) throw new Error(`No table found with the name ${tableName}`);
if (!table) throw new ApplicationError('No table found', { extra: { tableName } });
const oldColumn = table.findColumnByName(columnName)!;
const newColumn = oldColumn.clone();
newColumn.isNullable = isNullable;

View File

@@ -1,4 +1,5 @@
import type { MigrationContext, ReversibleMigration } from '@db/types';
import { ApplicationError } from 'n8n-workflow';
export class AddGlobalAdminRole1700571993961 implements ReversibleMigration {
async up({ escape, runQuery }: MigrationContext) {
@@ -39,7 +40,7 @@ export class AddGlobalAdminRole1700571993961 implements ReversibleMigration {
const memberRoleId = memberRoleIdResult[0]?.id;
if (!memberRoleId) {
throw new Error('Could not find global member role!');
throw new ApplicationError('Could not find global member role!');
}
await runQuery(

View File

@@ -8,7 +8,12 @@ import type {
SelectQueryBuilder,
} from 'typeorm';
import { parse, stringify } from 'flatted';
import type { ExecutionStatus, IExecutionsSummary, IRunExecutionData } from 'n8n-workflow';
import {
ApplicationError,
type ExecutionStatus,
type IExecutionsSummary,
type IRunExecutionData,
} from 'n8n-workflow';
import { BinaryDataService } from 'n8n-core';
import type {
ExecutionPayload,
@@ -381,7 +386,9 @@ export class ExecutionRepository extends Repository<ExecutionEntity> {
},
) {
if (!deleteConditions?.deleteBefore && !deleteConditions?.ids) {
throw new Error('Either "deleteBefore" or "ids" must be present in the request body');
throw new ApplicationError(
'Either "deleteBefore" or "ids" must be present in the request body',
);
}
const query = this.createQueryBuilder('execution')

View File

@@ -3,7 +3,7 @@ import { readFileSync, rmSync } from 'fs';
import { InstanceSettings } from 'n8n-core';
import type { ObjectLiteral } from 'typeorm';
import type { QueryRunner } from 'typeorm/query-runner/QueryRunner';
import { jsonParse } from 'n8n-workflow';
import { ApplicationError, jsonParse } from 'n8n-workflow';
import config from '@/config';
import { inTest } from '@/constants';
import type { BaseMigration, Migration, MigrationContext, MigrationFn } from '@db/types';
@@ -23,7 +23,7 @@ function loadSurveyFromDisk(): string | null {
const personalizationSurvey = JSON.parse(surveyFile) as object;
const kvPairs = Object.entries(personalizationSurvey);
if (!kvPairs.length) {
throw new Error('personalizationSurvey is empty');
throw new ApplicationError('personalizationSurvey is empty');
} else {
const emptyKeys = kvPairs.reduce((acc, [, value]) => {
if (!value || (Array.isArray(value) && !value.length)) {
@@ -32,7 +32,7 @@ function loadSurveyFromDisk(): string | null {
return acc;
}, 0);
if (emptyKeys === kvPairs.length) {
throw new Error('incomplete personalizationSurvey');
throw new ApplicationError('incomplete personalizationSurvey');
}
}
return surveyFile;
@@ -68,7 +68,8 @@ const runDisablingForeignKeys = async (
fn: MigrationFn,
) => {
const { dbType, queryRunner } = context;
if (dbType !== 'sqlite') throw new Error('Disabling transactions only available in sqlite');
if (dbType !== 'sqlite')
throw new ApplicationError('Disabling transactions only available in sqlite');
await queryRunner.query('PRAGMA foreign_keys=OFF');
await queryRunner.startTransaction();
try {