From 2ff8fe12e7717dde036d2e9d41b9e2393b7a66f2 Mon Sep 17 00:00:00 2001 From: Luciano Vernaschi Date: Tue, 10 Dec 2024 15:55:58 +0100 Subject: [PATCH] Add error message parameter to `onError` callback in subscription --- packages/ts/frontend/src/Connect.ts | 2 +- packages/ts/frontend/src/FluxConnection.ts | 6 +++--- packages/ts/frontend/test/FluxConnection.test.ts | 1 + 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/ts/frontend/src/Connect.ts b/packages/ts/frontend/src/Connect.ts index 2ded276983..5609e63bef 100644 --- a/packages/ts/frontend/src/Connect.ts +++ b/packages/ts/frontend/src/Connect.ts @@ -42,7 +42,7 @@ export interface Subscription { onComplete(callback: () => void): Subscription; /** Called when an exception occured in the subscription. */ - onError(callback: () => void): Subscription; + onError(callback: (message: string) => void): Subscription; /** Called when a new value is available. */ onNext(callback: (value: T) => void): Subscription; diff --git a/packages/ts/frontend/src/FluxConnection.ts b/packages/ts/frontend/src/FluxConnection.ts index bc17784861..a0cc022b79 100644 --- a/packages/ts/frontend/src/FluxConnection.ts +++ b/packages/ts/frontend/src/FluxConnection.ts @@ -80,7 +80,7 @@ export class FluxConnection extends EventTarget { readonly #endpointInfos = new Map(); #nextId = 0; readonly #onCompleteCallbacks = new Map void>(); - readonly #onErrorCallbacks = new Map void>(); + readonly #onErrorCallbacks = new Map void>(); readonly #onNextCallbacks = new Map void>(); readonly #onStateChangeCallbacks = new Map void>(); readonly #statusOfSubscriptions = new Map(); @@ -154,7 +154,7 @@ export class FluxConnection extends EventTarget { this.#onCompleteCallbacks.set(id, callback); return hillaSubscription; }, - onError: (callback: () => void): Subscription => { + onError: (callback: (message: string) => void): Subscription => { this.#onErrorCallbacks.set(id, callback); return hillaSubscription; }, @@ -285,7 +285,7 @@ export class FluxConnection extends EventTarget { } else { const callback = this.#onErrorCallbacks.get(id); if (callback) { - callback(); + callback(message.message); } this.#removeSubscription(id); if (!callback) { diff --git a/packages/ts/frontend/test/FluxConnection.test.ts b/packages/ts/frontend/test/FluxConnection.test.ts index 2fdd08cfdd..c6005a55ac 100644 --- a/packages/ts/frontend/test/FluxConnection.test.ts +++ b/packages/ts/frontend/test/FluxConnection.test.ts @@ -125,6 +125,7 @@ describe('@vaadin/hilla-frontend', () => { const msg: ClientErrorMessage = { '@type': 'error', id: '0', message: 'it failed' }; emitMessage(msg); expect(onError).to.have.been.calledOnce; + expect(onError).to.have.been.calledWith('it failed'); }); it('should not deliver messages after completing', () => {