Skip to content

Commit 0a1f7ca

Browse files
committed
Handle more error response codes for bluelink api
1 parent 42dc963 commit 0a1f7ca

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

util/request/functions.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ func (e StatusError) StatusCode() int {
4141
return e.resp.StatusCode
4242
}
4343

44+
// HasStatus returns true if the respose's status code matches any of the given codes
45+
func (e StatusError) HasStatus(codes ...int) bool {
46+
for _, code := range codes {
47+
if e.resp.StatusCode == code {
48+
return true
49+
}
50+
}
51+
return false
52+
}
53+
4454
// ReadBody reads HTTP response and returns error on response codes other than HTTP 2xx
4555
func ReadBody(resp *http.Response) ([]byte, error) {
4656
defer resp.Body.Close()

vehicle/bluelink/bluelink.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ func (v *API) getStatus() (float64, error) {
293293
err = v.DoJSON(req, &resp)
294294

295295
if err != nil {
296-
resp := v.LastResponse()
297-
if resp != nil && resp.StatusCode == http.StatusForbidden {
296+
// handle http 401, 403
297+
if se, ok := err.(request.StatusError); ok && se.HasStatus(http.StatusUnauthorized, http.StatusForbidden) {
298298
err = errAuthFail
299299
}
300300
}

0 commit comments

Comments
 (0)