From 1269fa7246284a670e39f48f154b9287edb7c1fb Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Thu, 3 Mar 2022 16:07:20 -0800 Subject: [PATCH] fix: Make catch clauses compatible with TypeScript 4.4 (#116) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In TypeScript 4.4, --strict implies --useUnknownInCatchVariables, so in a ‘catch (error)’ block, ‘error’ has type ‘unknown’ rather than ‘any’. Signed-off-by: Anders Kaseorg --- src/main/server.ts | 10 ++++++++-- test/all.ts | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/server.ts b/src/main/server.ts index abdd4c9..9555198 100644 --- a/src/main/server.ts +++ b/src/main/server.ts @@ -467,7 +467,10 @@ export function initialize() { try { return valueToMeta(event.sender, contextId, func(...args), true) } catch (error) { - const err = new Error(`Could not call remote function '${func.name || 'anonymous'}'. Check that the function signature is correct. Underlying error: ${error.message}\nUnderlying stack: ${error.stack}\n`); + const err = new Error( + `Could not call remote function '${func.name || "anonymous"}'. Check that the function signature is correct. Underlying error: ${error}\n` + + (error instanceof Error ? `Underlying stack: ${error.stack}\n` : "") + ); (err as any).cause = error throw err } @@ -495,7 +498,10 @@ export function initialize() { try { return valueToMeta(event.sender, contextId, object[method](...args), true) } catch (error) { - const err = new Error(`Could not call remote method '${method}'. Check that the method signature is correct. Underlying error: ${error.message}\nUnderlying stack: ${error.stack}\n`); + const err = new Error( + `Could not call remote method '${method}'. Check that the method signature is correct. Underlying error: ${error}` + + (error instanceof Error ? `Underlying stack: ${error.stack}\n` : "") + ); (err as any).cause = error throw err } diff --git a/test/all.ts b/test/all.ts index 30dd789..8e24af8 100644 --- a/test/all.ts +++ b/test/all.ts @@ -1007,7 +1007,7 @@ describe('remote module', () => { try { throwFunction(new Error('error from main')) expect.fail() - } catch (e) { + } catch (e: any) { expect(e.message).to.match(/Could not call remote function/) expect(e.cause.message).to.equal('error from main') }