Skip to content

Commit

Permalink
resolve Type errors and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
s1gr1d committed Oct 3, 2024
1 parent 7895693 commit 14b7526
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/browser/src/eventbuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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];
}
Expand Down
5 changes: 5 additions & 0 deletions packages/utils/test/is.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 14b7526

Please sign in to comment.