Fix crash from exceptions in handlers (#771)

* Fix crash from exceptions in handlers

* Fix withScope usage

* Add changelog
This commit is contained in:
Justin Carlson 2023-06-05 12:25:19 -04:00 committed by GitHub
parent 6ca2b51893
commit 96c398ce74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 17 deletions

1
changelog.d/771.bugfix Normal file
View File

@ -0,0 +1 @@
Fix crash when failing to handle events, typically due to lacking permissions to send messages in a room.

View File

@ -786,7 +786,10 @@ export class Bridge {
this.ready = true; this.ready = true;
} }
private handleHookshotEvent<EventType, ConnType extends IConnection>(msg: MessageQueueMessageOut<EventType>, connection: ConnType, handler: (c: ConnType, data: EventType) => Promise<unknown>|unknown) { private async handleHookshotEvent<EventType, ConnType extends IConnection>(msg: MessageQueueMessageOut<EventType>, connection: ConnType, handler: (c: ConnType, data: EventType) => Promise<unknown>|unknown) {
try {
await handler(connection, msg.data);
} catch (e) {
Sentry.withScope((scope) => { Sentry.withScope((scope) => {
scope.setTransactionName('handleHookshotEvent'); scope.setTransactionName('handleHookshotEvent');
scope.setTags({ scope.setTags({
@ -796,13 +799,12 @@ export class Bridge {
scope.setContext("connection", { scope.setContext("connection", {
id: connection.connectionId, id: connection.connectionId,
}); });
new Promise(() => handler(connection, msg.data)).catch((ex) => { log.warn(`Connection ${connection.toString()} failed to handle ${msg.eventName}:`, e);
Sentry.captureException(ex, scope);
Metrics.connectionsEventFailed.inc({ event: msg.eventName, connectionId: connection.connectionId }); Metrics.connectionsEventFailed.inc({ event: msg.eventName, connectionId: connection.connectionId });
log.warn(`Connection ${connection.toString()} failed to handle ${msg.eventName}:`, ex); Sentry.captureException(e, scope);
});
}); });
} }
}
private async bindHandlerToQueue<EventType, ConnType extends IConnection>(event: string, connectionFetcher: (data: EventType) => ConnType[], handler: (c: ConnType, data: EventType) => Promise<unknown>|unknown) { private async bindHandlerToQueue<EventType, ConnType extends IConnection>(event: string, connectionFetcher: (data: EventType) => ConnType[], handler: (c: ConnType, data: EventType) => Promise<unknown>|unknown) {
const connectionFetcherBound = connectionFetcher.bind(this); const connectionFetcherBound = connectionFetcher.bind(this);
@ -810,8 +812,8 @@ export class Bridge {
const connections = connectionFetcherBound(msg.data); const connections = connectionFetcherBound(msg.data);
log.debug(`${event} for ${connections.map(c => c.toString()).join(', ') || '[empty]'}`); log.debug(`${event} for ${connections.map(c => c.toString()).join(', ') || '[empty]'}`);
connections.forEach((connection) => { connections.forEach((connection) => {
this.handleHookshotEvent(msg, connection, handler); void this.handleHookshotEvent(msg, connection, handler);
}) });
}); });
} }