From 1caddd4a37d2e85c65e36065bdd47cf946e9e4c1 Mon Sep 17 00:00:00 2001 From: Javed Khan Date: Tue, 16 Sep 2014 01:44:44 +0530 Subject: [PATCH] handle http errors ok @jrick, @jcvernaleo --- jsonapi.go | 12 ++++++++++++ jsonresults.go | 6 ------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/jsonapi.go b/jsonapi.go index 0e37bd23..8d2a8b94 100644 --- a/jsonapi.go +++ b/jsonapi.go @@ -8,8 +8,17 @@ import ( "encoding/json" "errors" "fmt" + "net/http" ) +// BadStatusCode describes a HTTP error when a response has non-200 status code +type BadStatusCode int + +func (e BadStatusCode) Error() string { + status := int(e) + return fmt.Sprintf("http bad status: %d %s", status, http.StatusText(status)) +} + // ErrIncorrectArgTypes describes an error where the wrong argument types // are present. var ErrIncorrectArgTypes = errors.New("incorrect arguement types") @@ -696,6 +705,9 @@ func rpcRawCommand(user string, password string, server string, err := fmt.Errorf("error sending json message: " + err.Error()) return result, err } + if resp.StatusCode != http.StatusOK { + return nil, BadStatusCode(resp.StatusCode) + } result, err = GetRaw(resp.Body) if err != nil { err := fmt.Errorf("error getting json reply: %v", err) diff --git a/jsonresults.go b/jsonresults.go index 3fb23979..71a6418f 100644 --- a/jsonresults.go +++ b/jsonresults.go @@ -8,7 +8,6 @@ import ( "bytes" "encoding/json" "fmt" - "strings" ) // BlockResult models the data from the getblock command when the verbose flag @@ -446,11 +445,6 @@ func ReadResultCmd(cmd string, message []byte) (Reply, error) { var objmap map[string]json.RawMessage err = json.Unmarshal(message, &objmap) if err != nil { - if strings.Contains(string(message), "401 Unauthorized.") { - err = fmt.Errorf("authentication error") - } else { - err = fmt.Errorf("error unmarshalling json reply: %v", err) - } return result, err } // Take care of the parts that are the same for all replies.