Skip to content

Commit caa872f

Browse files
committed
fix(ios): full context working
1 parent 91ae3f1 commit caa872f

File tree

1 file changed

+26
-188
lines changed

1 file changed

+26
-188
lines changed

src/wrapper.ios.ts

Lines changed: 26 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import { pointsFromBuffer } from '@nativescript-community/arraybuffers';
2-
import { getClass } from '@nativescript/core/utils/types';
3-
import { Scope } from '@sentry/core';
4-
import { Attachment, BaseEnvelopeItemHeaders, Breadcrumb, Envelope, EnvelopeItem, Event, SeverityLevel, User } from '@sentry/types';
1+
import { BaseEnvelopeItemHeaders, Breadcrumb, Envelope, EnvelopeItem, Event, SeverityLevel } from '@sentry/types';
52
import { SentryError, logger } from '@sentry/utils';
63
import { isHardCrash } from './misc';
74
import { NativescriptOptions } from './options';
85
import { utf8ToBytes } from './vendor';
9-
import { UserFeedback } from './wrapper';
106

117
const numberHasDecimals = function (value: number): boolean {
128
return !(value % 1 === 0);
@@ -187,54 +183,6 @@ export namespace NATIVE {
187183
}
188184
}
189185
}
190-
export async function sendEvent(event: Event) {
191-
try {
192-
// Process and convert deprecated levels
193-
// event.level = event.level ? _processLevel(event.level) : undefined;
194-
console.error('sendEvent', JSON.stringify(event));
195-
196-
const payload = {
197-
...event,
198-
message: {
199-
message: event.message
200-
}
201-
};
202-
payload.tags = payload.tags || {};
203-
204-
payload.tags['event.origin'] = 'ios';
205-
payload.tags['event.environment'] = 'nativescript';
206-
// Serialize and remove any instances that will crash the native bridge such as Spans
207-
const serializedPayload = JSON.parse(JSON.stringify(payload));
208-
const eventId = SentryId.alloc().initWithUUIDString(event.event_id);
209-
const envelopeHeader = SentryEnvelopeHeader.alloc().initWithId(eventId);
210-
const envelopeItemData = NSJSONSerialization.dataWithJSONObjectOptionsError(serializedPayload, 0);
211-
212-
let itemType = payload.type;
213-
if (!itemType) {
214-
// Default to event type.
215-
itemType = 'event' as any;
216-
}
217-
218-
const envelopeItemHeader = SentryEnvelopeItemHeader.alloc().initWithTypeLength(itemType, envelopeItemData.length);
219-
const envelopeItem = SentryEnvelopeItem.alloc().initWithHeaderData(envelopeItemHeader, envelopeItemData);
220-
221-
const envelope = SentryEnvelope.alloc().initWithHeaderSingleItem(envelopeHeader, envelopeItem);
222-
223-
if (event.level === 'fatal') {
224-
// captureEvent queues the event for submission, so storing to disk happens asynchronously
225-
// We need to make sure the event is written to disk before resolving the promise.
226-
// This could be replaced by SentrySDK.flush() when available.
227-
NSSentrySDK.storeEnvelope(envelope);
228-
} else {
229-
NSSentrySDK.captureEnvelope(envelope);
230-
}
231-
// if (sentryOptions.flushSendEvent === true) {
232-
233-
// }
234-
} catch (err) {
235-
console.error('sendEvent', err, err.stack);
236-
}
237-
}
238186
/**
239187
* Sending the envelope over the bridge to native
240188
* @param envelope Envelope
@@ -337,12 +285,12 @@ export namespace NATIVE {
337285
delete toPassOptions[k];
338286
}
339287
});
340-
console.log('toPassOptions', Object.keys(toPassOptions));
341288
const mutDict = NSMutableDictionary.alloc().initWithDictionary(dataSerialize(toPassOptions) as any);
342289

343290
nSentryOptions = SentryOptions.alloc().initWithDictDidFailWithError(mutDict as any);
344-
nSentryOptions.beforeSend = (event: SentryEvent)=>{
345-
console.log('beforeSend', event);
291+
292+
// before send right now is never called when we send the envelope
293+
nSentryOptions.beforeSend = (event: SentryEvent) => {
346294
if (beforeSend) {
347295
beforeSend(event as any, null);
348296
}
@@ -352,9 +300,8 @@ export namespace NATIVE {
352300
if (toPassOptions.hasOwnProperty('enableNativeCrashHandling')) {
353301
if (!toPassOptions.enableNativeCrashHandling) {
354302
const integrations = nSentryOptions.integrations.mutableCopy();
355-
// console.log('integrations', typeof nSentryOptions, nSentryOptions.length, nSentryOptions[0]);
356303
integrations.removeObject('SentryCrashIntegration');
357-
sentryOptions.integrations = integrations;
304+
nSentryOptions.integrations = integrations;
358305
}
359306
}
360307

@@ -364,26 +311,6 @@ export namespace NATIVE {
364311
}
365312
NSSentrySDK.startWithOptionsObject(nSentryOptions);
366313

367-
// NSSentrySDK.startWithConfigureOptions((obj) => {
368-
// const beforeSend = options.beforeSend;
369-
// delete options.beforeSend;
370-
371-
// obj.beforeSend = (event: SentryEvent)=>{
372-
// console.log('beforeSend', event);
373-
// if (beforeSend) {
374-
// beforeSend(event as any, null);
375-
// }
376-
// return event;
377-
// };
378-
// Object.keys(options).forEach((k) => {
379-
// if (options[k] !== undefined ) {
380-
// // console.log('setting sentry option',k, obj.hasOwnProperty(k), options[k] );
381-
// obj[k] = options[k];
382-
383-
// }
384-
// });
385-
// nSentryOptions = obj;
386-
// });
387314
return (true);
388315
} catch (error) {
389316
enableNative = false;
@@ -392,115 +319,26 @@ export namespace NATIVE {
392319
}
393320
}
394321

395-
// export function nativeLogLevel(level: number) {
396-
// let cocoaLevel;
397-
// switch (level) {
398-
// case 1:
399-
// cocoaLevel = SentryLevel.kSentryLevelError;
400-
// break;
401-
// case 2:
402-
// cocoaLevel = SentryLevel.kSentryLevelDebug;
403-
// break;
404-
// case 3:
405-
// cocoaLevel = SentryLevel.kSentryLevelVerbose;
406-
// break;
407-
// default:
408-
// cocoaLevel = SentryLevel.kSentryLevelNone;
409-
// }
410-
// return cocoaLevel;
411-
// }
412-
export function cranativeCrashsh() {
322+
export function nativeCrash() {
413323
NSSentrySDK.crash();
414324
}
415325
export function flush(timeout: number) {
416326
NSSentrySDK.flush(timeout);
417327
}
418-
function toJsObject(objCObj) {
419-
if (objCObj === null || typeof objCObj !== 'object') {
420-
return objCObj;
421-
}
422-
let node, key, i, l;
423-
const oKeyArr = objCObj.allKeys;
424-
425-
if (oKeyArr === undefined && objCObj.count !== undefined) {
426-
// array
427-
node = [];
428-
for (i = 0, l = objCObj.count; i < l; i++) {
429-
key = objCObj.objectAtIndex(i);
430-
node.push(toJsObject(key));
431-
}
432-
} else if (oKeyArr !== undefined) {
433-
// object
434-
node = {};
435-
for (i = 0, l = oKeyArr.count; i < l; i++) {
436-
key = oKeyArr.objectAtIndex(i);
437-
const val = objCObj.valueForKey(key);
438-
439-
// Firestore can store nulls
440-
if (val === null) {
441-
node[key] = null;
442-
continue;
443-
}
444-
node[key] = getValueForClass(val);
445-
}
446-
} else {
447-
node = getValueForClass(objCObj);
448-
}
449-
450-
return node;
451-
}
452-
453-
function getValueForClass(val) {
454-
switch (getClass(val)) {
455-
case 'NSArray':
456-
case 'NSMutableArray':
457-
return toJsObject(val);
458-
case 'NSDictionary':
459-
case 'NSMutableDictionary':
460-
return toJsObject(val);
461-
case 'String':
462-
return String(val);
463-
case 'Boolean':
464-
return val;
465-
case 'Number':
466-
case 'NSDecimalNumber':
467-
return Number(String(val));
468-
case 'Date':
469-
return new Date(val);
470-
471-
default:
472-
return String(val);
473-
}
474-
}
475328

476329
function dictToJSON(dict) {
477-
JSON.parse(NSString.alloc().initWithDataEncoding(NSJSONSerialization.dataWithJSONObjectOptionsError(dict, 0), NSUTF8StringEncoding) as any);
330+
return JSON.parse(NSString.alloc().initWithDataEncoding(NSJSONSerialization.dataWithJSONObjectOptionsError(dict, 0), NSUTF8StringEncoding) as any);
478331
}
479332
export async function fetchNativeDeviceContexts() {
480333
if (!enableNative) {
481334
throw _DisabledNativeError;
482335
}
483-
const contexts = {};
336+
let contexts = {};
484337
NSSentrySDK.configureScope((scope) => {
485338
try {
486-
487-
const serializedScope = scope.serialize();
488-
// console.log('toto' , dictToJSON(serializedScope), NSString.alloc().initWithDataEncoding(NSJSONSerialization.dataWithJSONObjectOptionsError(serializedScope, 0), NSUTF8StringEncoding));
489-
// const context2 = SentryEvent.alloc().init().context;
490-
// console.log('context2' , context2 && dictToJSON(context2));
491-
const tempContexts = (serializedScope.valueForKey('context'));
492-
const tempUser = serializedScope.valueForKey('user');
493-
const user = {};
494-
if (tempUser) {
495-
Object.assign(user, dictToJSON(tempUser.valueForKey('user')));
496-
} else {
497-
user['id'] = NSSentrySDK.installationID;
498-
}
499-
contexts['user'] = user;
500-
501-
if (tempContexts) {
502-
contexts['context'] = dictToJSON(tempContexts);
503-
}
339+
const result = dictToJSON( scope.serialize());
340+
result['user'] = result['user'] || { id: NSSentrySDK.installationID };
341+
contexts = result;
504342
} catch (error) {
505343
console.error('fetchNativeDeviceContexts', error, error.stack);
506344
}
@@ -524,21 +362,21 @@ export namespace NATIVE {
524362
// }
525363
// NSSentrySDK.captureUserFeedback(userFeedback);
526364
// }
527-
function eventLevel(level) {
528-
switch (level) {
529-
case 'fatal':
530-
return SentryLevel.kSentryLevelFatal;
531-
case 'warning':
532-
return SentryLevel.kSentryLevelWarning;
533-
case 'info':
534-
case 'log':
535-
return SentryLevel.kSentryLevelInfo;
536-
case 'debug':
537-
return SentryLevel.kSentryLevelDebug;
538-
default:
539-
return SentryLevel.kSentryLevelError;
540-
}
541-
}
365+
// function eventLevel(level) {
366+
// switch (level) {
367+
// case 'fatal':
368+
// return SentryLevel.kSentryLevelFatal;
369+
// case 'warning':
370+
// return SentryLevel.kSentryLevelWarning;
371+
// case 'info':
372+
// case 'log':
373+
// return SentryLevel.kSentryLevelInfo;
374+
// case 'debug':
375+
// return SentryLevel.kSentryLevelDebug;
376+
// default:
377+
// return SentryLevel.kSentryLevelError;
378+
// }
379+
// }
542380

543381
// export function setUser(user: User | null, otherUserKeys) {
544382
// if (!enableNative) {

0 commit comments

Comments
 (0)