|
1 | 1 | import { BaseEnvelopeItemHeaders, Breadcrumb, Envelope, EnvelopeItem, Event, SeverityLevel } from '@sentry/types'; |
2 | 2 | import { SentryError, logger } from '@sentry/utils'; |
| 3 | +import { parseErrorStack } from './integrations/debugsymbolicator'; |
3 | 4 | import { isHardCrash } from './misc'; |
4 | 5 | import { NativescriptOptions } from './options'; |
| 6 | +import { rewriteFrameIntegration } from './sdk'; |
5 | 7 | import { utf8ToBytes } from './vendor'; |
6 | 8 |
|
7 | 9 | const numberHasDecimals = function (value: number): boolean { |
@@ -60,10 +62,63 @@ function dataSerialize (data?: any, wrapPrimitives?: boolean) { |
60 | 62 | } |
61 | 63 | }; |
62 | 64 |
|
| 65 | +const FATAL_ERROR_REGEXP = /NativeScript encountered a fatal error: (.*?)\n at \n(\t*)?(.*)$/m; |
| 66 | + |
63 | 67 | export namespace NATIVE { |
64 | 68 | let enableNative = true; |
65 | 69 | const _DisabledNativeError = new SentryError('Native is disabled'); |
66 | 70 |
|
| 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 | + |
67 | 122 | export function isNativeTransportAvailable() { |
68 | 123 | return enableNative; |
69 | 124 | } |
@@ -291,6 +346,23 @@ export namespace NATIVE { |
291 | 346 |
|
292 | 347 | // before send right now is never called when we send the envelope |
293 | 348 | 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 | + } |
294 | 366 | if (beforeSend) { |
295 | 367 | beforeSend(event as any, null); |
296 | 368 | } |
|
0 commit comments