From 3d3b9142335d481f01e6c52560be569048d495ae Mon Sep 17 00:00:00 2001 From: Martin Guillon Date: Fri, 9 Dec 2022 11:48:44 +0100 Subject: [PATCH] fix: if there is a crash we dont have time to get the event Also disable uncaught and discarded by default --- src/integrations/nativescripterrorhandlers.ts | 126 ++++++++++-------- 1 file changed, 69 insertions(+), 57 deletions(-) diff --git a/src/integrations/nativescripterrorhandlers.ts b/src/integrations/nativescripterrorhandlers.ts index 023c4ea..a0361b2 100755 --- a/src/integrations/nativescripterrorhandlers.ts +++ b/src/integrations/nativescripterrorhandlers.ts @@ -1,4 +1,5 @@ import { getCurrentHub } from '@sentry/core'; +import { eventFromUnknownInput } from '@sentry/browser/esm/eventbuilder'; import { Integration, SeverityLevel } from '@sentry/types'; import { NATIVE } from '../wrapper'; import { addExceptionMechanism, getGlobalObject, logger } from '@sentry/utils'; @@ -55,8 +56,8 @@ export class NativescriptErrorHandlers implements Integration { public constructor(options?: NativescriptErrorHandlersOptions) { this._options = { // uncaughtErrors: false, - onerror: true, - onunhandledrejection: true, + onerror: false, + onunhandledrejection: false, patchGlobalPromise: true, ...options, }; @@ -66,78 +67,88 @@ export class NativescriptErrorHandlers implements Integration { * @inheritDoc */ public setupOnce(): void { - // this._handleUnhandledRejections(); + this._handleUnhandledRejections(); this._handleOnError(); } /** * Handle Promises */ - // private _handleUnhandledRejections(): void { - // if (this._options.onunhandledrejection) { - // if (this._options.uncaughtErrors) { - // Application.on(Application.uncaughtErrorEvent, this.globalHanderEvent, this); - // } - // if (this._options.patchGlobalPromise) { - // this._polyfillPromise(); - // } - - // this._attachUnhandledRejectionHandler(); - // this._checkPromiseAndWarn(); - // } - // } + private _handleUnhandledRejections(): void { + if (this._options.onunhandledrejection) { + // if (this._options.uncaughtErrors) { + Application.on(Application.uncaughtErrorEvent, this.globalHanderEvent, this); + // } + // if (this._options.patchGlobalPromise) { + // this._polyfillPromise(); + // } + + // this._attachUnhandledRejectionHandler(); + // this._checkPromiseAndWarn(); + } + } private globalHanderEvent(event) { this.globalHander(event.error); } handlingFatal = false; - private async globalHander(error: any, isFatal?: boolean) { - console.log('globalHander', error, isFatal); - // We want to handle fatals, but only in production mode. - const shouldHandleFatal = isFatal && !__DEV__; - if (shouldHandleFatal) { - if (this.handlingFatal) { - logger.log( - 'Encountered multiple fatals in a row. The latest:', - error - ); - return; + private globalHander(error: any, isFatal?: boolean) { + console.log('globalHander', Object.keys(error)); + try { + // We want to handle fatals, but only in production mode. + const shouldHandleFatal = isFatal && !__DEV__; + if (shouldHandleFatal) { + if (this.handlingFatal) { + logger.log( + 'Encountered multiple fatals in a row. The latest:', + error + ); + return; + } + this.handlingFatal = true; } - this.handlingFatal = true; - } - - const currentHub = getCurrentHub(); - const client = currentHub.getClient(); - - if (!client) { - logger.error( - 'Sentry client is missing, the error event might be lost.', - error - ); - // If there is no client something is fishy, anyway we call the default handler - // defaultHandler(error, isFatal); + const currentHub = getCurrentHub(); + const client = currentHub.getClient(); - return; - } - - const options = client.getOptions(); + if (!client) { + logger.error( + 'Sentry client is missing, the error event might be lost.', + error + ); - const event = await client.eventFromException(error, { - originalException: error - }); + // If there is no client something is fishy, anyway we call the default handler + // defaultHandler(error, isFatal); - if (isFatal) { - event.level = 'fatal' as SeverityLevel; + return; + } + if (error['stackTrace']) { + error['stacktrace'] = error['stackTrace']; + } + // const syntheticException = (hint && hint.syntheticException) || undefined; + const event = eventFromUnknownInput(client.getOptions().stackParser, error, undefined, true); + addExceptionMechanism(event); // defaults to { type: 'generic', handled: true } + event.level = 'error'; + // const event = await client.eventFromException(error, { + // originalException: error + // }); + + if (isFatal) { + event.level = 'fatal' as SeverityLevel; + + addExceptionMechanism(event, { + handled: false, + type: 'onerror', + }); + } - addExceptionMechanism(event, { - handled: false, - type: 'onerror', - }); + const result = client.captureEvent(event); + console.log('globalHander2', result, Object.keys(event)); + } catch (error) { + console.error(error); } - currentHub.captureEvent(event); // if (!__DEV__) { // void client.flush(options.shutdownTimeout || 2000).then(() => { @@ -156,11 +167,12 @@ export class NativescriptErrorHandlers implements Integration { private _handleOnError(): void { if (this._options.onerror) { // let handlingFatal = false; + // Application.on(Application.uncaughtErrorEvent, this.globalHanderEvent, this); Application.on(Application.discardedErrorEvent, this.globalHanderEvent, this); - Trace.setErrorHandler({ - handlerError: this.globalHander - }); + // Trace.setErrorHandler({ + // handlerError: this.globalHander + // }); // const defaultHandler = ErrorUtils.getGlobalHandler && ErrorUtils.getGlobalHandler(); // ErrorUtils.setGlobalHandler);