refactor(rpc): handlers.go - makeJSONRPCHandler() control flow refactor #3716
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.
I am proposing a simpler way to write the makeJSONRPCHandler function. There was a recent bug due to this function that turned lists of requests with a single item to a object that is a response--but not a list of responses. With this refactor, this function would be more resilient to bugs because of its simplified control flow: it first tries to unmarshal as an slice of requests, and returns an array of responses if successful. It then tries to unmarshal it into a single request, and returns a single response if successful. This refactor also abstracts the logic that process the requests to see if they actually need to be sent. All of that logic is now in the processRequest function, and improves on the previous implementation because now makeJSONRPCHandle is much easier to read and understand. If both unmarshals fail, the function returns an error "error unmarshalling request," same as before. Please let me know what you think of the refactor. Thanks.