From 07d80700374cf9f23164ccf0b6c6c52f53b7ccf7 Mon Sep 17 00:00:00 2001 From: Valere Date: Thu, 5 May 2022 05:46:03 +0200 Subject: [PATCH] Add raw error to analytics E2E error event context (#8447) * Add raw error to analytics E2E error context * Fix code style Co-authored-by: Michael Telatynski <7t3chguy@gmail.com> --- src/DecryptionFailureTracker.ts | 7 ++++--- test/DecryptionFailureTracker-test.js | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/DecryptionFailureTracker.ts b/src/DecryptionFailureTracker.ts index 2bb522e7fe..c56b245f25 100644 --- a/src/DecryptionFailureTracker.ts +++ b/src/DecryptionFailureTracker.ts @@ -31,18 +31,19 @@ export class DecryptionFailure { type ErrorCode = "OlmKeysNotSentError" | "OlmIndexError" | "UnknownError" | "OlmUnspecifiedError"; -type TrackingFn = (count: number, trackedErrCode: ErrorCode) => void; +type TrackingFn = (count: number, trackedErrCode: ErrorCode, rawError: string) => void; export type ErrCodeMapFn = (errcode: string) => ErrorCode; export class DecryptionFailureTracker { - private static internalInstance = new DecryptionFailureTracker((total, errorCode) => { + private static internalInstance = new DecryptionFailureTracker((total, errorCode, rawError) => { Analytics.trackEvent('E2E', 'Decryption failure', errorCode, String(total)); for (let i = 0; i < total; i++) { PosthogAnalytics.instance.trackEvent({ eventName: "Error", domain: "E2EE", name: errorCode, + context: `mxc_crypto_error_type_${rawError}`, }); } }, (errorCode) => { @@ -236,7 +237,7 @@ export class DecryptionFailureTracker { if (this.failureCounts[errorCode] > 0) { const trackedErrorCode = this.errorCodeMapFn(errorCode); - this.fn(this.failureCounts[errorCode], trackedErrorCode); + this.fn(this.failureCounts[errorCode], trackedErrorCode, errorCode); this.failureCounts[errorCode] = 0; } } diff --git a/test/DecryptionFailureTracker-test.js b/test/DecryptionFailureTracker-test.js index b0494f97aa..997c4913c3 100644 --- a/test/DecryptionFailureTracker-test.js +++ b/test/DecryptionFailureTracker-test.js @@ -57,6 +57,33 @@ describe('DecryptionFailureTracker', function() { done(); }); + it('tracks a failed decryption with expected raw error for a visible event', function(done) { + const failedDecryptionEvent = createFailedDecryptionEvent(); + + let count = 0; + let reportedRawCode = ""; + const tracker = new DecryptionFailureTracker((total, errcode, rawCode) => { + count += total; + reportedRawCode = rawCode; + }, () => "UnknownError"); + + tracker.addVisibleEvent(failedDecryptionEvent); + + const err = new MockDecryptionError('INBOUND_SESSION_MISMATCH_ROOM_ID'); + tracker.eventDecrypted(failedDecryptionEvent, err); + + // Pretend "now" is Infinity + tracker.checkFailures(Infinity); + + // Immediately track the newest failures + tracker.trackFailures(); + + expect(count).not.toBe(0, 'should track a failure for an event that failed decryption'); + expect(reportedRawCode).toBe('INBOUND_SESSION_MISMATCH_ROOM_ID', 'Should add the rawCode to the event context'); + + done(); + }); + it('tracks a failed decryption for an event that becomes visible later', function(done) { const failedDecryptionEvent = createFailedDecryptionEvent();