handle http errors

ok @jrick, @jcvernaleo
This commit is contained in:
Javed Khan 2014-09-16 01:44:44 +05:30 committed by John C. Vernaleo
parent 26802c7ecc
commit 1caddd4a37
2 changed files with 12 additions and 6 deletions

View file

@ -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)

View file

@ -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.