From 0ec5398e9cb4b0e5ab133cc0c330b85b3d37766e Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Sun, 3 Mar 2024 18:31:05 +0100 Subject: [PATCH] chore: Handle app startup errors as session creation exceptions (#855) --- .../Commands/FBSessionCommands.m | 22 ++++++++++++------- .../Routing/FBExceptionHandler.m | 3 +++ WebDriverAgentLib/Routing/FBExceptions.h | 3 +++ WebDriverAgentLib/Routing/FBExceptions.m | 1 + 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/WebDriverAgentLib/Commands/FBSessionCommands.m b/WebDriverAgentLib/Commands/FBSessionCommands.m index 9594058a9..38def9553 100644 --- a/WebDriverAgentLib/Commands/FBSessionCommands.m +++ b/WebDriverAgentLib/Commands/FBSessionCommands.m @@ -11,6 +11,7 @@ #import "FBCapabilities.h" #import "FBConfiguration.h" +#import "FBExceptions.h" #import "FBLogger.h" #import "FBProtocolHelpers.h" #import "FBRouteRequest.h" @@ -91,7 +92,7 @@ + (NSArray *)routes traceback:nil]); } if (nil == (capabilities = FBParseCapabilities((NSDictionary *)request.arguments[@"capabilities"], &error))) { - return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:error.description traceback:nil]); + return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:error.localizedDescription traceback:nil]); } [FBConfiguration resetSessionSettings]; @@ -159,12 +160,16 @@ + (NSArray *)routes if (![XCUIDevice.sharedDevice fb_openUrl:initialUrl withApplication:bundleID error:&openError]) { - NSString *errorMsg = [NSString stringWithFormat:@"Cannot open the URL %@ wuth the %@ application. Original error: %@", - initialUrl, bundleID, openError.description]; + NSString *errorMsg = [NSString stringWithFormat:@"Cannot open the URL %@ with the %@ application. Original error: %@", + initialUrl, bundleID, openError.localizedDescription]; return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:errorMsg traceback:nil]); } } else { - [app launch]; + @try { + [app launch]; + } @catch (NSException *e) { + return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:e.reason traceback:nil]); + } } if (!app.running) { NSString *errorMsg = [NSString stringWithFormat:@"Cannot launch %@ application. Make sure the correct bundle identifier has been provided in capabilities and check the device log for possible crash report occurrences", bundleID]; @@ -178,7 +183,7 @@ + (NSArray *)routes withApplication:bundleID error:&openError]) { NSString *errorMsg = [NSString stringWithFormat:@"Cannot open the URL %@ with the %@ application. Original error: %@", - initialUrl, bundleID, openError.description]; + initialUrl, bundleID, openError.localizedDescription]; return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:errorMsg traceback:nil]); } } else { @@ -191,7 +196,7 @@ + (NSArray *)routes NSError *openError; if (![XCUIDevice.sharedDevice fb_openUrl:initialUrl error:&openError]) { NSString *errorMsg = [NSString stringWithFormat:@"Cannot open the URL %@. Original error: %@", - initialUrl, openError.description]; + initialUrl, openError.localizedDescription]; return FBResponseWithStatus([FBCommandStatus sessionNotCreatedError:errorMsg traceback:nil]); } } @@ -396,7 +401,8 @@ + (NSArray *)routes NSError *error; if (![FBActiveAppDetectionPoint.sharedInstance setCoordinatesWithString:(NSString *)[settings objectForKey:FB_SETTING_ACTIVE_APP_DETECTION_POINT] error:&error]) { - return FBResponseWithStatus([FBCommandStatus invalidArgumentErrorWithMessage:error.description traceback:nil]); + return FBResponseWithStatus([FBCommandStatus invalidArgumentErrorWithMessage:error.localizedDescription + traceback:nil]); } } if (nil != [settings objectForKey:FB_SETTING_INCLUDE_NON_MODAL_ELEMENTS]) { @@ -427,7 +433,7 @@ + (NSArray *)routes NSError *error; if (![FBConfiguration setScreenshotOrientation:(NSString *)[settings objectForKey:FB_SETTING_SCREENSHOT_ORIENTATION] error:&error]) { - return FBResponseWithStatus([FBCommandStatus invalidArgumentErrorWithMessage:error.description + return FBResponseWithStatus([FBCommandStatus invalidArgumentErrorWithMessage:error.localizedDescription traceback:nil]); } } diff --git a/WebDriverAgentLib/Routing/FBExceptionHandler.m b/WebDriverAgentLib/Routing/FBExceptionHandler.m index 7b7a8263f..811a0efb5 100644 --- a/WebDriverAgentLib/Routing/FBExceptionHandler.m +++ b/WebDriverAgentLib/Routing/FBExceptionHandler.m @@ -45,6 +45,9 @@ - (void)handleException:(NSException *)exception forResponse:(RouteResponse *)re } else if ([exception.name isEqualToString:FBTimeoutException]) { commandStatus = [FBCommandStatus timeoutErrorWithMessage:exception.reason traceback:traceback]; + } else if ([exception.name isEqualToString:FBSessionCreationException]) { + commandStatus = [FBCommandStatus sessionNotCreatedError:exception.reason + traceback:traceback]; } else { commandStatus = [FBCommandStatus unknownErrorWithMessage:exception.reason traceback:traceback]; diff --git a/WebDriverAgentLib/Routing/FBExceptions.h b/WebDriverAgentLib/Routing/FBExceptions.h index 1c9507a19..a7263bae4 100644 --- a/WebDriverAgentLib/Routing/FBExceptions.h +++ b/WebDriverAgentLib/Routing/FBExceptions.h @@ -14,6 +14,9 @@ NS_ASSUME_NONNULL_BEGIN /*! Exception used to notify about missing session */ extern NSString *const FBSessionDoesNotExistException; +/*! Exception used to notify about session creation issues */ +extern NSString *const FBSessionCreationException; + /*! Exception used to notify about application deadlock */ extern NSString *const FBApplicationDeadlockDetectedException; diff --git a/WebDriverAgentLib/Routing/FBExceptions.m b/WebDriverAgentLib/Routing/FBExceptions.m index 571cea4fb..9e1f2141f 100644 --- a/WebDriverAgentLib/Routing/FBExceptions.m +++ b/WebDriverAgentLib/Routing/FBExceptions.m @@ -10,6 +10,7 @@ #import "FBExceptions.h" NSString *const FBInvalidArgumentException = @"FBInvalidArgumentException"; +NSString *const FBSessionCreationException = @"FBSessionCreationException"; NSString *const FBSessionDoesNotExistException = @"FBSessionDoesNotExistException"; NSString *const FBApplicationDeadlockDetectedException = @"FBApplicationDeadlockDetectedException"; NSString *const FBElementAttributeUnknownException = @"FBElementAttributeUnknownException";