diff --git a/packages/rpc/src/protocol.ts b/packages/rpc/src/protocol.ts index 2ee89979d..adac23ff3 100644 --- a/packages/rpc/src/protocol.ts +++ b/packages/rpc/src/protocol.ts @@ -737,5 +737,9 @@ export function rpcDecodeError(error: EncodedError): Error { const e = new Error(error.message); e.stack = error.stack + '\nat ___SERVER___'; + if(error.stack === "") { + e.stack = "" + } + return e; } diff --git a/packages/rpc/src/server/kernel.ts b/packages/rpc/src/server/kernel.ts index dfb1b80fe..2f13586e2 100644 --- a/packages/rpc/src/server/kernel.ts +++ b/packages/rpc/src/server/kernel.ts @@ -528,7 +528,7 @@ export class RpcKernelConnection extends RpcKernelBaseConnection { return await this.actionHandler.handle(message, response); } } catch (error: any) { - response.error(error); + response.error(this.security.transformError(error)); } } diff --git a/packages/rpc/tests/security.spec.ts b/packages/rpc/tests/security.spec.ts index 9dd0d0547..6eae3a4e6 100644 --- a/packages/rpc/tests/security.spec.ts +++ b/packages/rpc/tests/security.spec.ts @@ -102,6 +102,36 @@ test('authentication errors', async () => { expect(memoryLogger.messages.length).toBe(1); }); +test('authentication errors and calls transformError', async () => { + let transformedError: Error | null = null; + + class MyKernelSecurity extends RpcKernelSecurity { + async authenticate(token: any): Promise { + throw new Error('Malformed token'); + } + + override transformError(err: Error) { + transformedError = new Error("Transformed"); + transformedError.stack = ""; + return transformedError; + } + } + + const kernel = new RpcKernel([{ provide: RpcKernelSecurity, useClass: MyKernelSecurity, scope: 'rpc' }]); + const client = new DirectClient(kernel); + + client.token.set('generic'); + + try { + await client.connect() + } catch(e: any) { + expect(e.stack).toBe(''); + } + + expect(transformedError).toBeInstanceOf(Error); + expect(transformedError!.message).toBe('Transformed'); +}); + test('onAuthenticate controllers', async () => { class AuthenticatedSession extends Session {