Skip to content

Commit 30304d3

Browse files
committed
fix(ios): correctly translate JS stack from fatal error
1 parent fd93a73 commit 30304d3

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

src/wrapper.ios.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { BaseEnvelopeItemHeaders, Breadcrumb, Envelope, EnvelopeItem, Event, SeverityLevel } from '@sentry/types';
22
import { SentryError, logger } from '@sentry/utils';
3+
import { parseErrorStack } from './integrations/debugsymbolicator';
34
import { isHardCrash } from './misc';
45
import { NativescriptOptions } from './options';
6+
import { rewriteFrameIntegration } from './sdk';
57
import { utf8ToBytes } from './vendor';
68

79
const numberHasDecimals = function (value: number): boolean {
@@ -60,10 +62,63 @@ function dataSerialize (data?: any, wrapPrimitives?: boolean) {
6062
}
6163
};
6264

65+
const FATAL_ERROR_REGEXP = /NativeScript encountered a fatal error: (.*?)\n at \n(\t*)?(.*)$/m;
66+
6367
export namespace NATIVE {
6468
let enableNative = true;
6569
const _DisabledNativeError = new SentryError('Native is disabled');
6670

71+
function convertToNativeJavascriptStacktrace(
72+
stack: {
73+
file?: string;
74+
filename?: string;
75+
function?: string;
76+
methodName?: string;
77+
column?: number;
78+
colno?: number;
79+
lineno?: number;
80+
lineNumber?: number;
81+
in_app?: boolean;
82+
}[]
83+
) {
84+
if (!stack) {
85+
return null;
86+
}
87+
const nStackTrace = SentryStacktrace.new();
88+
const frames = NSMutableArray.alloc().init();
89+
for (let i = 0; i < stack.length; i++) {
90+
const frame = stack[i];
91+
92+
const fileName = frame.file || frame.filename || '';
93+
const methodName = frame.methodName || frame.function || '';
94+
95+
const lineNumber = frame.lineNumber || frame.lineno || 0;
96+
const column = frame.column || frame.colno || 0;
97+
const stackFrame = SentryFrame.new();
98+
stackFrame.function = methodName;
99+
stackFrame.fileName = fileName;
100+
stackFrame.lineNumber = lineNumber;
101+
stackFrame.columnNumber = column;
102+
stackFrame.platform = 'javascript';
103+
stackFrame.inApp = NSNumber.numberWithBool(frame.in_app || false) ;
104+
frames.addObject(stackFrame);
105+
}
106+
nStackTrace.frames = (frames) as any;
107+
return nStackTrace;
108+
}
109+
function addJavascriptExceptionInterface(nEvent: SentryEvent, type: string, value: string, stack) {
110+
const exceptions = nEvent.exceptions;
111+
112+
const actualExceptions = NSMutableArray.alloc().initWithArray(exceptions);
113+
const nException = SentryException.new();
114+
nException.type = type;
115+
nException.value = value;
116+
// nException.threadId = NSThread.currentThread.;
117+
nException.stacktrace = convertToNativeJavascriptStacktrace(stack);
118+
actualExceptions.insertObjectAtIndex(nException, 0);
119+
nEvent.exceptions = (actualExceptions) as any;
120+
}
121+
67122
export function isNativeTransportAvailable() {
68123
return enableNative;
69124
}
@@ -291,6 +346,23 @@ export namespace NATIVE {
291346

292347
// before send right now is never called when we send the envelope
293348
nSentryOptions.beforeSend = (event: SentryEvent) => {
349+
const exception = event.exceptions?.objectAtIndex(0);
350+
const exceptionvalue = exception?.value;
351+
if(exceptionvalue ) {
352+
353+
const matches =exceptionvalue.match(FATAL_ERROR_REGEXP);
354+
if (matches) {
355+
const errorMessage = matches[1];
356+
const jsStackTrace = exceptionvalue.substring(exceptionvalue.indexOf(matches[2]));
357+
const stack = parseErrorStack({ stack: 'at ' + jsStackTrace } as any).reverse();
358+
stack.forEach((frame) => rewriteFrameIntegration._iteratee(frame));
359+
console.log('errorMessage!', errorMessage);
360+
console.log('jsStackTrace!', jsStackTrace);
361+
addJavascriptExceptionInterface(event, 'Error', errorMessage, stack.reverse());
362+
exception.type = 'NativeScriptException';
363+
exception.value = errorMessage;
364+
}
365+
}
294366
if (beforeSend) {
295367
beforeSend(event as any, null);
296368
}

0 commit comments

Comments
 (0)