-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
57 lines (50 loc) · 1.89 KB
/
Copy patherrors.go
File metadata and controls
57 lines (50 loc) · 1.89 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
package switchos
import (
"errors"
"fmt"
)
// ErrUnsupported is returned when a requested operation or whole endpoint
// isn't supported by the connected device's board (e.g. GetPoe on
// BoardCSS318G, which has no PoE hardware). Individual fields that a
// board doesn't support are simply left nil in the returned struct
// instead of returning an error.
var ErrUnsupported = errors.New("switchos: operation not supported on this board")
// StatusError is returned when a SwOS device responds with a non-200
// HTTP status, or with a 200 status but no decodable JSON body (which
// happens on some devices/firmware when hitting an endpoint that isn't
// actually implemented - see the RB260GS v1.17 firmware caveat in the
// README).
type StatusError struct {
StatusCode int
Body []byte
}
func (e *StatusError) Error() string {
return fmt.Sprintf("switchos: unexpected response (status %d): %s", e.StatusCode, e.Body)
}
func checkResponse(statusCode int, body []byte, json200 bool) error {
if statusCode != 200 {
return &StatusError{StatusCode: statusCode, Body: body}
}
if !json200 {
return &StatusError{StatusCode: statusCode, Body: body}
}
return nil
}
// NameTooLongError is returned by PutLink/PutVlan when a name field
// (e.g. Link.Name, VlanEntry.Name) exceeds the connected board's
// firmware limit (see MaxNameLength). Without this check, an overlong
// name is silently truncated by the device instead of rejected, which
// otherwise only surfaces several layers away as a confusing "Provider
// produced inconsistent result after apply" error in Terraform.
type NameTooLongError struct {
Board Board
Field string
Name string
MaxLength int
}
func (e *NameTooLongError) Error() string {
return fmt.Sprintf(
"switchos: %s %q is %d bytes long, but board %s allows at most %d bytes - the device would silently truncate it",
e.Field, e.Name, len(e.Name), e.Board, e.MaxLength,
)
}