Commit 2228b35b authored by Sietse Ringers's avatar Sietse Ringers
Browse files

Better error handling in HTTP transport

parent d2e939bb
...@@ -112,6 +112,10 @@ const ( ...@@ -112,6 +112,10 @@ const (
ErrorKeyshare = ErrorCode("keyshare") ErrorKeyshare = ErrorCode("keyshare")
// Keyshare server has blocked us // Keyshare server has blocked us
ErrorKeyshareBlocked = ErrorCode("keyshareBlocked") ErrorKeyshareBlocked = ErrorCode("keyshareBlocked")
// API server error
ErrorApi = ErrorCode("api")
// Server returned unexpected or malformed response
ErrorServerResponse = ErrorCode("serverResponse")
) )
func (e *Error) Error() string { func (e *Error) Error() string {
......
...@@ -18,7 +18,7 @@ type HTTPTransport struct { ...@@ -18,7 +18,7 @@ type HTTPTransport struct {
headers map[string]string headers map[string]string
} }
const verbose = true const verbose = false
// NewHTTPTransport returns a new HTTPTransport. // NewHTTPTransport returns a new HTTPTransport.
func NewHTTPTransport(serverURL string) *HTTPTransport { func NewHTTPTransport(serverURL string) *HTTPTransport {
...@@ -61,7 +61,6 @@ func (transport *HTTPTransport) request(url string, method string, result interf ...@@ -61,7 +61,6 @@ func (transport *HTTPTransport) request(url string, method string, result interf
marshaled, err := json.Marshal(object) marshaled, err := json.Marshal(object)
if err != nil { if err != nil {
return &Error{Err: err, ErrorCode: ErrorSerialization} return &Error{Err: err, ErrorCode: ErrorSerialization}
//return &TransportError{Err: err.Error()}
} }
if verbose { if verbose {
fmt.Printf("POST %s: %s\n", url, string(marshaled)) fmt.Printf("POST %s: %s\n", url, string(marshaled))
...@@ -89,23 +88,23 @@ func (transport *HTTPTransport) request(url string, method string, result interf ...@@ -89,23 +88,23 @@ func (transport *HTTPTransport) request(url string, method string, result interf
res, err := transport.client.Do(req) res, err := transport.client.Do(req)
if err != nil { if err != nil {
return &Error{Err: err, ErrorCode: ErrorTransport} return &Error{ErrorCode: ErrorTransport, Err: err}
} }
body, err := ioutil.ReadAll(res.Body) body, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
return &Error{Err: err, Status: res.StatusCode} return &Error{ErrorCode: ErrorServerResponse, Err: err, Status: res.StatusCode}
} }
if res.StatusCode != 200 { if res.StatusCode != 200 {
apierr := &ApiError{} apierr := &ApiError{}
json.Unmarshal(body, apierr) json.Unmarshal(body, apierr)
if apierr.ErrorName == "" { // Not an ApiErrorMessage if apierr.ErrorName == "" { // Not an ApiErrorMessage
return &Error{ErrorCode: ErrorTransport, Status: res.StatusCode} return &Error{ErrorCode: ErrorServerResponse, Status: res.StatusCode}
} }
if verbose { if verbose {
fmt.Printf("ERROR: %+v\n", apierr) fmt.Printf("ERROR: %+v\n", apierr)
} }
return &Error{ErrorCode: ErrorTransport, Status: res.StatusCode, ApiError: apierr} return &Error{ErrorCode: ErrorApi, Status: res.StatusCode, ApiError: apierr}
} }
if verbose { if verbose {
...@@ -116,7 +115,7 @@ func (transport *HTTPTransport) request(url string, method string, result interf ...@@ -116,7 +115,7 @@ func (transport *HTTPTransport) request(url string, method string, result interf
} else { } else {
err = json.Unmarshal(body, result) err = json.Unmarshal(body, result)
if err != nil { if err != nil {
return &Error{Err: err, Status: res.StatusCode} return &Error{ErrorCode: ErrorServerResponse, Err: err, Status: res.StatusCode}
} }
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment