Skip to content

Commit

Permalink
BugsnagApiClientDeliveryStatus -> BSGDeliveryStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdowell committed Aug 8, 2022
1 parent b9e2d94 commit 39477aa
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 45 deletions.
8 changes: 4 additions & 4 deletions Bugsnag/Delivery/BSGEventUploadOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,19 @@ - (void)runWithDelegate:(id<BSGEventUploadOperationDelegate>)delegate completion
return;
}

BSGPostJSONData(configuration.session, data, requestHeaders, notifyURL, ^(BugsnagApiClientDeliveryStatus status, __unused NSError *deliveryError) {
BSGPostJSONData(configuration.session, data, requestHeaders, notifyURL, ^(BSGDeliveryStatus status, __unused NSError *deliveryError) {
switch (status) {
case BugsnagApiClientDeliveryStatusDelivered:
case BSGDeliveryStatusDelivered:
bsg_log_debug(@"Uploaded event %@", self.name);
[self deleteEvent];
break;

case BugsnagApiClientDeliveryStatusFailed:
case BSGDeliveryStatusFailed:
bsg_log_debug(@"Upload failed retryably for event %@", self.name);
[self prepareForRetry:originalPayload ?: eventPayload HTTPBodySize:data.length];
break;

case BugsnagApiClientDeliveryStatusUndeliverable:
case BSGDeliveryStatusUndeliverable:
bsg_log_debug(@"Upload failed; will discard event %@", self.name);
[self deleteEvent];
break;
Expand Down
28 changes: 14 additions & 14 deletions Bugsnag/Delivery/BSGSessionUploader.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ - (instancetype)initWithConfig:(BugsnagConfiguration *)config notifier:(BugsnagN
}

- (void)uploadSession:(BugsnagSession *)session {
[self sendSession:session completionHandler:^(BugsnagApiClientDeliveryStatus status) {
[self sendSession:session completionHandler:^(BSGDeliveryStatus status) {
switch (status) {
case BugsnagApiClientDeliveryStatusDelivered:
case BSGDeliveryStatusDelivered:
[self processStoredSessions];
break;

case BugsnagApiClientDeliveryStatusFailed:
case BSGDeliveryStatusFailed:
[self storeSession:session]; // Retry later
break;

case BugsnagApiClientDeliveryStatusUndeliverable:
case BSGDeliveryStatusUndeliverable:
break;
}
}];
Expand Down Expand Up @@ -106,8 +106,8 @@ - (void)processStoredSessions {
[self.activeIds addObject:file];
}

[self sendSession:session completionHandler:^(BugsnagApiClientDeliveryStatus status) {
if (status != BugsnagApiClientDeliveryStatusFailed) {
[self sendSession:session completionHandler:^(BSGDeliveryStatus status) {
if (status != BSGDeliveryStatusFailed) {
[fileManager removeItemAtPath:file error:nil];
}
@synchronized (self.activeIds) {
Expand All @@ -133,18 +133,18 @@ - (void)pruneFiles {
//
// https://bugsnagsessiontrackingapi.docs.apiary.io/#reference/0/session/report-a-session-starting
//
- (void)sendSession:(BugsnagSession *)session completionHandler:(nonnull void (^)(BugsnagApiClientDeliveryStatus status))completionHandler {
- (void)sendSession:(BugsnagSession *)session completionHandler:(nonnull void (^)(BSGDeliveryStatus status))completionHandler {
NSString *apiKey = [self.config.apiKey copy];
if (!apiKey) {
bsg_log_err(@"Cannot send session because no apiKey is configured.");
completionHandler(BugsnagApiClientDeliveryStatusUndeliverable);
completionHandler(BSGDeliveryStatusUndeliverable);
return;
}

NSURL *url = self.config.sessionURL;
if (!url) {
bsg_log_err(@"Cannot send session because no endpoint is configured.");
completionHandler(BugsnagApiClientDeliveryStatusUndeliverable);
completionHandler(BSGDeliveryStatusUndeliverable);
return;
}

Expand All @@ -167,19 +167,19 @@ - (void)sendSession:(BugsnagSession *)session completionHandler:(nonnull void (^
NSData *data = BSGJSONDataFromDictionary(payload, NULL);
if (!data) {
bsg_log_err(@"Failed to encode session %@", session.id);
completionHandler(BugsnagApiClientDeliveryStatusUndeliverable);
completionHandler(BSGDeliveryStatusUndeliverable);
return;
}

BSGPostJSONData(self.config.session, data, headers, url, ^(BugsnagApiClientDeliveryStatus status, NSError *error) {
BSGPostJSONData(self.config.session, data, headers, url, ^(BSGDeliveryStatus status, NSError *error) {
switch (status) {
case BugsnagApiClientDeliveryStatusDelivered:
case BSGDeliveryStatusDelivered:
bsg_log_info(@"Sent session %@", session.id);
break;
case BugsnagApiClientDeliveryStatusFailed:
case BSGDeliveryStatusFailed:
bsg_log_warn(@"Failed to send sessions: %@", error);
break;
case BugsnagApiClientDeliveryStatusUndeliverable:
case BSGDeliveryStatusUndeliverable:
bsg_log_warn(@"Failed to send sessions: %@", error);
break;
}
Expand Down
10 changes: 5 additions & 5 deletions Bugsnag/Delivery/BugsnagApiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ static BugsnagHTTPHeaderName const BugsnagHTTPHeaderNamePayloadVersion = @"B
static BugsnagHTTPHeaderName const BugsnagHTTPHeaderNameSentAt = @"Bugsnag-Sent-At";
static BugsnagHTTPHeaderName const BugsnagHTTPHeaderNameStacktraceTypes = @"Bugsnag-Stacktrace-Types";

typedef NS_ENUM(NSInteger, BugsnagApiClientDeliveryStatus) {
typedef NS_ENUM(NSInteger, BSGDeliveryStatus) {
/// The payload was delivered successfully and can be deleted.
BugsnagApiClientDeliveryStatusDelivered,
BSGDeliveryStatusDelivered,
/// The payload was not delivered but can be retried, e.g. when there was a loss of connectivity.
BugsnagApiClientDeliveryStatusFailed,
BSGDeliveryStatusFailed,
/// The payload cannot be delivered and should be deleted without attempting to retry.
BugsnagApiClientDeliveryStatusUndeliverable,
BSGDeliveryStatusUndeliverable,
};

void BSGPostJSONData(NSURLSession *URLSession,
NSData *data,
NSDictionary<BugsnagHTTPHeaderName, NSString *> *headers,
NSURL *url,
void (^ completionHandler)(BugsnagApiClientDeliveryStatus status, NSError *_Nullable error));
void (^ completionHandler)(BSGDeliveryStatus status, NSError *_Nullable error));

NSString *_Nullable BSGIntegrityHeaderValue(NSData *_Nullable data);

Expand Down
10 changes: 5 additions & 5 deletions Bugsnag/Delivery/BugsnagApiClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void BSGPostJSONData(NSURLSession *URLSession,
NSData *data,
NSDictionary<BugsnagHTTPHeaderName, NSString *> *headers,
NSURL *url,
void (^ completionHandler)(BugsnagApiClientDeliveryStatus status, NSError *_Nullable error)) {
void (^ completionHandler)(BSGDeliveryStatus status, NSError *_Nullable error)) {

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];
request.HTTPMethod = @"POST";
Expand All @@ -52,7 +52,7 @@ void BSGPostJSONData(NSURLSession *URLSession,
[[URLSession uploadTaskWithRequest:request fromData:data completionHandler:^(__unused NSData *responseData, NSURLResponse *response, NSError *error) {
if (![response isKindOfClass:[NSHTTPURLResponse class]]) {
bsg_log_debug(@"Request to %@ completed with error %@", url, error);
completionHandler(BugsnagApiClientDeliveryStatusFailed, error ?:
completionHandler(BSGDeliveryStatusFailed, error ?:
[NSError errorWithDomain:@"BugsnagApiClientErrorDomain" code:0 userInfo:@{
NSLocalizedDescriptionKey: @"Request failed: no response was received",
NSURLErrorFailingURLErrorKey: url }]);
Expand All @@ -63,7 +63,7 @@ void BSGPostJSONData(NSURLSession *URLSession,
bsg_log_debug(@"Request to %@ completed with status code %ld", url, (long)statusCode);

if (statusCode / 100 == 2) {
completionHandler(BugsnagApiClientDeliveryStatusDelivered, nil);
completionHandler(BSGDeliveryStatusDelivered, nil);
return;
}

Expand All @@ -80,11 +80,11 @@ void BSGPostJSONData(NSURLSession *URLSession,
statusCode != HTTPStatusCodeProxyAuthenticationRequired &&
statusCode != HTTPStatusCodeClientTimeout &&
statusCode != HTTPStatusCodeTooManyRequests) {
completionHandler(BugsnagApiClientDeliveryStatusUndeliverable, error);
completionHandler(BSGDeliveryStatusUndeliverable, error);
return;
}

completionHandler(BugsnagApiClientDeliveryStatusFailed, error);
completionHandler(BSGDeliveryStatusFailed, error);
}] resume];
}

Expand Down
34 changes: 17 additions & 17 deletions Tests/BugsnagTests/BugsnagApiClientTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,34 @@ - (void)testHTTPStatusCodes {
NSURL *url = [NSURL URLWithString:@"https://example.com"];
id URLSession = [[URLSessionMock alloc] init];

void (^ test)(NSInteger, BugsnagApiClientDeliveryStatus, BOOL) =
^(NSInteger statusCode, BugsnagApiClientDeliveryStatus expectedDeliveryStatus, BOOL expectError) {
void (^ test)(NSInteger, BSGDeliveryStatus, BOOL) =
^(NSInteger statusCode, BSGDeliveryStatus expectedDeliveryStatus, BOOL expectError) {
XCTestExpectation *expectation = [self expectationWithDescription:@"completionHandler should be called"];
id response = [[NSHTTPURLResponse alloc] initWithURL:url statusCode:statusCode HTTPVersion:@"1.1" headerFields:nil];
[URLSession mockData:[NSData data] response:response error:nil];
BSGPostJSONData(URLSession, [NSData data], @{}, url, ^(BugsnagApiClientDeliveryStatus status, NSError * _Nullable error) {
BSGPostJSONData(URLSession, [NSData data], @{}, url, ^(BSGDeliveryStatus status, NSError * _Nullable error) {
XCTAssertEqual(status, expectedDeliveryStatus);
expectError ? XCTAssertNotNil(error) : XCTAssertNil(error);
[expectation fulfill];
});
};

test(200, BugsnagApiClientDeliveryStatusDelivered, NO);
test(200, BSGDeliveryStatusDelivered, NO);

// Permanent failures
test(400, BugsnagApiClientDeliveryStatusUndeliverable, YES);
test(401, BugsnagApiClientDeliveryStatusUndeliverable, YES);
test(403, BugsnagApiClientDeliveryStatusUndeliverable, YES);
test(404, BugsnagApiClientDeliveryStatusUndeliverable, YES);
test(405, BugsnagApiClientDeliveryStatusUndeliverable, YES);
test(406, BugsnagApiClientDeliveryStatusUndeliverable, YES);
test(400, BSGDeliveryStatusUndeliverable, YES);
test(401, BSGDeliveryStatusUndeliverable, YES);
test(403, BSGDeliveryStatusUndeliverable, YES);
test(404, BSGDeliveryStatusUndeliverable, YES);
test(405, BSGDeliveryStatusUndeliverable, YES);
test(406, BSGDeliveryStatusUndeliverable, YES);

// Transient failures
test(402, BugsnagApiClientDeliveryStatusFailed, YES);
test(407, BugsnagApiClientDeliveryStatusFailed, YES);
test(408, BugsnagApiClientDeliveryStatusFailed, YES);
test(429, BugsnagApiClientDeliveryStatusFailed, YES);
test(500, BugsnagApiClientDeliveryStatusFailed, YES);
test(402, BSGDeliveryStatusFailed, YES);
test(407, BSGDeliveryStatusFailed, YES);
test(408, BSGDeliveryStatusFailed, YES);
test(429, BSGDeliveryStatusFailed, YES);
test(500, BSGDeliveryStatusFailed, YES);

[self waitForExpectationsWithTimeout:1 handler:nil];
}
Expand All @@ -62,8 +62,8 @@ - (void)testNotConnectedToInternetError {
[URLSession mockData:nil response:nil error:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorNotConnectedToInternet userInfo:@{
NSURLErrorFailingURLErrorKey: url,
}]];
BSGPostJSONData(URLSession, [NSData data], @{}, url, ^(BugsnagApiClientDeliveryStatus status, NSError * _Nullable error) {
XCTAssertEqual(status, BugsnagApiClientDeliveryStatusFailed);
BSGPostJSONData(URLSession, [NSData data], @{}, url, ^(BSGDeliveryStatus status, NSError * _Nullable error) {
XCTAssertEqual(status, BSGDeliveryStatusFailed);
XCTAssertNotNil(error);
XCTAssertEqualObjects(error.domain, NSURLErrorDomain);
[expectation fulfill];
Expand Down

0 comments on commit 39477aa

Please sign in to comment.