-
Notifications
You must be signed in to change notification settings - Fork 5k
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
Feature/improve xml request error handling (#3947) #4298
Conversation
* Improve http request error handling * Update changelog.md * Update mock response * Add error code to HTTP connection error * Propagate the original request error (#3425) * Fixed a test (#3425) Co-authored-by: Liam Aharon <[email protected]>
Your Render PR Server URL is https://web3-js-pr-4298.onrender.com. Follow its progress at https://dashboard.render.com/static/srv-c4s1sfs1nok4ouigmojg. |
Pull Request Test Coverage Report for Build 1232103545
💛 - Coveralls |
For better understanding, we can see that on line And here we can see that |
@uluhonolulu Did you test that these changes solve #3425? Would it be possible for you to provide an example project where an |
@@ -97,7 +97,7 @@ HttpProvider.prototype.send = function (payload, callback) { | |||
var request = this._prepareRequest(); | |||
|
|||
request.onreadystatechange = function() { | |||
if (request.readyState === 4 && request.timeout !== 1) { | |||
if (request.readyState === 4 && request.timeout !== 1 && request.status !== 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per documentation this property returns (HTTP Status Codes)[https://developer.mozilla.org/en-US/docs/Web/HTTP/Status] and there is no status code 0
exists, so why are we checking it here?
Do we want to check if the status code exists? if yes then condition should not use a constant zero.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/status
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mentioned in the related issue, request.status
is 0
. Also mentioned here
@@ -97,7 +97,7 @@ HttpProvider.prototype.send = function (payload, callback) { | |||
var request = this._prepareRequest(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@spacesailor24 The PR referred in mentioned to upgrade xhr2-cookies
to node-xhr2
. I could not find that change incorporated here. Does the scope for this PR changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR does not include the package switch. What do y'all think about updating the package used?
Current package - Last release 4 years ago
Suggested package - Last release was Feb 9 2021
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@spacesailor24 could you check xhr2-cookies vs node-xhr2 with internal Lib Selection Checklist for Web3.js .
( build size impact, ..etc ) , lets open another PR for lib switch, if this lib update is not directly required for fix of xml request error handling ( this PR )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@spacesailor24 BTW node-xhr2 has the same issue (https://github.com/pwnall/node-xhr2/blob/master/src/001-xml_http_request.coffee#L608), but at least it's developed more actively so there's a chance they fix it.
As far as I see, the code looks pretty similar to xhr2-cookies, just rewritten in CoffeeScript.
@@ -112,6 +112,16 @@ HttpProvider.prototype.send = function (payload, callback) { | |||
} | |||
}; | |||
|
|||
//since XHR2._onHttpRequestError swallows the initial request error, we need to get it from the underlying request | |||
request.addEventListener('loadstart', function() { | |||
var clientRequest = request._request; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If request
is an object of XMLHttpRequest
then I could not find any documented reference of _request
property. We should avoid using such private behaviour of a native object.
Also the request
object could be either xhr2-cookies
or XMLHttpRequest
so such private attributes can't provide confirmation of availability on different type of objects.
Did you tried top level events error
or abort
? As per docs if there is any error error
should be fired after loadstart
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_request
is a private property, and this PR would likely break if we switch out the package to use node-xhr2
This PR was originally created by @uluhonolulu, and I'm not really sure how to setup an environment to test that this works as intended. However, how we adjust this specific code is dependent on what package we choose to go with
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why we are binding clientRequest.on('error'...
when request started loading loadstart
? shouldnt it be done directly with request
inside HttpProvider.prototype.send
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
request
is an object ofXMLHttpRequest
then I could not find any documented reference of_request
property. We should avoid using such private behaviour of a native object.Also the
request
object could be eitherxhr2-cookies
orXMLHttpRequest
so such private attributes can't provide confirmation of availability on different type of objects.Did you tried top level events
error
orabort
? As per docs if there is any errorerror
should be fired afterloadstart
@nazarhussain this is my hacky attempt to get the underlying error, because xhr2-cookies just swallows it (see souldreamer/xhr2-cookies#9).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why we are binding
clientRequest.on('error'...
when request started loadingloadstart
? shouldnt it be done directly withrequest
insideHttpProvider.prototype.send
?
@jdevcs because request._request
doesn't exist at this point yet.
Exactly. This is why I suggested switching to node-xhr2. |
@spacesailor24 I tested it with my own project and I'm 100% sure it worked as intended, but it was half a year ago so I don't even remember how I invoked the error. Most probably I just tried to connect to a node that didn't exist. I can provide a test project, but TBH I'm in a middle of moving to a new apt so it's going to be the next weekend at best. |
@uluhonolulu okay! no worries on the test project, I'll try connecting to a node that doesn't exist. Thank you for your patience on getting this merged, apologies for it taking so long! |
My opinion on this is that we shouldn't even be using an XML HTTP package and should rely either on the native Node.js HTTP package or something like Fetch or Axios. However, as most of our tests make use of the |
Well
First it is a false error, because the response was not invalid it was throwed by an error, second I never expected that in case of HTTP error I won't get any notification. |
Thanks for bringing this discussion in 1.x, our team will consider this request error handling improvement in web3.js 4.x. |
This PR has been automatically marked as stale beacause it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment. |
Closing this in favor of 4.x
|
I took over #3947 to fix
CHANGELOG.md