Skip to content

Commit

Permalink
[dotnet] Treat unsuccessful http responses as errored commands (#13608)
Browse files Browse the repository at this point in the history
* Update HttpCommandExecutor.cs

* Parse only if json

* Added guards for NullRefException
  • Loading branch information
nvborisenko authored Mar 25, 2024
1 parent 4668df3 commit 3fab5fa
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
17 changes: 12 additions & 5 deletions dotnet/src/webdriver/Remote/HttpCommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,21 @@ private Response CreateResponse(HttpResponseInfo responseInfo)
{
Response response = new Response();
string body = responseInfo.Body;
if (responseInfo.ContentType != null && responseInfo.ContentType.StartsWith(JsonMimeType, StringComparison.OrdinalIgnoreCase))
if ((int)responseInfo.StatusCode < 200 || (int)responseInfo.StatusCode > 299)
{
response = Response.FromJson(body);
if (responseInfo.ContentType != null && responseInfo.ContentType.StartsWith(JsonMimeType, StringComparison.OrdinalIgnoreCase))
{
response = Response.FromErrorJson(body);
}
else
{
response.Status = WebDriverResult.UnhandledError;
response.Value = body;
}
}
else if (responseInfo.StatusCode.ToString().First() != '2')
else if (responseInfo.ContentType != null && responseInfo.ContentType.StartsWith(JsonMimeType, StringComparison.OrdinalIgnoreCase))
{
response.Status = WebDriverResult.UnhandledError;
response.Value = body;
response = Response.FromJson(body);
}
else
{
Expand Down
42 changes: 38 additions & 4 deletions dotnet/src/webdriver/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ private Response(Dictionary<string, object> rawResponse)
this.responseValue = valueDictionary["value"];
}
}
else if (valueDictionary.ContainsKey("error"))
{
this.responseStatus = WebDriverError.ResultFromError(valueDictionary["error"].ToString());
}
}
}

Expand Down Expand Up @@ -148,6 +144,44 @@ public static Response FromJson(string value)
return response;
}

/// <summary>
/// Returns a new <see cref="Response"/> from a JSON-encoded string.
/// </summary>
/// <param name="value">The JSON string to deserialize into a <see cref="Response"/>.</param>
/// <returns>A <see cref="Response"/> object described by the JSON string.</returns>
public static Response FromErrorJson(string value)
{
Dictionary<string, object> deserializedResponse = JsonConvert.DeserializeObject<Dictionary<string, object>>(value, new ResponseValueJsonConverter());

var response = new Response();

if (!deserializedResponse.TryGetValue("value", out var valueObject))
{
throw new WebDriverException($"The 'value' property was not found in the response:{Environment.NewLine}{value}");
}

if (valueObject is not Dictionary<string, object> valueDictionary)
{
throw new WebDriverException($"The 'value' property is not a dictionary of <string, object>{Environment.NewLine}{value}");
}

response.Value = valueDictionary;

if (!valueDictionary.TryGetValue("error", out var errorObject))
{
throw new WebDriverException($"The 'value > error' property was not found in the response:{Environment.NewLine}{value}");

This comment has been minimized.

Copy link
@limuyuan
}

if (errorObject is not string)
{
throw new WebDriverException($"The 'value > error' property is not a string{Environment.NewLine}{value}");
}

response.Status = WebDriverError.ResultFromError(errorObject.ToString());

return response;
}

/// <summary>
/// Returns this object as a JSON-encoded string.
/// </summary>
Expand Down

0 comments on commit 3fab5fa

Please sign in to comment.