From 481c494df0c72aa2600effe4865d69920ea8e220 Mon Sep 17 00:00:00 2001 From: "n8n-cat-bot[bot]" <283985454+n8n-cat-bot[bot]@users.noreply.github.com> Date: Thu, 10 Sep 2026 10:27:32 +0000 Subject: [PATCH] chore: Move the duplicated CLI command error banner into BaseCommand (#38298) Co-authored-by: n8n-cat-bot[bot] Co-authored-by: Claude Opus 5 --- .../commands/__tests__/base-command.test.ts | 44 +++++++++++++++++++ packages/cli/src/commands/base-command.ts | 9 ++++ packages/cli/src/commands/list/workflow.ts | 5 +-- packages/cli/src/commands/publish/workflow.ts | 6 +-- packages/cli/src/commands/ttwf/generate.ts | 5 +-- .../cli/src/commands/unpublish/workflow.ts | 6 +-- packages/cli/src/commands/update/workflow.ts | 6 +-- 7 files changed, 58 insertions(+), 23 deletions(-) create mode 100644 packages/cli/src/commands/__tests__/base-command.test.ts diff --git a/packages/cli/src/commands/__tests__/base-command.test.ts b/packages/cli/src/commands/__tests__/base-command.test.ts new file mode 100644 index 00000000000..290f6ef8a3c --- /dev/null +++ b/packages/cli/src/commands/__tests__/base-command.test.ts @@ -0,0 +1,44 @@ +import { Logger } from '@n8n/backend-common'; +import { mockInstance } from '@n8n/backend-test-utils'; + +import { BaseCommand } from '../base-command'; + +class TestCommand extends BaseCommand { + async run() {} +} + +const logger = mockInstance(Logger); + +afterEach(() => { + vi.resetAllMocks(); +}); + +describe('logError', () => { + const error = new Error('Something went wrong'); + error.stack = 'the stack'; + + it('should log the error banner', () => { + // @ts-expect-error Protected method + new TestCommand().logError(error); + + expect(logger.error.mock.calls.flat()).toEqual([ + '\nGOT ERROR', + '====================================', + 'Something went wrong', + 'the stack', + ]); + }); + + it('should log the summary before the error banner', () => { + // @ts-expect-error Protected method + new TestCommand().logError(error, 'Error updating database.'); + + expect(logger.error.mock.calls.flat()).toEqual([ + 'Error updating database.', + '\nGOT ERROR', + '====================================', + 'Something went wrong', + 'the stack', + ]); + }); +}); diff --git a/packages/cli/src/commands/base-command.ts b/packages/cli/src/commands/base-command.ts index b7eae142b8e..da115b812ab 100644 --- a/packages/cli/src/commands/base-command.ts +++ b/packages/cli/src/commands/base-command.ts @@ -331,6 +331,15 @@ export abstract class BaseCommand { throw new UnexpectedError(message); } + /** Print an error banner, optionally preceded by a command-specific summary. */ + protected logError(error: Error, summary?: string) { + if (summary) this.logger.error(summary); + this.logger.error('\nGOT ERROR'); + this.logger.error('===================================='); + this.logger.error(error.message); + this.logger.error(error.stack!); + } + async initBinaryDataService() { const binaryDataConfig = Container.get(BinaryDataConfig); const binaryDataService = Container.get(BinaryDataService); diff --git a/packages/cli/src/commands/list/workflow.ts b/packages/cli/src/commands/list/workflow.ts index eaff1f407ff..37cc2752e4c 100644 --- a/packages/cli/src/commands/list/workflow.ts +++ b/packages/cli/src/commands/list/workflow.ts @@ -42,9 +42,6 @@ export class ListWorkflowCommand extends BaseCommand } async catch(error: Error) { - this.logger.error('\nGOT ERROR'); - this.logger.error('===================================='); - this.logger.error(error.message); - this.logger.error(error.stack!); + this.logError(error); } } diff --git a/packages/cli/src/commands/publish/workflow.ts b/packages/cli/src/commands/publish/workflow.ts index b330de31209..1329df63465 100644 --- a/packages/cli/src/commands/publish/workflow.ts +++ b/packages/cli/src/commands/publish/workflow.ts @@ -47,10 +47,6 @@ export class PublishWorkflowCommand extends BaseCommand } async catch(error: Error) { - this.logger.error('\nGOT ERROR'); - this.logger.error('===================================='); - this.logger.error(error.message); - this.logger.error(error.stack!); + this.logError(error); } } diff --git a/packages/cli/src/commands/unpublish/workflow.ts b/packages/cli/src/commands/unpublish/workflow.ts index 628986e89ee..965f207a262 100644 --- a/packages/cli/src/commands/unpublish/workflow.ts +++ b/packages/cli/src/commands/unpublish/workflow.ts @@ -45,10 +45,6 @@ export class UnpublishWorkflowCommand extends BaseCommand