Handle more error response codes for bluelink api
This commit is contained in:
parent
42dc963c95
commit
0a1f7caf55
2 changed files with 12 additions and 2 deletions
|
|
@ -41,6 +41,16 @@ func (e StatusError) StatusCode() int {
|
|||
return e.resp.StatusCode
|
||||
}
|
||||
|
||||
// HasStatus returns true if the respose's status code matches any of the given codes
|
||||
func (e StatusError) HasStatus(codes ...int) bool {
|
||||
for _, code := range codes {
|
||||
if e.resp.StatusCode == code {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ReadBody reads HTTP response and returns error on response codes other than HTTP 2xx
|
||||
func ReadBody(resp *http.Response) ([]byte, error) {
|
||||
defer resp.Body.Close()
|
||||
|
|
|
|||
|
|
@ -293,8 +293,8 @@ func (v *API) getStatus() (float64, error) {
|
|||
err = v.DoJSON(req, &resp)
|
||||
|
||||
if err != nil {
|
||||
resp := v.LastResponse()
|
||||
if resp != nil && resp.StatusCode == http.StatusForbidden {
|
||||
// handle http 401, 403
|
||||
if se, ok := err.(request.StatusError); ok && se.HasStatus(http.StatusUnauthorized, http.StatusForbidden) {
|
||||
err = errAuthFail
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue