-
Notifications
You must be signed in to change notification settings - Fork 17
/
errors.go
80 lines (68 loc) · 2.02 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package cbyge
import (
"encoding/json"
"github.com/pkg/errors"
)
const (
RemoteErrorCodeAccessTokenRefresh = 4031022
RemoteErrorCodePasswordError = 4001007
RemoteErrorCodeUserNotExists = 4041011
RemoteErrorCodePropertyNotExists = 4041009
)
// A RemoteCallError is triggered when the packet server returns an
// unspecified error.
var RemoteCallError = errors.New("the server returned with an error")
// An UnreachableError is triggered when a device cannot be reached
// through any wifi-connected switch.
var UnreachableError = errors.New("the device cannot be reached")
// A RemoteError is an error message returned by the HTTPS API server.
type RemoteError struct {
Msg string `json:"msg"`
Code int `json:"code"`
Context string
}
func decodeRemoteError(data []byte, context string) *RemoteError {
var res struct {
Error *RemoteError `json:"error"`
}
if err := json.Unmarshal(data, &res); err != nil {
return nil
}
if res.Error != nil {
res.Error.Context = context
}
return res.Error
}
func (l *RemoteError) Error() string {
if l.Context == "" {
return l.Msg
}
return l.Context + ": " + l.Msg
}
// IsAccessTokenError returns true if the error is an API error that can be
// solved by refreshing the access token.
func IsAccessTokenError(err error) bool {
return isErrorWithCode(err, RemoteErrorCodeAccessTokenRefresh)
}
// IsCredentialsError returns true if the error was the result of a bad
// username or password.
func IsCredentialsError(err error) bool {
return isErrorWithCode(err, RemoteErrorCodePasswordError, RemoteErrorCodeUserNotExists)
}
// IsPropertyNotExistsError returns true if an error was the result of looking
// up properties for a device without properties.
func IsPropertyNotExistsError(err error) bool {
return isErrorWithCode(err, RemoteErrorCodePropertyNotExists)
}
func isErrorWithCode(err error, codes ...int) bool {
var re *RemoteError
if !errors.As(err, &re) {
return false
}
for _, code := range codes {
if re.Code == code {
return true
}
}
return false
}