Skip to content

Commit

Permalink
[dotnet] Handle unsuccessful http requests properly (#10807)
Browse files Browse the repository at this point in the history
* Remove code that can't be executed after the move from WebRequest to HttpClient

* Set response status to unhandled error in case if content type is not json
  • Loading branch information
nvborisenko authored Jul 13, 2022
1 parent 4aebd9c commit c0fa00f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 21 deletions.
26 changes: 6 additions & 20 deletions dotnet/src/webdriver/Remote/HttpCommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,26 +190,6 @@ public virtual Response Execute(Command commandToExecute)
}
catch (HttpRequestException ex)
{
WebException innerWebException = ex.InnerException as WebException;
if (innerWebException != null)
{
if (innerWebException.Status == WebExceptionStatus.Timeout)
{
string timeoutMessage = "The HTTP request to the remote WebDriver server for URL {0} timed out after {1} seconds.";
throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, timeoutMessage, requestInfo.FullUri.AbsoluteUri, this.serverResponseTimeout.TotalSeconds), ex);
}
else if (innerWebException.Status == WebExceptionStatus.ConnectFailure)
{
string connectFailureMessage = "Could not connect to the remote WebDriver server for URL {0}.";
throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, connectFailureMessage, requestInfo.FullUri.AbsoluteUri, this.serverResponseTimeout.TotalSeconds), ex);
}
else if (innerWebException.Response == null)
{
string nullResponseMessage = "A exception with a null response was thrown sending an HTTP request to the remote WebDriver server for URL {0}. The status of the exception was {1}, and the message was: {2}";
throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, nullResponseMessage, requestInfo.FullUri.AbsoluteUri, innerWebException.Status, innerWebException.Message), innerWebException);
}
}

string unknownErrorMessage = "An unknown exception was encountered sending an HTTP request to the remote WebDriver server for URL {0}. The exception message was: {1}";
throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, unknownErrorMessage, requestInfo.FullUri.AbsoluteUri, ex.Message), ex);
}
Expand All @@ -220,6 +200,12 @@ public virtual Response Execute(Command commandToExecute)
}

Response toReturn = this.CreateResponse(responseInfo);

if (toReturn.Status == WebDriverResult.Success && !((int)responseInfo.StatusCode >= 200 && (int)responseInfo.StatusCode <= 299))
{
toReturn.Status = WebDriverResult.UnhandledError;
}

return toReturn;
}

Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/WebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ protected virtual Response Execute(string driverCommandToExecute, Dictionary<str
{
commandResponse = this.executor.Execute(commandToExecute);
}
catch (System.Net.WebException e)
catch (System.Net.Http.HttpRequestException e)
{
commandResponse = new Response
{
Expand Down

0 comments on commit c0fa00f

Please sign in to comment.