Skip to content

[java] Improving exception when server returns 504 (WIP) #9354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

alb-i986
Copy link
Contributor

@alb-i986 alb-i986 commented Apr 3, 2021

Description

Here I'm adding one extra condition to W3CHttpResponseCodec#decode in order to specifically handle 504 Gateway Time-out server errors.

This fix is very similar to the fix that was applied in 6a9c546 (see #7792).

Motivation and Context

While running a test on a Selenium Grid (v3.141.59), I got the following pretty unfriendly exception upon a WebElement#click call:

org.openqa.selenium.json.JsonException: Unable to determine type from: <. Last 1 characters read: <
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'Albertos-iMac.local', ip: '192.168.1.130', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.15.7', java.version: '1.8.0_281'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.json.JsonInput.peek(JsonInput.java:122)
    at org.openqa.selenium.json.JsonTypeCoercer.lambda$null$6(JsonTypeCoercer.java:140)
    at org.openqa.selenium.json.JsonTypeCoercer.coerce(JsonTypeCoercer.java:126)
    at org.openqa.selenium.json.Json.toType(Json.java:69)
    at org.openqa.selenium.json.Json.toType(Json.java:55)
    at org.openqa.selenium.json.Json.toType(Json.java:50)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:87)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
[..]

Only by debugging the test and stepping through the calls, I was able to find that the server was actually returning a plain text response:

<html><body><h1>504 Gateway Time-out</h1>
The server didn't respond in time.
</body></html>

Screenshot 2021-03-25 at 19 39 07

It shouldn't be required from the end user to do this.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed. (note: I only ran small tests)

PS: I was wondering if it makes sense to extend this special handling logic to any 4XX/5XX errors, instead of only to 405 and 504.
I mean, I would expect the server to return a Json only when the HTTP status is 200.

I did some research and I found that AbstractHttpResponseCodec (the superclass of W3CHttpResponseCodec) is already doing what I was thinking:

On the other hand, I also see that in W3CHttpResponseCodec there's a line which expects to get a JSON together with a 500 error 😕

HTTP_INTERNAL_ERROR == encodedResponse.getStatus()) {

@shs96c can you maybe spread some light? Tagging you as the author of W3CHttpResponseCodec

Thanks!

Add a special case in W3CHttpResponseCodec handling 504 Gateway Time-out errors.

Before, the end user used to see a pretty unfriendly error:

> JsonException: Unable to determine type from: <. Last 1 characters read: <
@sonarqubecloud
Copy link

sonarqubecloud bot commented Apr 3, 2021

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

Copy link
Member

@diemol diemol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @alb-i986!

Perhaps you can join Slack/IRC to continue the conversation about the question asked in this PR.

@diemol diemol merged commit 430c7a4 into SeleniumHQ:trunk Apr 6, 2021
@alb-i986 alb-i986 deleted the fix-W3CHttpResponseCodec-handle-500 branch May 25, 2021 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants