Skip to content

Commit

Permalink
handle http errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxcanfly authored and jcvernaleo committed Sep 16, 2014
1 parent 26802c7 commit 1caddd4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
12 changes: 12 additions & 0 deletions jsonapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 0 additions & 6 deletions jsonresults.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"strings"
)

// BlockResult models the data from the getblock command when the verbose flag
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 1caddd4

Please sign in to comment.