Skip to content

Commit

Permalink
iOS: Improves error messaging when StaticServer fails to start
Browse files Browse the repository at this point in the history
  • Loading branch information
birdofpreyru committed Aug 29, 2022
1 parent fe40f2d commit f62b3b6
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions ios/FPStaticServer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,29 @@ - (dispatch_queue_t)methodQueue
return response;
}];

NSError *error;
NSMutableDictionary* options = [NSMutableDictionary dictionary];
// Note: It must be __block variable to allow onStartFailure() block below
// to access its current, rather than its initial value.
__block NSError *error;

/**
* Rejects the start promise with details from "error" object, if that
* exists, otherwise rejects with the fallback "StaticServer could not start"
* message.
*
* BEWARE: Once this block is executed, you should ensure yourself
* the parent function returns, or at least does not attempt to resolve or
* reject the start promise again.
*/
void (^onStartFailure)(void) = ^{
if (error) {
NSString *errorDescription = [NSString stringWithFormat:@"%@ / %@",
error.localizedDescription,
error.localizedFailureReason];
reject(error.domain, errorDescription, error);
} else reject(@"server_error", @"StaticServer could not start", nil);
};

NSMutableDictionary* options = [NSMutableDictionary dictionary];

NSLog(@"Started StaticServer on port %@", self.port);

Expand All @@ -147,18 +167,15 @@ - (dispatch_queue_t)methodQueue
NSNumber *listenPort = [NSNumber numberWithUnsignedInteger:_webServer.port];
self.port = listenPort;

if(_webServer.serverURL == NULL) {
reject(@"server_error", @"StaticServer could not start", error);
} else {
if(_webServer.serverURL == NULL) onStartFailure();
else {
self.url = [NSString stringWithFormat: @"%@://%@:%@", [_webServer.serverURL scheme], [_webServer.serverURL host], [_webServer.serverURL port]];
NSLog(@"Started StaticServer at URL %@", self.url);
resolve(self.url);
}
} else {
NSLog(@"Error starting StaticServer: %@", error);

reject(@"server_error", @"StaticServer could not start", error);

onStartFailure();
}

}
Expand Down

0 comments on commit f62b3b6

Please sign in to comment.