From 2b780d16b042054d07aa322146194118fd7f7b81 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Fri, 16 Sep 2016 14:40:27 -0500 Subject: [PATCH] Improve HTTP error handling. This modifies the code which handles failed server responses to attempt to unmarshal the response bytes as a regular JSON-RPC response regardless of the HTTP status code and, if that fails, return an error that includes the status code as well as the raw response bytes. This is being done because some JSON-RPC servers, such as Bitcoin Core, don't follow the intended HTTP status code meanings. For example, if you request a block that doesn't exist, it returns a status code of 500 which is supposed to mean internal server error instead of a standard 200 status code (since the HTTP request itself was successful) along with the JSON-RPC error response. The result is that errors from Core will now show up as an actual RPCError instead of an error with the raw JSON bytes. This also has the benefit of returning the HTTP status code in the error for real HTTP failure cases such as 401 authentication failures, which previously would just be an empty error when used against Core since it doesn't return the actual response along with the status code as it should. --- infrastructure.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/infrastructure.go b/infrastructure.go index fe6cbc90..66ca79d6 100644 --- a/infrastructure.go +++ b/infrastructure.go @@ -705,15 +705,15 @@ func (c *Client) handleSendPostMessage(details *sendPostDetails) { return } - // Handle unsuccessful HTTP responses - if httpResponse.StatusCode < 200 || httpResponse.StatusCode >= 300 { - jReq.responseChan <- &response{err: errors.New(string(respBytes))} - return - } - + // Try to unmarshal the response as a regular JSON-RPC response. var resp rawResponse err = json.Unmarshal(respBytes, &resp) if err != nil { + // When the response itself isn't a valid JSON-RPC response + // return an error which includes the HTTP status code and raw + // response bytes. + err = fmt.Errorf("status code: %d, response: %q", + httpResponse.StatusCode, string(respBytes)) jReq.responseChan <- &response{err: err} return }