From 14b7526179f3fb71a658c055da36c0bb10ca5ceb Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Thu, 3 Oct 2024 10:14:38 +0200 Subject: [PATCH] resolve Type errors and add test --- packages/browser/src/eventbuilder.ts | 13 ++++++++++--- packages/utils/test/is.test.ts | 5 +++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/browser/src/eventbuilder.ts b/packages/browser/src/eventbuilder.ts index ab10ce66e175..973515b0a2e8 100644 --- a/packages/browser/src/eventbuilder.ts +++ b/packages/browser/src/eventbuilder.ts @@ -159,14 +159,21 @@ function getPopFirstTopFrames(ex: Error & { framesToPop?: unknown }): number { return 0; } +// https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/Exception +// @ts-expect-error - WebAssembly.Exception is a valid class +function isWebAssemblyException(exception: unknown): exception is WebAssembly.Exception { + // @ts-expect-error - WebAssembly.Exception is a valid class + return exception instanceof WebAssembly.Exception; +} + /** * There are cases where error is an WebAssembly.Exception object * https://github.com/getsentry/sentry-javascript/issues/13787 * In this specific case we try to extract name/type from .message in WebAssembly.Exception */ -function extractName(ex: Error & { message: { error?: Error } }): string { +function extractName(ex: Error & { message: { error?: Error } }): string | undefined { const name = ex && ex.name; - if (!name && ex instanceof WebAssembly.Exception) { + if (!name && isWebAssemblyException(ex)) { // Emscripten sets array[type, message] to the "message" property on the WebAssembly.Exception object const hasTypeInMessage = ex.message && Array.isArray(ex.message) && ex.message.length == 2; return hasTypeInMessage ? ex.message[0] : 'WebAssembly.Exception'; @@ -187,7 +194,7 @@ function extractMessage(ex: Error & { message: { error?: Error } }): string { if (message.error && typeof message.error.message === 'string') { return message.error.message; } - if (ex instanceof WebAssembly.Exception && Array.isArray(ex.message) && ex.message.length == 2) { + if (isWebAssemblyException(ex) && Array.isArray(ex.message) && ex.message.length == 2) { // Emscripten sets array[type, message] to the "message" property on the WebAssembly.Exception object return ex.message[1]; } diff --git a/packages/utils/test/is.test.ts b/packages/utils/test/is.test.ts index 1ccfc2cd1754..c1a1c15dcbb4 100644 --- a/packages/utils/test/is.test.ts +++ b/packages/utils/test/is.test.ts @@ -46,6 +46,11 @@ describe('isError()', () => { expect(isError(new Error())).toEqual(true); expect(isError(new ReferenceError())).toEqual(true); expect(isError(new SentryError('message'))).toEqual(true); + // https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/Exception/Exception#examples + // @ts-expect-error - WebAssembly.Tag is a valid constructor + const tag = new WebAssembly.Tag({ parameters: ['i32', 'f32'] }); + // @ts-expect-error - WebAssembly.Exception is a valid constructor + expect(isError(new WebAssembly.Exception(tag, [42, 42.3]))).toBe(true); expect(isError({})).toEqual(false); expect( isError({