private static void UnpackAndThrowOnError(Response errorResponse)
{
// Check the status code of the error, and only handle if not success.
if (errorResponse.Status != WebDriverResult.Success)
{
Dictionary<string, object> errorAsDictionary = errorResponse.Value as Dictionary<string, object>;
if (errorAsDictionary != null)
{
ErrorResponse errorResponseObject = new ErrorResponse(errorAsDictionary);
string errorMessage = errorResponseObject.Message;
switch (errorResponse.Status)
{
case WebDriverResult.NoSuchElement:
throw new NoSuchElementException(errorMessage);
case WebDriverResult.NoSuchFrame:
throw new NoSuchFrameException(errorMessage);
case WebDriverResult.UnknownCommand:
throw new NotImplementedException(errorMessage);
case WebDriverResult.ObsoleteElement:
throw new StaleElementReferenceException(errorMessage);
case WebDriverResult.ElementNotDisplayed:
throw new ElementNotVisibleException(errorMessage);
case WebDriverResult.InvalidElementState:
case WebDriverResult.ElementNotSelectable:
throw new InvalidElementStateException(errorMessage);
case WebDriverResult.UnhandledError:
throw new InvalidOperationException(errorMessage);
case WebDriverResult.NoSuchDocument:
throw new NoSuchElementException(errorMessage);
case WebDriverResult.Timeout:
throw new TimeoutException("The driver reported that the command timed out. There may "
+ "be several reasons for this. Check that the destination "
+ "site is in IE's 'Trusted Sites' (accessed from Tools->"
+ "Internet Options in the 'Security' tab) If it is a "
+ "trusted site, then the request may have taken more than "
+ "a minute to finish.");
case WebDriverResult.NoSuchWindow:
throw new NoSuchWindowException(errorMessage);
case WebDriverResult.InvalidCookieDomain:
case WebDriverResult.UnableToSetCookie:
throw new WebDriverException(errorMessage);
case WebDriverResult.AsyncScriptTimeout:
throw new TimeoutException(errorMessage);
case WebDriverResult.NoAlertPresent:
throw new NoAlertPresentException(errorMessage);
default:
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "{0} ({1})", errorMessage, errorResponse.Status));
}
}
else
{
throw new WebDriverException("Unexpected error. " + errorResponse.Value.ToString());
}
}
}