Skip to content

Conversation

@biandratti
Copy link
Contributor

@biandratti biandratti commented Dec 4, 2020

Description

Today for every error in json rpc we return status code 200.
As per the 2.0 spec, there is no reason for using anything but 200 http status code whenever we are doing JSON-RPC over HTTP.
Also, returning 400 for valid JSON-RPC requests doesn't help when integrating with JSON-RPC clients.

Proposed Solution

Any valid JSON-RPC request should generate a JSON-RPC response, the HTTP status code should be 200. No matter if the response is a success or error response.
Calling a method that hasn't been enabled or a method that doesn't exist are valid JSON-RPC requests, therefore the expected response is a JSON-RPC error and the status code 200.
Any invalid JSON-RPC request (malformed json, etc) should generate a 400 status code.
So in this way we have status code 400 for json rpc error: [-32600, -32602, -32700]. In other cases we have status code 200.

Important Changes Introduced

Possibly requires change of integration with faucet ui and wallet.

@biandratti biandratti changed the title WIP - [ETCM-448] status code WIP [ETCM-448] status code Dec 4, 2020
@biandratti biandratti changed the title WIP [ETCM-448] status code WIP [ETCM-448] json rpc - status code Dec 4, 2020
Copy link
Contributor

@lemastero lemastero left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@lemastero
Copy link
Contributor

Please update the description of the issue :)

@biandratti biandratti changed the title WIP [ETCM-448] json rpc - status code [ETCM-448] json rpc - status code Dec 17, 2020
@biandratti biandratti added BREAKS API Affects services that interacts with APIs and removed don't merge labels Dec 17, 2020
@kapke
Copy link
Contributor

kapke commented Dec 18, 2020

@conqeror Please verify it's working with wallet in case of errors (it should)
@biandratti Does faucet handle these errors properly?

@biandratti
Copy link
Contributor Author

Yes @kapke, the node and faucet using the same class, both have the same behavior. So, with this, we need to change faucet ui and wallet ui.

Copy link
Contributor

@enriquerodbe enriquerodbe left a comment

Choose a reason for hiding this comment

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

Looks good to me, only comments for readability

private def handleResponse(f: Task[JsonRpcResponse]): Task[(StatusCode, JsonRpcResponse)] = f map { jsonRpcResponse =>
jsonRpcResponse.error match {
case Some(JsonRpcError(error, _, _))
if List(JsonRpcError.InvalidRequest.code, JsonRpcError.ParseError.code, JsonRpcError.InvalidParams().code)
Copy link
Contributor

Choose a reason for hiding this comment

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

For readability, this List of errors could be extracted to a constant at the class level or companion object

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


postRequest ~> Route.seal(mockJsonRpcHttpServer.route) ~> check {
status shouldEqual StatusCodes.OK
responseAs[String] shouldEqual
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think if instead of comparing Strings (which in case of an error forces the developer to compare the whole string to find the difference), we parse the response as Json and assert each attribute individually?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@kapke
Copy link
Contributor

kapke commented Dec 21, 2020

@biandratti I meant - if faucet's rpc client handles errors from mantis node properly? IIRC faucet is being run as a separate process communicating with Mantis over RPC.

@conqeror
Copy link

@kapke @biandratti
Regarding Faucet UI - it uses rpc client which already expects proper format. If I recall correctly, I was requesting proper rpc errors for faucet_sendFunds :)

Regarding mantis wallet - let me know when this gets merged and I will test it. I don't expect that it will require immediate changes, but it should enable us to handle errors more easily (...no more Internal JSON-RPC error everywhere :)

@biandratti
Copy link
Contributor Author

@biandratti I meant - if faucet's rpc client handles errors from mantis node properly? IIRC faucet is being run as a separate process communicating with Mantis over RPC.

Good question @kapke
Any invalid JSON-RPC request (malformed json, etc) should generate a 400 status code. Any way, if we have this error between faucet and node, we probably will have an integration error. Because any invalid json rpc request is validated first from faucet.
So in this way if we have an error with status code 200 or 400 (json rpc error: [-32600, -32602, -32700]) between the faucet and node always we will receive a json rpc error -32000 (status code 200).
Here we can see how it is resolved.

Left(JsonRpcError.LogicError(s"Faucet error: $error"))

Copy link
Contributor

@kapke kapke left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Contributor

@lemastero lemastero left a comment

Choose a reason for hiding this comment

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

LGTM 👍 Nice work.

@biandratti biandratti merged commit 8eea376 into develop Dec 22, 2020
@biandratti biandratti deleted the feature-status-code branch December 22, 2020 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BREAKS API Affects services that interacts with APIs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants