Skip to content

Commit d02559c

Browse files
authored
Make error handling in decryptionLoop more generic (#3024)
Not everything is a `DecryptionError`, and there's no real reason that we should only do retries for `DecryptionError`s
1 parent ec6272a commit d02559c

File tree

2 files changed

+19
-34
lines changed

2 files changed

+19
-34
lines changed

spec/unit/models/event.spec.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ limitations under the License.
1616

1717
import { MatrixEvent, MatrixEventEvent } from "../../../src/models/event";
1818
import { emitPromise } from "../../test-utils/test-utils";
19-
import { EventType } from "../../../src";
2019
import { Crypto } from "../../../src/crypto";
2120

2221
describe("MatrixEvent", () => {
@@ -88,22 +87,6 @@ describe("MatrixEvent", () => {
8887
expect(ev.getWireContent().ciphertext).toBeUndefined();
8988
});
9089

91-
it("should abort decryption if fails with an error other than a DecryptionError", async () => {
92-
const ev = new MatrixEvent({
93-
type: EventType.RoomMessageEncrypted,
94-
content: {
95-
body: "Test",
96-
},
97-
event_id: "$event1:server",
98-
});
99-
await ev.attemptDecryption({
100-
decryptEvent: jest.fn().mockRejectedValue(new Error("Not a DecryptionError")),
101-
} as unknown as Crypto);
102-
expect(ev.isEncrypted()).toBeTruthy();
103-
expect(ev.isBeingDecrypted()).toBeFalsy();
104-
expect(ev.isDecryptionFailure()).toBeFalsy();
105-
});
106-
10790
describe("applyVisibilityEvent", () => {
10891
it("should emit VisibilityChange if a change was made", async () => {
10992
const ev = new MatrixEvent({
@@ -134,6 +117,21 @@ describe("MatrixEvent", () => {
134117
});
135118
});
136119

120+
it("should report decryption errors", async () => {
121+
const crypto = {
122+
decryptEvent: jest.fn().mockRejectedValue(new Error("test error")),
123+
} as unknown as Crypto;
124+
125+
await encryptedEvent.attemptDecryption(crypto);
126+
expect(encryptedEvent.isEncrypted()).toBeTruthy();
127+
expect(encryptedEvent.isBeingDecrypted()).toBeFalsy();
128+
expect(encryptedEvent.isDecryptionFailure()).toBeTruthy();
129+
expect(encryptedEvent.getContent()).toEqual({
130+
msgtype: "m.bad.encrypted",
131+
body: "** Unable to decrypt: Error: test error **",
132+
});
133+
});
134+
137135
it("should retry decryption if a retry is queued", async () => {
138136
const eventAttemptDecryptionSpy = jest.spyOn(encryptedEvent, "attemptDecryption");
139137

src/models/event.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -828,17 +828,7 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
828828
}
829829
}
830830
} catch (e) {
831-
if ((<Error>e).name !== "DecryptionError") {
832-
// not a decryption error: log the whole exception as an error
833-
// (and don't bother with a retry)
834-
const re = options.isRetry ? "re" : "";
835-
// For find results: this can produce "Error decrypting event (id=$ev)" and
836-
// "Error redecrypting event (id=$ev)".
837-
logger.error(`Error ${re}decrypting event (${this.getDetails()})`, e);
838-
this.decryptionPromise = null;
839-
this.retryDecryption = false;
840-
return;
841-
}
831+
const detailedError = e instanceof DecryptionError ? (<DecryptionError>e).detailedString : String(e);
842832

843833
err = e as Error;
844834

@@ -858,10 +848,7 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
858848
//
859849
if (this.retryDecryption) {
860850
// decryption error, but we have a retry queued.
861-
logger.log(
862-
`Error decrypting event (${this.getDetails()}), but retrying: ` +
863-
(<DecryptionError>e).detailedString,
864-
);
851+
logger.log(`Error decrypting event (${this.getDetails()}), but retrying: ${detailedError}`);
865852
continue;
866853
}
867854

@@ -870,9 +857,9 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
870857
//
871858
// the detailedString already includes the name and message of the error, and the stack isn't much use,
872859
// so we don't bother to log `e` separately.
873-
logger.warn(`Error decrypting event (${this.getDetails()}): ` + (<DecryptionError>e).detailedString);
860+
logger.warn(`Error decrypting event (${this.getDetails()}): ${detailedError}`);
874861

875-
res = this.badEncryptedMessage((<DecryptionError>e).message);
862+
res = this.badEncryptedMessage(String(e));
876863
}
877864

878865
// at this point, we've either successfully decrypted the event, or have given up

0 commit comments

Comments
 (0)