From 6595fde713871bb1bfedee8eb53a3bc13ccf8f02 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Tue, 9 Apr 2024 22:43:12 +0800 Subject: [PATCH] chore(gateway): report error with cause message (#3999) --- .../core/server/src/__tests__/gateway.test.ts | 31 +++++++++++++++++-- packages/core/server/src/application.ts | 2 ++ packages/core/server/src/gateway/ws-server.ts | 8 ++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/packages/core/server/src/__tests__/gateway.test.ts b/packages/core/server/src/__tests__/gateway.test.ts index 630323d8b1..6c10800d79 100644 --- a/packages/core/server/src/__tests__/gateway.test.ts +++ b/packages/core/server/src/__tests__/gateway.test.ts @@ -165,8 +165,8 @@ describe('gateway', () => { wsClient.on('open', resolve); }); }; - const getLastMessage = () => { - return JSON.parse(messages[messages.length - 1]); + const getLastMessage = (n = 0) => { + return JSON.parse(messages[messages.length - (1 + n)]); }; const clearMessages = () => { messages = []; @@ -377,5 +377,32 @@ describe('gateway', () => { }, }); }); + it('should receive error message with cause property', async () => { + await connectClient(port); + const app = new Application({ + database: { + dialect: 'sqlite', + storage: ':memory:', + }, + }); + await waitSecond(); + await app.runCommand('start'); + await app.runCommand('install'); + await waitSecond(); + expect(getLastMessage()).toMatchObject({ + type: 'maintaining', + payload: { + code: 'APP_RUNNING', + }, + }); + await app.runAsCLI(['pm', 'add', 'not-exists-plugin'], { + from: 'user', + }); + await waitSecond(); + const errorMsg = getLastMessage(1); + expect(errorMsg.type).toBe('notification'); + expect(errorMsg.payload.type).toBe('error'); + expect(errorMsg.payload.message).contains('Failed to add plugin:'); + }); }); }); diff --git a/packages/core/server/src/application.ts b/packages/core/server/src/application.ts index ae80ea3181..c9a2a27940 100644 --- a/packages/core/server/src/application.ts +++ b/packages/core/server/src/application.ts @@ -765,6 +765,8 @@ export class Application exten if (options?.throwError) { throw error; + } else { + this.log.error(error); } } finally { const _actionCommand = this._actionCommand; diff --git a/packages/core/server/src/gateway/ws-server.ts b/packages/core/server/src/gateway/ws-server.ts index c50808d9cc..e9ec0fbcd2 100644 --- a/packages/core/server/src/gateway/ws-server.ts +++ b/packages/core/server/src/gateway/ws-server.ts @@ -62,10 +62,16 @@ export class WSServer { }); AppSupervisor.getInstance().on('appError', async ({ appName, error }) => { + let message = error.message; + + if (error.cause) { + message = `${message}: ${error.cause.message}`; + } + this.sendToConnectionsByTag('app', appName, { type: 'notification', payload: { - message: error.message, + message, type: 'error', }, });