From 6143888f6c2d2e2c03c9b10b0c2ca9bdfe346046 Mon Sep 17 00:00:00 2001 From: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> Date: Tue, 13 Aug 2024 13:42:20 -0700 Subject: [PATCH] Better database errors in worker thread --- app/main.ts | 5 +++-- ts/sql/main.ts | 12 ++++++++++-- ts/sql/mainWorker.ts | 12 ++++++++---- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/app/main.ts b/app/main.ts index f01ac17735f0..4e2cfad86ebc 100644 --- a/app/main.ts +++ b/app/main.ts @@ -117,7 +117,6 @@ import { load as loadLocale } from './locale'; import type { LoggerType } from '../ts/types/Logging'; import { HourCyclePreference } from '../ts/types/I18N'; import { ScreenShareStatus } from '../ts/types/Calling'; -import { DBVersionFromFutureError } from '../ts/sql/migrations'; import type { ParsedSignalRoute } from '../ts/util/signalRoutes'; import { parseSignalRoute } from '../ts/util/signalRoutes'; import * as dns from '../ts/util/dns'; @@ -1800,7 +1799,9 @@ const onDatabaseError = async (error: Error) => { const copyErrorAndQuitButtonIndex = 0; const SIGNAL_SUPPORT_LINK = 'https://support.signal.org/error'; - if (error instanceof DBVersionFromFutureError) { + // Note that this error is thrown by the worker process and thus instanceof + // check won't work. + if (error.name === 'DBVersionFromFutureError') { // If the DB version is too new, the user likely opened an older version of Signal, // and they would almost never want to delete their data as a result, so we don't show // that option diff --git a/ts/sql/main.ts b/ts/sql/main.ts index 33de0ecc84a8..9ec713c0679a 100644 --- a/ts/sql/main.ts +++ b/ts/sql/main.ts @@ -70,7 +70,13 @@ export type WrappedWorkerResponse = | Readonly<{ type: 'response'; seq: number; - error: string | undefined; + error: + | Readonly<{ + name: string; + message: string; + stack: string | undefined; + }> + | undefined; errorKind: SqliteErrorKind | undefined; // eslint-disable-next-line @typescript-eslint/no-explicit-any response: any; @@ -382,7 +388,9 @@ export class MainSQL { } if (error) { - const errorObj = new Error(error); + const errorObj = new Error(error.message); + errorObj.stack = error.stack; + errorObj.name = error.name; this.onError(errorKind ?? SqliteErrorKind.Unknown, errorObj); pair.reject(errorObj); diff --git a/ts/sql/mainWorker.ts b/ts/sql/mainWorker.ts index 4d274fde6472..bdb179cce355 100644 --- a/ts/sql/mainWorker.ts +++ b/ts/sql/mainWorker.ts @@ -4,7 +4,6 @@ import { parentPort } from 'worker_threads'; import type { LoggerType } from '../types/Logging'; -import * as Errors from '../types/errors'; import type { WrappedWorkerRequest, WrappedWorkerResponse, @@ -23,10 +22,8 @@ const port = parentPort; // eslint-disable-next-line @typescript-eslint/no-explicit-any function respond(seq: number, error: Error | undefined, response?: any) { let errorKind: SqliteErrorKind | undefined; - let errorString: string | undefined; if (error !== undefined) { errorKind = parseSqliteError(error); - errorString = Errors.toLogFormat(error); if (errorKind === SqliteErrorKind.Corrupted && db != null) { DataWriter.runCorruptionChecks(db); @@ -36,7 +33,14 @@ function respond(seq: number, error: Error | undefined, response?: any) { const wrappedResponse: WrappedWorkerResponse = { type: 'response', seq, - error: errorString, + error: + error == null + ? undefined + : { + name: error.name, + message: error.message, + stack: error.stack, + }, errorKind, response, };