Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.

Retain errors if send fails #52

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Raven.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Pod::Spec.new do |s|
s.homepage = "https://getsentry.com/"
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { "David Cramer" => "[email protected]" }
s.source = { :git => "https://github.com/getsentry/raven-objc.git", :tag => s.version.to_s }
s.source = { :git => "https://github.com/toolboxash/raven-objc.git", :tag => s.version.to_s }
s.source_files = ['Raven']
s.requires_arc = true
end
1 change: 1 addition & 0 deletions Raven/RavenClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,6 @@ typedef enum {
- (void)captureException:(NSException *)exception additionalExtra:(NSDictionary *)additionalExtra additionalTags:(NSDictionary *)additionalTags sendNow:(BOOL)sendNow;
- (void)captureException:(NSException*)exception method:(const char*)method file:(const char*)file line:(NSInteger)line sendNow:(BOOL)sendNow;
- (void)setupExceptionHandler;
- (void)flush;

@end
40 changes: 29 additions & 11 deletions Raven/RavenClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ - (void)captureMessage:(NSString *)message
}
[[NSUserDefaults standardUserDefaults] synchronize];
} else {
[self sendDictionary:data];
[self sendDictionary:data success:nil error:nil];
}
}

Expand Down Expand Up @@ -263,7 +263,7 @@ - (void)captureException:(NSException *)exception additionalExtra:(NSDictionary
}
[[NSUserDefaults standardUserDefaults] synchronize];
} else {
[self sendDictionary:data];
[self sendDictionary:data success:nil error:nil];
}
}

Expand Down Expand Up @@ -312,24 +312,34 @@ - (void)captureException:(NSException *)exception method:(const char *)method fi
}
[[NSUserDefaults standardUserDefaults] synchronize];
} else {
[self sendDictionary:data];
[self sendDictionary:data success:nil error:nil];
}
}

- (void)setupExceptionHandler {
NSSetUncaughtExceptionHandler(&exceptionHandler);
[self flush];
}


- (void)flush
{
// Process saved crash reports
NSArray *reports = [[NSUserDefaults standardUserDefaults] objectForKey:userDefaultsKey];
NSMutableArray *reports = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:userDefaultsKey]];
if (reports != nil && [reports count]) {
for (NSDictionary *data in reports) {
[self sendDictionary:data];
[self sendDictionary:data success:^{
[reports removeObject:data];
[[NSUserDefaults standardUserDefaults] setObject:reports forKey:userDefaultsKey];
[[NSUserDefaults standardUserDefaults] synchronize];
} error:^(NSError *err) {
// Coudn't send report, keep for next time
}];
}
[[NSUserDefaults standardUserDefaults] setObject:[NSArray array] forKey:userDefaultsKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}


#pragma mark - Private methods

- (NSString *)generateUUID {
Expand Down Expand Up @@ -379,18 +389,18 @@ - (NSDictionary *)prepareDictionaryForMessage:(NSString *)message
nil];
}

- (void)sendDictionary:(NSDictionary *)dict {
- (void)sendDictionary:(NSDictionary *)dict success:(void (^)(void))success error:(void (^)(NSError * err))error{
NSData *JSON = [self encodeJSON:dict];
[self sendJSON:JSON];
[self sendJSON:JSON success:success error:error];
}

- (void)sendJSON:(NSData *)JSON {
- (void)sendJSON:(NSData *)JSON success:(void (^)(void))success error:(void (^)(NSError *))error{
if (!self.config) {
NSLog(@"Sentry JSON (DSN not configured, will not be sent):\n%@\n",
[[NSString alloc] initWithData:JSON encoding:NSUTF8StringEncoding]);
return;
}

NSString *header = [NSString stringWithFormat:@"Sentry sentry_version=%@, sentry_client=%@, sentry_timestamp=%ld, sentry_key=%@, sentry_secret=%@",
sentryProtocol,
sentryClient,
Expand All @@ -409,8 +419,16 @@ - (void)sendJSON:(NSData *)JSON {
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (data) {
NSLog(@"JSON sent to Sentry");
if (success != nil)
{
success();
}
} else {
NSLog(@"Connection failed! Error - %@ %@", [connectionError localizedDescription], [[connectionError userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
if (error != nil)
{
error(connectionError);
}
}
}];
}
Expand Down
4 changes: 2 additions & 2 deletions Raven/RavenClient_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
culprit:(NSString *)culprit
stacktrace:(NSArray *)stacktrace
exception:(NSDictionary *)exceptionDict;
- (void)sendDictionary:(NSDictionary *)dict;
- (void)sendJSON:(NSData *)JSON;
- (void)sendDictionary:(NSDictionary *)dict success:(void (^)(void))success error:(void (^)(NSError *))error;
- (void)sendJSON:(NSData *)JSON success:(void (^)(void))success error:(void (^)(NSError *err))error;

@end
2 changes: 1 addition & 1 deletion RavenTests/RavenClientTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@implementation MockRavenClient

- (void)sendDictionary:(NSDictionary *)dict
- (void)sendDictionary:(NSDictionary *)dict success:(void (^)(void))success error:(void (^)(NSError *))error;
{
self.lastEvent = dict;
self.numEvents += 1;
Expand Down