Skip to content

Commit

Permalink
fix: if there is a crash we dont have time to get the event
Browse files Browse the repository at this point in the history
Also disable uncaught and discarded by default
  • Loading branch information
farfromrefug committed Dec 9, 2022
1 parent b73b021 commit 3d3b914
Showing 1 changed file with 69 additions and 57 deletions.
126 changes: 69 additions & 57 deletions src/integrations/nativescripterrorhandlers.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
};
Expand All @@ -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<NativescriptClient>();

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<NativescriptClient>();

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(() => {
Expand All @@ -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);
Expand Down

0 comments on commit 3d3b914

Please sign in to comment.