Skip to content

Commit

Permalink
fix: Make catch clauses compatible with TypeScript 4.4 (#116)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
andersk authored Mar 4, 2022
1 parent 0dd6549 commit 1269fa7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/main/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion test/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand Down

0 comments on commit 1269fa7

Please sign in to comment.