Skip to content

Commit

Permalink
setup generic signalr error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hahn-kev committed Jul 10, 2024
1 parent 22ea1ad commit 4a62b9e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions backend/FwLite/LocalWebApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
builder.Services.AddSignalR(options =>
{
options.AddFilter(new LockedProjectFilter());
options.EnableDetailedErrors = true;
}).AddJsonProtocol();

var app = builder.Build();
Expand Down
14 changes: 12 additions & 2 deletions frontend/viewer/src/FwDataProjectView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
console.error('Failed to start the connection:', err);
});
}
connect();
onDestroy(() => connection.stop());
connection.onclose(error => {
Expand Down Expand Up @@ -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;
</script>
<ProjectView {projectName} isConnected={connected}></ProjectView>
22 changes: 20 additions & 2 deletions frontend/viewer/src/lib/services/service-provider-signalr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down

0 comments on commit 4a62b9e

Please sign in to comment.