Skip to content

Commit 0e55045

Browse files
committed
fix(ios): try to get callstack/sourcemaps to work all the time
1 parent de4a87b commit 0e55045

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/sentry/client.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ import { NativescriptTracing } from './tracing';
1919
import { rewriteFrameIntegration } from './integrations/default';
2020
import { parseErrorStack } from './integrations/debugsymbolicator';
2121

22+
function wrapNativeException(ex, errorType = typeof ex) {
23+
if (__ANDROID__ && !(ex instanceof Error) && errorType === 'object') {
24+
const err = new Error(ex.toString());
25+
err['nativeException'] = ex;
26+
//@ts-ignore
27+
err['stackTrace'] = com.tns.NativeScriptException.getStackTraceAsString(ex);
28+
return err;
29+
}
30+
return ex;
31+
}
32+
const FATAL_ERROR_REGEXP = /NativeScript encountered a fatal error:([^]*?) at([\t\n\s]*)?([^]*)$/m;
33+
2234
/**
2335
* The Sentry React Native SDK Client.
2436
*
@@ -54,14 +66,31 @@ export class NativescriptClient extends BaseClient<NativescriptClientOptions> {
5466
* @inheritDoc
5567
*/
5668
public async eventFromException(exception: unknown, hint?: EventHint): Promise<Event> {
69+
exception = wrapNativeException(exception);
5770
// N put stackTrace in "stackTrace" instead of "stacktrace"
58-
if (exception['nativeException']) {
71+
if (__ANDROID__ && exception['nativeException']) {
5972
// in case of nativeException we have:
6073
// - stack with only the JS error stack
6174
// stackTrace with a mix of JS/Java error
6275
exception['stacktrace'] = exception.toString() + '\n at ' + exception['stack'];
6376
} else if (exception['stackTrace']) {
64-
exception['stacktrace'] = exception['stackTrace'];
77+
if (__IOS__) {
78+
exception['stacktrace'] = exception['stack'];
79+
80+
// const stackTrace = exception['stackTrace'];
81+
// const matches = stackTrace.match(FATAL_ERROR_REGEXP);
82+
// console.log('matches', stackTrace, matches);
83+
// if (matches) {
84+
// const errorMessage = matches[1];
85+
// const jsStackTrace = stackTrace.substring(stackTrace.indexOf(matches[2]));
86+
// // const stack = parseErrorStack({ stack: 'at ' + jsStackTrace } as any).reverse();
87+
// exception['stacktrace'] = errorMessage + '\n at ' + jsStackTrace;
88+
// } else {
89+
// exception['stacktrace'] = stackTrace;
90+
// }
91+
} else {
92+
exception['stacktrace'] = exception['stackTrace'];
93+
}
6594
}
6695
const hintWithScreenshot = Screenshot.attachScreenshotToEventHint(hint, this._options);
6796
const event = await eventFromException(this._options.stackParser, exception, hintWithScreenshot, this._options.attachStacktrace);
@@ -81,9 +110,10 @@ export class NativescriptClient extends BaseClient<NativescriptClientOptions> {
81110
}
82111
} else if (__IOS__ && exception['stackTrace']) {
83112
try {
84-
const stack = parseErrorStack({ stack: 'at ' + exception['stackTrace'] } as any).filter((f) => f.platform !== 'javascript');
113+
// const stack = parseErrorStack({ stack: 'at ' + exception['stackTrace'] } as any).filter((f) => f.platform !== 'javascript');
85114
// stack.forEach((frame) => rewriteFrameIntegration._iteratee(frame));
86-
event.exception.values[0].stacktrace.frames.forEach((frame) => rewriteFrameIntegration._iteratee(frame));
115+
// event.exception.values[0].stacktrace.frames.forEach((frame) => rewriteFrameIntegration._iteratee(frame));
116+
// event.exception.values[0].stacktrace.frames = event.exception.values[0].stacktrace.frames.reverse();
87117
// event.exception.values.unshift({
88118
// type: 'NativeException',
89119
// value: exception.toString(),

src/sentry/wrapper.ios.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ export namespace NATIVE {
210210

211211
if (itemHeader.type === 'event' || itemHeader.type === 'transaction') {
212212
const event = _processLevels(itemPayload as Event);
213+
// console.log('_processItem', JSON.stringify(event.exception.values[0].stacktrace.frames.reverse()))
213214

214215
event.breadcrumbs = _getBreadcrumbs(event);
215216
return [itemHeader, event];

0 commit comments

Comments
 (0)