chore(gateway): report error with cause message (#3999)

This commit is contained in:
ChengLei Shao 2024-04-09 22:43:12 +08:00 committed by GitHub
parent 320005843a
commit 6595fde713
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 38 additions and 3 deletions

View File

@ -165,8 +165,8 @@ describe('gateway', () => {
wsClient.on('open', resolve); wsClient.on('open', resolve);
}); });
}; };
const getLastMessage = () => { const getLastMessage = (n = 0) => {
return JSON.parse(messages[messages.length - 1]); return JSON.parse(messages[messages.length - (1 + n)]);
}; };
const clearMessages = () => { const clearMessages = () => {
messages = []; 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:');
});
}); });
}); });

View File

@ -765,6 +765,8 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
if (options?.throwError) { if (options?.throwError) {
throw error; throw error;
} else {
this.log.error(error);
} }
} finally { } finally {
const _actionCommand = this._actionCommand; const _actionCommand = this._actionCommand;

View File

@ -62,10 +62,16 @@ export class WSServer {
}); });
AppSupervisor.getInstance().on('appError', async ({ appName, error }) => { AppSupervisor.getInstance().on('appError', async ({ appName, error }) => {
let message = error.message;
if (error.cause) {
message = `${message}: ${error.cause.message}`;
}
this.sendToConnectionsByTag('app', appName, { this.sendToConnectionsByTag('app', appName, {
type: 'notification', type: 'notification',
payload: { payload: {
message: error.message, message,
type: 'error', type: 'error',
}, },
}); });