Skip to content

Commit 3d3b914

Browse files
committed
fix: if there is a crash we dont have time to get the event
Also disable uncaught and discarded by default
1 parent b73b021 commit 3d3b914

File tree

1 file changed

+69
-57
lines changed

1 file changed

+69
-57
lines changed

src/integrations/nativescripterrorhandlers.ts

Lines changed: 69 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getCurrentHub } from '@sentry/core';
2+
import { eventFromUnknownInput } from '@sentry/browser/esm/eventbuilder';
23
import { Integration, SeverityLevel } from '@sentry/types';
34
import { NATIVE } from '../wrapper';
45
import { addExceptionMechanism, getGlobalObject, logger } from '@sentry/utils';
@@ -55,8 +56,8 @@ export class NativescriptErrorHandlers implements Integration {
5556
public constructor(options?: NativescriptErrorHandlersOptions) {
5657
this._options = {
5758
// uncaughtErrors: false,
58-
onerror: true,
59-
onunhandledrejection: true,
59+
onerror: false,
60+
onunhandledrejection: false,
6061
patchGlobalPromise: true,
6162
...options,
6263
};
@@ -66,78 +67,88 @@ export class NativescriptErrorHandlers implements Integration {
6667
* @inheritDoc
6768
*/
6869
public setupOnce(): void {
69-
// this._handleUnhandledRejections();
70+
this._handleUnhandledRejections();
7071
this._handleOnError();
7172
}
7273

7374
/**
7475
* Handle Promises
7576
*/
76-
// private _handleUnhandledRejections(): void {
77-
// if (this._options.onunhandledrejection) {
78-
// if (this._options.uncaughtErrors) {
79-
// Application.on(Application.uncaughtErrorEvent, this.globalHanderEvent, this);
80-
// }
81-
// if (this._options.patchGlobalPromise) {
82-
// this._polyfillPromise();
83-
// }
84-
85-
// this._attachUnhandledRejectionHandler();
86-
// this._checkPromiseAndWarn();
87-
// }
88-
// }
77+
private _handleUnhandledRejections(): void {
78+
if (this._options.onunhandledrejection) {
79+
// if (this._options.uncaughtErrors) {
80+
Application.on(Application.uncaughtErrorEvent, this.globalHanderEvent, this);
81+
// }
82+
// if (this._options.patchGlobalPromise) {
83+
// this._polyfillPromise();
84+
// }
85+
86+
// this._attachUnhandledRejectionHandler();
87+
// this._checkPromiseAndWarn();
88+
}
89+
}
8990

9091
private globalHanderEvent(event) {
9192
this.globalHander(event.error);
9293
}
9394
handlingFatal = false;
9495

95-
private async globalHander(error: any, isFatal?: boolean) {
96-
console.log('globalHander', error, isFatal);
97-
// We want to handle fatals, but only in production mode.
98-
const shouldHandleFatal = isFatal && !__DEV__;
99-
if (shouldHandleFatal) {
100-
if (this.handlingFatal) {
101-
logger.log(
102-
'Encountered multiple fatals in a row. The latest:',
103-
error
104-
);
105-
return;
96+
private globalHander(error: any, isFatal?: boolean) {
97+
console.log('globalHander', Object.keys(error));
98+
try {
99+
// We want to handle fatals, but only in production mode.
100+
const shouldHandleFatal = isFatal && !__DEV__;
101+
if (shouldHandleFatal) {
102+
if (this.handlingFatal) {
103+
logger.log(
104+
'Encountered multiple fatals in a row. The latest:',
105+
error
106+
);
107+
return;
108+
}
109+
this.handlingFatal = true;
106110
}
107-
this.handlingFatal = true;
108-
}
109-
110-
const currentHub = getCurrentHub();
111-
const client = currentHub.getClient<NativescriptClient>();
112-
113-
if (!client) {
114-
logger.error(
115-
'Sentry client is missing, the error event might be lost.',
116-
error
117-
);
118111

119-
// If there is no client something is fishy, anyway we call the default handler
120-
// defaultHandler(error, isFatal);
112+
const currentHub = getCurrentHub();
113+
const client = currentHub.getClient<NativescriptClient>();
121114

122-
return;
123-
}
124-
125-
const options = client.getOptions();
115+
if (!client) {
116+
logger.error(
117+
'Sentry client is missing, the error event might be lost.',
118+
error
119+
);
126120

127-
const event = await client.eventFromException(error, {
128-
originalException: error
129-
});
121+
// If there is no client something is fishy, anyway we call the default handler
122+
// defaultHandler(error, isFatal);
130123

131-
if (isFatal) {
132-
event.level = 'fatal' as SeverityLevel;
124+
return;
125+
}
126+
if (error['stackTrace']) {
127+
error['stacktrace'] = error['stackTrace'];
128+
}
129+
// const syntheticException = (hint && hint.syntheticException) || undefined;
130+
const event = eventFromUnknownInput(client.getOptions().stackParser, error, undefined, true);
131+
addExceptionMechanism(event); // defaults to { type: 'generic', handled: true }
132+
event.level = 'error';
133+
// const event = await client.eventFromException(error, {
134+
// originalException: error
135+
// });
136+
137+
if (isFatal) {
138+
event.level = 'fatal' as SeverityLevel;
139+
140+
addExceptionMechanism(event, {
141+
handled: false,
142+
type: 'onerror',
143+
});
144+
}
133145

134-
addExceptionMechanism(event, {
135-
handled: false,
136-
type: 'onerror',
137-
});
146+
const result = client.captureEvent(event);
147+
console.log('globalHander2', result, Object.keys(event));
148+
} catch (error) {
149+
console.error(error);
138150
}
139151

140-
currentHub.captureEvent(event);
141152

142153
// if (!__DEV__) {
143154
// void client.flush(options.shutdownTimeout || 2000).then(() => {
@@ -156,11 +167,12 @@ export class NativescriptErrorHandlers implements Integration {
156167
private _handleOnError(): void {
157168
if (this._options.onerror) {
158169
// let handlingFatal = false;
170+
// Application.on(Application.uncaughtErrorEvent, this.globalHanderEvent, this);
159171
Application.on(Application.discardedErrorEvent, this.globalHanderEvent, this);
160172

161-
Trace.setErrorHandler({
162-
handlerError: this.globalHander
163-
});
173+
// Trace.setErrorHandler({
174+
// handlerError: this.globalHander
175+
// });
164176
// const defaultHandler = ErrorUtils.getGlobalHandler && ErrorUtils.getGlobalHandler();
165177

166178
// ErrorUtils.setGlobalHandler);

0 commit comments

Comments
 (0)