-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
78 lines (67 loc) · 2.35 KB
/
Copy patherrors.go
File metadata and controls
78 lines (67 loc) · 2.35 KB
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
package foxfire
import (
"errors"
"fmt"
"net/http"
"strings"
)
// Sentinel errors callers are expected to match on.
var (
// ErrLinkButtonNotPressed is returned by Pair when the physical button on
// the bridge has not been pressed within the preceding 30 seconds.
ErrLinkButtonNotPressed = errors.New("foxfire: link button not pressed")
// ErrUnauthorized means the application key was rejected. Keys can be
// revoked from the Hue app, so this is not necessarily a bug.
ErrUnauthorized = errors.New("foxfire: unauthorized")
// ErrNotFound means the resource ID does not exist on this bridge.
ErrNotFound = errors.New("foxfire: resource not found")
// ErrBridgeIdentity means the TLS peer did not present a certificate
// matching the expected bridge ID. Treat this as hostile until proven
// otherwise.
ErrBridgeIdentity = errors.New("foxfire: bridge certificate identity mismatch")
// ErrNoBridges is returned by Discover when neither mDNS nor the cloud
// discovery endpoint yielded a bridge.
ErrNoBridges = errors.New("foxfire: no bridges found")
)
// apiError is a single entry from the bridge's errors array.
type apiError struct {
Description string `json:"description"`
}
// APIError aggregates the errors array returned by the bridge alongside the
// HTTP status, since the bridge will happily return 200 with a non-empty
// errors array for partially applied updates.
type APIError struct {
StatusCode int
Descriptions []string
}
func (e *APIError) Error() string {
if len(e.Descriptions) == 0 {
return fmt.Sprintf("foxfire: bridge returned HTTP %d", e.StatusCode)
}
return fmt.Sprintf("foxfire: bridge returned HTTP %d: %s",
e.StatusCode, strings.Join(e.Descriptions, "; "))
}
// Unwrap maps bridge statuses onto the sentinels so that errors.Is works for
// the cases callers actually branch on.
func (e *APIError) Unwrap() error {
switch e.StatusCode {
case http.StatusUnauthorized, http.StatusForbidden:
return ErrUnauthorized
case http.StatusNotFound:
return ErrNotFound
}
return nil
}
func errorsFrom(status int, in []apiError) error {
if len(in) == 0 && status >= 200 && status < 300 {
return nil
}
descs := make([]string, 0, len(in))
for _, e := range in {
descs = append(descs, e.Description)
}
if status >= 200 && status < 300 && len(descs) == 0 {
return nil
}
return &APIError{StatusCode: status, Descriptions: descs}
}