fix: Websocket propper error when the payload size is too big #756
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this PR do ?
If the sdk is using a WebSocket and trying to send a payload > to the maxPayload defined in the backend ( by default 1mb but can be extended in the kuzzlerc) we are getting a Network error with no real indication on what happened instead of a proper 413 error max payload size exceeded.
This bug is happening because of the way the uWebSocket library is handling the maxPayload case. If a payload is received in the socket with a size over the maxPayload the connection gets automatically closed see here. The connection being closed doesn't allow us to provide a propper error message on the backend side. Instead we have to adapt the sdk by doing the following:
When we open a connection we also send a request to the backend
controller: server; action: getConfig
where reside the defined maxRequestSize of the server. We store this value in this.maxPayloadSize in the WebSockerProtocol Class. Each time we perform a request, we have a if condition checking if the payload we are trying to send is not over this value. If so, we properly close the connection before sending the message as it should be done.here is the new type of error message we get now (here the server limit is 2mb):
How should this be manually tested?
You might also have a package.json like the following
This command should try to send a request to the backend using websocket. In the code you must also add a file under 1mb and one > to 1mb, big-file.png is an example where I used an heavy image to reproduce the bug.