-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
140 lines (117 loc) · 4.83 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package shelly
import "fmt"
// https://shelly-api-docs.shelly.cloud/gen2/ComponentsAndServices/HTTP
// https://shelly-api-docs.shelly.cloud/gen2/General/CommonErrors
// ShellyErrorCode describes errors return from the Shelly RPC API.
type ShellyErrorCode int
// BadStatusWithMessageError provides a detailed description of errors from the Shelly RPC API.
type BadStatusWithMessageError struct {
Status ShellyErrorCode
Msg string
}
func (err *BadStatusWithMessageError) Error() string {
return fmt.Sprintf("RPC Bad Status %d: %s", err.Status, err.Msg)
}
// Unwrap implements errors.Unwrapper and unwraps the underlying status without the message.
// This is useful because you can use match the errors below with errors.Is() instead of a
// bunch of gross type casting.
func (err *BadStatusWithMessageError) Unwrap() error {
return err.Status
}
var (
// ErrRPCInvalidOrMissingArguments is returned when there are invalid or missing arguments.
ErrRPCInvalidOrMissingArguments = ShellyErrorCode(-103)
// ErrRPCDeadlineExceeded indicates the request timed out
ErrRPCDeadlineExceeded = ShellyErrorCode(-104)
// ErrRPCUnknownComponentID is returned when requesting a component id which is not supported
// by the device (ex. switch?id=2 on single switch device).
ErrRPCUnknownComponentID = ShellyErrorCode(-105)
// ErrRPCResourcesExhausted is returned if the response body payload was too large to handle.
ErrRPCResourcesExhausted = ShellyErrorCode(-108)
// ErrRPCFailedPrecondition is returned when a precondition for a requested action is not
// satisfied. For example, when you try to turn a switch on in a situation of overpower
// condition, or when a reboot has been scheduled and the device is shutting down.
ErrRPCFailedPrecondition = ShellyErrorCode(-109)
// ErrRPCUnavailable is a generic error returned by the device for "other error conditions".
ErrRPCUnavailable = ShellyErrorCode(-114)
// ErrRPCNameNotResolved is returned when the request name cannot be resolved.
ErrRPCNameNotResolved = ShellyErrorCode(-10)
// ErrRPCSendingDataToRemotePeerFailed ...
ErrRPCSendingDataToRemotePeerFailed = ShellyErrorCode(-11)
// ErrRPCHeaderParseError ...
ErrRPCHeaderParseError = ShellyErrorCode(-12)
// ErrRPCUnsupportedEncoding ...
ErrRPCUnsupportedEncoding = ShellyErrorCode(-13)
// ErrRPCResponseTooBig is returned when the response body cannot fit into a frame.
ErrRPCResponseTooBig = ShellyErrorCode(-14)
// ErrRPCParsingBody is returned when an error occurs parsing the request body.
ErrRPCParsingBody = ShellyErrorCode(-15)
// ErrRPCConnectionClosedPrematurely is returned when the connection is closed prematurely.
ErrRPCConnectionClosedPrematurely = ShellyErrorCode(-16)
// ErrRPCTooManyRedirects is returned when too many HTTP redirects occur.
ErrRPCTooManyRedirects = ShellyErrorCode(-17)
// ErrRPCHTTPErrorResponse ...
ErrRPCHTTPErrorResponse = ShellyErrorCode(-18)
// ErrRPCNoHandler is returned when a call is made to an unknown handler.
// NOTE: This is not documented, but was seen.
ErrRPCNoHandler = ShellyErrorCode(404)
)
// Error implements the `error` interface.
func (err ShellyErrorCode) Error() string {
var msg string
switch err {
case -103:
// -103 for invalid or missing arguments
msg = "invalid or missing arguments"
case -104:
// -104 "Deadline exceeded" when the request times out
msg = "deadline exceeded"
case -105:
// -105 isn't officially documented but has been empirically when requesting the status
// of a component not supported by the device (ex. switch?id=2 on single switch device).
msg = "unknown component ID"
case -108:
// -108 "Resource exhausted" if the response body payload was too large to handle
msg = "resource exhausted"
case -109:
// -109 This error is received when a precondition for a requested action is not satisfied.
// For example, when you try to turn a switch on in a situation of overpower condition, or
// when a reboot has been scheduled and the device is shutting down.
msg = "failed precondition"
case -114:
// -114 "Unavailable" for other error conditions.
msg = "unavailable"
case -10:
// -10: Name not resolved
msg = "name not resolved"
case -11:
// -11: Sending data to remote peer failed
msg = "sending data to remote peer failed"
case -12:
// -12: Header parse error
msg = "header parse error"
case -13:
// -13: Unsupported encoding
msg = "unsupported encoding"
case -14:
// -14: Response too big
msg = "response too big"
case -15:
// -15: Body parse error
msg = "body parse error"
case -16:
// -16: Connection closed prematurely
msg = "connection closed prematurely"
case -17:
// -17: Too many redirects
msg = "too many redirects"
case -18:
// -18: HTTP Error Response
msg = "http error response"
case 404:
msg = "no handler for request"
default:
msg = "unknown error"
}
return fmt.Sprintf("rpc error: %s (%d)", msg, err)
}