Skip to content

Commit

Permalink
Merge pull request #31 from etherlabsio:feat/add-client-response-chec…
Browse files Browse the repository at this point in the history
…kers

add; http client utility functions
  • Loading branch information
karthikmuralidharan authored Oct 17, 2019
2 parents 6cc4e3c + 5e294ac commit 44723d3
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions httputil/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package httputil

import (
"net/http"

"github.com/etherlabsio/errors"
)

// CheckResponse checks the API response for errors, and returns them if
// present. A response is considered an error if it has a status code outside
// the 200 range or equal to 202 Accepted.
// API error responses are expected to have either no response
// body, or a JSON response body that maps to ErrorResponse. Any other
// response body will be silently ignored.
//
// The error type will be *TwoFactorAuthError for two-factor authentication errors.
func CheckResponse(r *http.Response) error {
success := func(statusCode int) bool {
return 200 <= statusCode && statusCode < 500
}(r.StatusCode)
if success {
return nil
}
return errors.New("internal server error with http status: "+r.Status, errors.Internal)
}

// CheckOKResponse checks if the status code is within 2XX range
func CheckOKResponse(r *http.Response) error {
isOK := r.StatusCode >= 200 && r.StatusCode <= 299
if !isOK {
return errors.New("response error with http status: "+r.Status, errors.Internal)
}
return nil
}

0 comments on commit 44723d3

Please sign in to comment.