Skip to content

Commit 21f6386

Browse files
committed
fix(ios): better error handling
1 parent 1f6989c commit 21f6386

File tree

2 files changed

+70
-23
lines changed

2 files changed

+70
-23
lines changed

src/sentry/client.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,21 @@ export class NativescriptClient extends BaseClient<NativescriptClientOptions> {
7979
} catch (error) {
8080
console.error(error, error.stack);
8181
}
82+
} else if (__IOS__ && exception['stackTrace']) {
83+
try {
84+
const stack = parseErrorStack({ stack: 'at ' + exception['stackTrace'] } as any).filter((f) => f.platform !== 'javascript');
85+
// stack.forEach((frame) => rewriteFrameIntegration._iteratee(frame));
86+
event.exception.values[0].stacktrace.frames.forEach((frame) => rewriteFrameIntegration._iteratee(frame));
87+
// event.exception.values.unshift({
88+
// type: 'NativeException',
89+
// value: exception.toString(),
90+
// stacktrace: {
91+
// frames: stack
92+
// }
93+
// });
94+
} catch (error) {
95+
console.error(error, error.stack);
96+
}
8297
}
8398
return event;
8499
// return this._browserClient.eventFromException(exception, hint);

src/sentry/wrapper.ios.ts

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function dataSerialize(data?: any, wrapPrimitives?: boolean) {
6262
}
6363
}
6464

65-
const FATAL_ERROR_REGEXP = /NativeScript encountered a fatal error: (.*?)\n at \n(\t*)?(.*)$/m;
65+
const FATAL_ERROR_REGEXP = /NativeScript encountered a fatal error:([^]*?) at([\t\n\s]*)?([^]*)$/m;
6666

6767
export namespace NATIVE {
6868
let enableNative = true;
@@ -324,7 +324,17 @@ export namespace NATIVE {
324324
return false;
325325
}
326326
sentryOptions = options;
327-
const { tracesSampleRate, tracesSampler, beforeSend, beforeBreadcrumb, ...toPassOptions } = options;
327+
const {
328+
enableCrashHandler,
329+
enableNativeCrashHandling,
330+
enableAutoPerformanceTracking,
331+
tracesSampleRate,
332+
tracesSampler,
333+
beforeSend,
334+
beforeBreadcrumb,
335+
disabledNativeIntegrations,
336+
...toPassOptions
337+
} = options;
328338

329339
Object.keys(toPassOptions).forEach((k) => {
330340
const value = toPassOptions[k];
@@ -339,18 +349,29 @@ export namespace NATIVE {
339349

340350
// before send right now is never called when we send the envelope. Only on native crash
341351
nSentryOptions.beforeSend = (event: SentryEvent) => {
342-
const exception = event.exceptions?.objectAtIndex(0);
343-
const exceptionvalue = exception?.value;
344-
if (exceptionvalue) {
345-
const matches = exceptionvalue.match(FATAL_ERROR_REGEXP);
346-
if (matches) {
347-
const errorMessage = matches[1];
348-
const jsStackTrace = exceptionvalue.substring(exceptionvalue.indexOf(matches[2]));
349-
const stack = parseErrorStack({ stack: 'at ' + jsStackTrace } as any).reverse();
350-
stack.forEach((frame) => rewriteFrameIntegration._iteratee(frame));
351-
addJavascriptExceptionInterface(event, 'Error', errorMessage, stack.reverse());
352-
exception.type = 'NativeScriptException';
353-
exception.value = errorMessage;
352+
const exceptions = event.exceptions;
353+
const count = exceptions?.count;
354+
// if enableCrashHandler is disabled we actually dont disable it but prevent event Sending
355+
// the reason is that without SentryCrashIntegration the scope does not get augmented and we loose info
356+
if (enableCrashHandler === false) {
357+
return null;
358+
}
359+
if (count) {
360+
for (let index = 0; index < exceptions.count; index++) {
361+
const exception = exceptions.objectAtIndex(index);
362+
const exceptionvalue = exception.value;
363+
if (exceptionvalue) {
364+
const matches = exceptionvalue.match(FATAL_ERROR_REGEXP);
365+
if (matches) {
366+
const errorMessage = matches[1];
367+
const jsStackTrace = exceptionvalue.substring(exceptionvalue.indexOf(matches[2]));
368+
const stack = parseErrorStack({ stack: 'at ' + jsStackTrace } as any).reverse();
369+
stack.forEach((frame) => rewriteFrameIntegration._iteratee(frame));
370+
addJavascriptExceptionInterface(event, 'Error', errorMessage, stack.reverse());
371+
exception.type = 'NativeScriptException';
372+
exception.value = errorMessage;
373+
}
374+
}
354375
}
355376
}
356377
if (beforeSend) {
@@ -376,18 +397,29 @@ export namespace NATIVE {
376397
}
377398
return breadcrumb;
378399
};
379-
if (toPassOptions.hasOwnProperty('enableNativeCrashHandling')) {
380-
if (!toPassOptions.enableNativeCrashHandling) {
381-
const integrations = nSentryOptions.integrations.mutableCopy();
382-
integrations.removeObject('SentryCrashIntegration');
383-
nSentryOptions.integrations = integrations;
400+
if (enableNativeCrashHandling === false) {
401+
const integrations = nSentryOptions.integrations.mutableCopy();
402+
integrations.removeObject('SentryCrashIntegration');
403+
nSentryOptions.integrations = integrations;
404+
}
405+
if (disabledNativeIntegrations) {
406+
const integrations = nSentryOptions.integrations.mutableCopy() as NSMutableArray<any>;
407+
const size = integrations.count;
408+
for (let index = size - 1; index >= 0; index--) {
409+
const inte = integrations.objectAtIndex(index);
410+
if (disabledNativeIntegrations.indexOf(inte) !== -1) {
411+
integrations.removeObject(inte);
412+
}
384413
}
414+
nSentryOptions.integrations = integrations;
385415
}
386416

387-
if (toPassOptions.hasOwnProperty('enableAutoPerformanceTracking')) {
388-
NSSentrySDK.appStartMeasurementHybridSDKMode = toPassOptions.enableAutoPerformanceTracking;
389-
NSSentrySDK.framesTrackingMeasurementHybridSDKMode = toPassOptions.enableAutoPerformanceTracking;
417+
if (enableAutoPerformanceTracking !== undefined) {
418+
NSSentrySDK.appStartMeasurementHybridSDKMode = enableAutoPerformanceTracking;
419+
NSSentrySDK.framesTrackingMeasurementHybridSDKMode = enableAutoPerformanceTracking;
390420
}
421+
const sdkVersion = PrivateSentrySDKOnly.getSdkVersionString();
422+
PrivateSentrySDKOnly.setSdkNameAndVersionString('sentry.cocoa.nativescript', sdkVersion);
391423
NSSentrySDK.startWithOptions(nSentryOptions);
392424

393425
return true;
@@ -422,7 +454,7 @@ export namespace NATIVE {
422454
console.error('fetchNativeDeviceContexts', error, error.stack);
423455
}
424456
});
425-
const contexts = serializedScope.context;
457+
const contexts = serializedScope.context || {};
426458
const extraContextDict = PrivateSentrySDKOnly.getExtraContext();
427459
if (extraContextDict) {
428460
const extraContext = dictToJSON(extraContextDict);

0 commit comments

Comments
 (0)