-
-
Notifications
You must be signed in to change notification settings - Fork 337
/
RNSentry.m
255 lines (213 loc) · 8.36 KB
/
RNSentry.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#import "RNSentry.h"
#if __has_include(<React/RCTConvert.h>)
#import <React/RCTConvert.h>
#else
#import "RCTConvert.h"
#endif
#import <Sentry/Sentry.h>
@interface RNSentry()
@end
@implementation RNSentry
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
+ (BOOL)requiresMainQueueSetup {
return YES;
}
RCT_EXPORT_MODULE()
- (NSDictionary<NSString *, id> *)constantsToExport
{
return @{@"nativeClientAvailable": @YES, @"nativeTransport": @YES};
}
RCT_EXPORT_METHOD(startWithOptions:(NSDictionary *_Nonnull)options
resolve:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
NSError *error = nil;
SentryBeforeSendEventCallback beforeSend = ^SentryEvent*(SentryEvent *event) {
// We don't want to send an event after startup that came from a Unhandled JS Exception of react native
// Because we sent it already before the app crashed.
if (nil != event.exceptions.firstObject.type &&
[event.exceptions.firstObject.type rangeOfString:@"Unhandled JS Exception"].location != NSNotFound) {
NSLog(@"Unhandled JS Exception");
return nil;
}
// set the event.origin tag to be ios if the event originated from the sentry-cocoa sdk.
if (event.sdk && [event.sdk[@"name"] isEqualToString:@"sentry.cocoa"]) {
NSMutableDictionary *newTags = [NSMutableDictionary new];
[newTags addEntriesFromDictionary:event.tags];
[newTags setValue:@"ios" forKey:@"event.origin"];
event.tags = newTags;
}
return event;
};
[options setValue:beforeSend forKey:@"beforeSend"];
SentryOptions *sentryOptions = [[SentryOptions alloc] initWithDict:options didFailWithError:&error];
if (error) {
reject(@"SentryReactNative", error.localizedDescription, error);
return;
}
[SentrySDK startWithOptionsObject:sentryOptions];
resolve(@YES);
}
RCT_EXPORT_METHOD(deviceContexts:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
NSLog(@"Bridge call to: deviceContexts");
// Temp work around until sorted out this API in sentry-cocoa.
// TODO: If the callback isnt' executed the promise wouldn't be resolved.
[SentrySDK configureScope:^(SentryScope * _Nonnull scope) {
NSDictionary<NSString *, id> *serializedScope = [scope serialize];
// Scope serializes as 'context' instead of 'contexts' as it does for the event.
NSDictionary<NSString *, id> *contexts = [serializedScope valueForKey:@"context"];
#if DEBUG
NSData *data = [NSJSONSerialization dataWithJSONObject:contexts options:0 error:nil];
NSString *debugContext = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Contexts: %@", debugContext);
#endif
resolve(contexts);
}];
}
RCT_EXPORT_METHOD(setLogLevel:(int)level)
{
SentryLogLevel cocoaLevel;
switch (level) {
case 1:
cocoaLevel = kSentryLogLevelError;
break;
case 2:
cocoaLevel = kSentryLogLevelDebug;
break;
case 3:
cocoaLevel = kSentryLogLevelVerbose;
break;
default:
cocoaLevel = kSentryLogLevelNone;
break;
}
[SentrySDK setLogLevel:cocoaLevel];
}
RCT_EXPORT_METHOD(fetchRelease:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
resolve(@{
@"id": infoDict[@"CFBundleIdentifier"],
@"version": infoDict[@"CFBundleShortVersionString"],
@"build": infoDict[@"CFBundleVersion"],
});
}
RCT_EXPORT_METHOD(captureEnvelope:(NSDictionary * _Nonnull)envelopeDict
resolve:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
if ([NSJSONSerialization isValidJSONObject:envelopeDict]) {
SentrySdkInfo *sdkInfo = [[SentrySdkInfo alloc] initWithDict:envelopeDict[@"header"]];
SentryId *eventId = [[SentryId alloc] initWithUUIDString:envelopeDict[@"header"][@"event_id"]];
SentryEnvelopeHeader *envelopeHeader = [[SentryEnvelopeHeader alloc] initWithId:eventId andSdkInfo:sdkInfo];
NSError *error;
NSData *envelopeItemData = [NSJSONSerialization dataWithJSONObject:envelopeDict[@"payload"] options:0 error:&error];
if (nil != error) {
reject(@"SentryReactNative", @"Cannot serialize event", error);
}
SentryEnvelopeItemHeader *envelopeItemHeader = [[SentryEnvelopeItemHeader alloc] initWithType:envelopeDict[@"payload"][@"type"] length:envelopeItemData.length];
SentryEnvelopeItem *envelopeItem = [[SentryEnvelopeItem alloc] initWithHeader:envelopeItemHeader data:envelopeItemData];
SentryEnvelope *envelope = [[SentryEnvelope alloc] initWithHeader:envelopeHeader singleItem:envelopeItem];
#if DEBUG
[[[SentrySDK currentHub] getClient] captureEnvelope: envelope];
#else
if ([envelopeDict[@"payload"][@"level"] isEqualToString:@"fatal"]) {
// Storing to disk happens asynchronously with captureEnvelope
// We need to make sure the event is written to disk before resolving the promise.
// This could be replaced by SentrySDK.flush() when available.
[[[SentrySDK currentHub] getClient] storeEnvelope:envelope];
} else {
[[[SentrySDK currentHub] getClient] captureEnvelope:envelope];
}
#endif
resolve(@YES);
} else {
reject(@"SentryReactNative", @"Cannot serialize event", nil);
}
}
RCT_EXPORT_METHOD(setUser:(NSDictionary *)user
otherUserKeys:(NSDictionary *)otherUserKeys
)
{
[SentrySDK configureScope:^(SentryScope * _Nonnull scope) {
if (nil == user && nil == otherUserKeys) {
[scope setUser:nil];
} else {
SentryUser* userInstance = [[SentryUser alloc] init];
if (nil != user) {
[userInstance setUserId:user[@"id"]];
[userInstance setEmail:user[@"email"]];
[userInstance setUsername:user[@"username"]];
}
if (nil != otherUserKeys) {
[userInstance setData:otherUserKeys];
}
[scope setUser:userInstance];
}
}];
}
RCT_EXPORT_METHOD(addBreadcrumb:(NSDictionary *)breadcrumb)
{
[SentrySDK configureScope:^(SentryScope * _Nonnull scope) {
SentryBreadcrumb* breadcrumbInstance = [[SentryBreadcrumb alloc] init];
NSString * levelString = breadcrumb[@"level"];
SentryLevel sentryLevel;
if ([levelString isEqualToString:@"fatal"]) {
sentryLevel = kSentryLevelFatal;
} else if ([levelString isEqualToString:@"warning"]) {
sentryLevel = kSentryLevelWarning;
} else if ([levelString isEqualToString:@"info"]) {
sentryLevel = kSentryLevelInfo;
} else if ([levelString isEqualToString:@"debug"]) {
sentryLevel = kSentryLevelDebug;
} else {
sentryLevel = kSentryLevelError;
}
[breadcrumbInstance setLevel:sentryLevel];
[breadcrumbInstance setCategory:breadcrumb[@"category"]];
[breadcrumbInstance setType:breadcrumb[@"type"]];
[breadcrumbInstance setMessage:breadcrumb[@"message"]];
[breadcrumbInstance setData:breadcrumb[@"data"]];
[scope addBreadcrumb:breadcrumbInstance];
}];
}
RCT_EXPORT_METHOD(clearBreadcrumbs) {
[SentrySDK configureScope:^(SentryScope * _Nonnull scope) {
[scope clearBreadcrumbs];
}];
}
RCT_EXPORT_METHOD(setExtra:(NSString *)key
extra:(NSString *)extra
)
{
[SentrySDK configureScope:^(SentryScope * _Nonnull scope) {
[scope setExtraValue:extra forKey:key];
}];
}
RCT_EXPORT_METHOD(setContext:(NSString *)key
context:(NSDictionary *)context
)
{
[SentrySDK configureScope:^(SentryScope * _Nonnull scope) {
[scope setContextValue:context forKey:key];
}];
}
RCT_EXPORT_METHOD(setTag:(NSString *)key
value:(NSString *)value
)
{
[SentrySDK configureScope:^(SentryScope * _Nonnull scope) {
[scope setTagValue:value forKey:key];
}];
}
RCT_EXPORT_METHOD(crash)
{
[SentrySDK crash];
}
@end