forked from viamrobotics/rdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
87 lines (69 loc) · 2.67 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
package resource
import (
"fmt"
"github.com/pkg/errors"
"go.viam.com/rdk/utils"
)
// NewNotFoundError is used when a resource is not found.
func NewNotFoundError(name Name) error {
return ¬FoundError{name}
}
// IsNotFoundError returns if the given error is any kind of not found error.
func IsNotFoundError(err error) bool {
var errArt *notFoundError
return errors.As(err, &errArt)
}
type notFoundError struct {
name Name
}
func (e *notFoundError) Error() string {
return fmt.Sprintf("resource %q not found", e.name)
}
// NewNotAvailableError is used when a resource is not available because of some error.
func NewNotAvailableError(name Name, err error) error {
return ¬AvailableError{name, err}
}
// IsNotAvailableError returns if the given error is any kind of not available error.
func IsNotAvailableError(err error) bool {
var errArt *notAvailableError
return errors.As(err, &errArt)
}
type notAvailableError struct {
name Name
reason error
}
func (e *notAvailableError) Error() string {
return fmt.Sprintf("resource %q not available; reason=%q", e.name, e.reason)
}
// NewMustRebuildError is returned when a resource cannot be reconfigured in place and
// instead must be rebuilt. Almost all models/drivers should be able to reconfigure in
// place to support the best user experience.
func NewMustRebuildError(name Name) error {
return &mustRebuildError{name: name}
}
// IsMustRebuildError returns whether or not the given error is a MustRebuildError.
func IsMustRebuildError(err error) bool {
var errArt *mustRebuildError
return errors.As(err, &errArt)
}
type mustRebuildError struct {
name Name
}
func (e *mustRebuildError) Error() string {
return fmt.Sprintf("cannot reconfigure %q; must rebuild", e.name)
}
// DependencyNotFoundError is used when a resource is not found in a dependencies.
func DependencyNotFoundError(name Name) error {
// This error represents a logical configuration error. No need to include a stack trace.
return fmt.Errorf("Resource missing from dependencies. Resource: %v", name)
}
// DependencyTypeError is used when a resource doesn't implement the expected interface.
func DependencyTypeError[T Resource](name Name, actual interface{}) error {
// This error represents a coding error. Include a stack trace for diagnostics.
return errors.Errorf("dependency %q should be an implementation of %s but it was a %T", name, utils.TypeStr[T](), actual)
}
// TypeError is used when a resource is an unexpected type.
func TypeError[T Resource](actual Resource) error {
// This error represents a coding error. Include a stack trace for diagnostics.
return errors.Errorf("expected implementation of %s but it was a %T", utils.TypeStr[T](), actual)
}