-
Notifications
You must be signed in to change notification settings - Fork 266
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add custom HttpStatusCodeProvider to adjust to JSON-RPC 2.0 conventions #1522
Add custom HttpStatusCodeProvider to adjust to JSON-RPC 2.0 conventions #1522
Conversation
b5a0475
to
382315d
Compare
import static com.googlecode.jsonrpc4j.ErrorResolver.JsonError; | ||
import static com.googlecode.jsonrpc4j.ErrorResolver.JsonError.*; | ||
|
||
public class Web3HttpStatusCodeProvider implements HttpStatusCodeProvider { |
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.
Based on this class (which we used prior to this change).
FYI: version 1.6 of jsonrpc4j doesn't adjust error handling to the JSON-RPC guidelines, that's why I decided not to update it this PR.
*/ | ||
@Override | ||
public Integer getJsonRpcCode(int httpStatusCode) { | ||
throw new UnsupportedOperationException("Cannot possibly determine a single JSON RPC from an HTTP status code"); |
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.
jsonrpc4j maps each supported HTTP status code to a single JSON-RPC code - this is not entirely correct in my opinion, since a single HTTP code could map to N JSON-RPC codes (this even happens in jsonrpc4j).
Since we don't use this method in rskj, I decided to leave it like this.
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.
Take into account that a JSON RPC request could be an array of valid JSON RPC requests. Is this case covered in the process of responses/responses code?
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.
Since I just swapped the DefaultHttpStatusCodeProvider
(from jsonrpc4j library) for a custom implementation that adheres to the same signatures (but mapping HTTP codes differently), the actual processing of requests is not involved (this is only used in the response handling part of the pipeline).
pipeline: run |
pipeline:run |
*/ | ||
@Override | ||
public Integer getJsonRpcCode(int httpStatusCode) { | ||
throw new UnsupportedOperationException("Cannot possibly determine a single JSON RPC from an HTTP status code"); |
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.
Take into account that a JSON RPC request could be an array of valid JSON RPC requests. Is this case covered in the process of responses/responses code?
} | ||
|
||
private boolean isBadRequestResultCode(int resultCode) { | ||
Optional<JsonError> optionalJsonError = BAD_REQUEST_JSON_ERRORS |
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 not a simple contains? Or a map of JSON ERROR to boolean (true == it is a bad request result code)?
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.
I didn't use a contains
because it's a list of JsonError
and what I receive is an integer (somehow I have to reach the code
member variable).
Let me know what you think about the change I just uploaded (instead of using a JsonError list y directly use a collection of the codes, then I just use a contains
).
pipeline:run |
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.
lgtm
rskj-core/src/main/java/co/rsk/rpc/netty/Web3HttpStatusCodeProvider.java
Outdated
Show resolved
Hide resolved
rskj-core/src/main/java/co/rsk/rpc/netty/Web3ResultHttpResponseHandler.java
Outdated
Show resolved
Hide resolved
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE; | ||
import static io.netty.handler.codec.http.HttpHeaders.Values.APPLICATION_JSON; | ||
|
||
public class Web3ResultHttpResponseHandler extends SimpleChannelInboundHandler<Web3Result> { | ||
|
||
private HttpStatusCodeProvider httpStatusCodeProvider; | ||
|
||
public Web3ResultHttpResponseHandler(HttpStatusCodeProvider httpStatusCodeProvider) { |
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.
I guess using this 3rd-party interface is perfectly fine here, but as an option it could also be IntFunction<HttpResponseStatus> errorCodeToHttpStatusMapper
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.
I think it's an interesting suggestion, but considering that we already depend on many classes of com.googlecode.jsonrpc4 for JSON-RPC handling, I think it'd be overkill (besides, I guess rskj shouldn't have the responsibility of defining stuff related to this). Is it ok if I leave it like this?
pipeline: run |
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.
lgtm!
It'd be nice to cover it also in the integration tests
pipeline: run |
1 similar comment
pipeline: run |
fecfb31
to
6e6e16d
Compare
6e6e16d
to
7a619c1
Compare
pipeline: run |
pipeline:run |
2 similar comments
pipeline:run |
pipeline:run |
Description
Since the jsonrpc4j keeps on returning internal error HTTP status codes upon JSON-RPC errors (it shouldn't, since the transport layer protocol should be agnostic to JSON-RPC), we add custom logic to handle them.
Inspired on this, 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 status code = 200)
Any invalid JSON-RPC request (malformed JSON, etc.) should generate a 400 status code.
Motivation and Context
Issue #1387
How Has This Been Tested?
Unit testing
Types of changes
Checklist: