From 4a62b9e8cb734ca619d836a5948a373b87fea064 Mon Sep 17 00:00:00 2001 From: Kevin Hahn Date: Wed, 10 Jul 2024 15:44:32 +0700 Subject: [PATCH] setup generic signalr error handling --- backend/FwLite/LocalWebApp/Program.cs | 1 + frontend/viewer/src/FwDataProjectView.svelte | 14 ++++++++++-- .../lib/services/service-provider-signalr.ts | 22 +++++++++++++++++-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/backend/FwLite/LocalWebApp/Program.cs b/backend/FwLite/LocalWebApp/Program.cs index 151beb5aa..753f32660 100644 --- a/backend/FwLite/LocalWebApp/Program.cs +++ b/backend/FwLite/LocalWebApp/Program.cs @@ -29,6 +29,7 @@ builder.Services.AddSignalR(options => { options.AddFilter(new LockedProjectFilter()); + options.EnableDetailedErrors = true; }).AddJsonProtocol(); var app = builder.Build(); diff --git a/frontend/viewer/src/FwDataProjectView.svelte b/frontend/viewer/src/FwDataProjectView.svelte index 046bbf549..90519ec92 100644 --- a/frontend/viewer/src/FwDataProjectView.svelte +++ b/frontend/viewer/src/FwDataProjectView.svelte @@ -23,7 +23,6 @@ console.error('Failed to start the connection:', err); }); } - connect(); onDestroy(() => connection.stop()); connection.onclose(error => { @@ -54,7 +53,18 @@ break; } } - }); + }, + (errorContext) => { + + connected = false; + if (errorContext.error instanceof Error) { + let message = errorContext.error.message; + AppNotification.display('Connection error: ' + message, 'error', 'long'); + } else { + AppNotification.display('Unknown Connection error', 'error', 'long'); + } + } + ); let connected = false; diff --git a/frontend/viewer/src/lib/services/service-provider-signalr.ts b/frontend/viewer/src/lib/services/service-provider-signalr.ts index d7edb9560..996a2a842 100644 --- a/frontend/viewer/src/lib/services/service-provider-signalr.ts +++ b/frontend/viewer/src/lib/services/service-provider-signalr.ts @@ -5,9 +5,27 @@ import type { LexboxApiFeatures, LexboxApiMetadata } from './lexbox-api'; import {LexboxService} from './service-provider'; import type {ILexboxClient} from '../generated-signalr-client/TypedSignalR.Client/Lexbox.ClientServer.Hubs'; - -export function SetupSignalR(connection: HubConnection, features: LexboxApiFeatures, client: ILexboxClient | null = null) { +type ErrorContext = {error: Error|unknown, methodName: string}; +export function SetupSignalR(connection: HubConnection, features: LexboxApiFeatures, client: ILexboxClient | null = null, onError?: (context: ErrorContext) => void) { const hubFactory = getHubProxyFactory('ILexboxApiHub'); + if (onError) { + connection = new Proxy(connection, { + get(target, prop: keyof HubConnection, receiver) { + if (prop === 'invoke') { + return async (methodName: string, ...args: any[]) => { + try { + return await target.invoke(methodName, ...args); + } catch (e) { + onError({error: e, methodName}); + throw e; + } + } + } else { + return Reflect.get(target, prop, receiver); + } + } + }) as HubConnection; + } const hubProxy = hubFactory.createHubProxy(connection); const lexboxApiHubProxy = Object.assign(hubProxy, {