From 037674115a5bffdcf2a7cb45ff3d240d35cd954c Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Fri, 24 Mar 2023 14:39:19 +0000 Subject: [PATCH] Wrap createConnectionForState/ensureGrant in a try/catch (#680) * Fix thrown exception when the state is not granted * changelog * make it an error * Typing --- changelog.d/680.bugfix | 1 + src/ConnectionManager.ts | 34 +++++++++++++++++++--------------- 2 files changed, 20 insertions(+), 15 deletions(-) create mode 100644 changelog.d/680.bugfix diff --git a/changelog.d/680.bugfix b/changelog.d/680.bugfix new file mode 100644 index 00000000..17ed4035 --- /dev/null +++ b/changelog.d/680.bugfix @@ -0,0 +1 @@ +Fix a missing grant for a connection sometimes causing a crash. \ No newline at end of file diff --git a/src/ConnectionManager.ts b/src/ConnectionManager.ts index b509c7e1..f131a132 100644 --- a/src/ConnectionManager.ts +++ b/src/ConnectionManager.ts @@ -173,7 +173,7 @@ export class ConnectionManager extends EventEmitter { * @param rollbackBadState * @returns */ - public async createConnectionForState(roomId: string, state: StateEvent, rollbackBadState: boolean) { + public async createConnectionForState(roomId: string, state: StateEvent, rollbackBadState: boolean): Promise { // Empty object == redacted if (state.content.disabled === true || Object.keys(state.content).length === 0) { log.debug(`${roomId} has disabled state for ${state.type}`); @@ -195,20 +195,24 @@ export class ConnectionManager extends EventEmitter { return; } - const connection = await connectionType.createConnectionForState(roomId, state, { - as: this.as, - intent: botUser.intent, - config: this.config, - tokenStore: this.tokenStore, - commentProcessor: this.commentProcessor, - messageClient: this.messageClient, - storage: this.storage, - github: this.github, - }); - - // Finally, ensure the connection is allowed by us. - await connection.ensureGrant?.(state.sender); - return connection; + try { + const connection = await connectionType.createConnectionForState(roomId, state, { + as: this.as, + intent: botUser.intent, + config: this.config, + tokenStore: this.tokenStore, + commentProcessor: this.commentProcessor, + messageClient: this.messageClient, + storage: this.storage, + github: this.github, + }); + // Finally, ensure the connection is allowed by us. + await connection.ensureGrant?.(state.sender); + return connection; + } catch (ex) { + log.error(`Not creating connection for state ${roomId}/${state.type}`, ex); + return; + } } /**