-
Notifications
You must be signed in to change notification settings - Fork 110
/
errors.go
48 lines (39 loc) · 1.51 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
package utils
import (
"fmt"
"reflect"
"time"
"github.com/pkg/errors"
"go.viam.com/rdk/logging"
)
// NewRemoteResourceClashError is used when you are more than one resource with the same name exist.
func NewRemoteResourceClashError(name string) error {
return errors.Errorf("more than one remote resources with name %q exists", name)
}
// NewUnexpectedTypeError is used when there is a type mismatch.
func NewUnexpectedTypeError[ExpectedT any](actual interface{}) error {
return errors.Errorf("expected %s but got %T", TypeStr[ExpectedT](), actual)
}
// TypeStr returns the a human readable type string of the given value.
func TypeStr[T any]() string {
zero := new(T)
vT := reflect.TypeOf(zero).Elem()
return vT.String()
}
// NewBuildTimeoutError is used when a resource times out during construction or reconfiguration.
func NewBuildTimeoutError(name string) error {
timeout := GetResourceConfigurationTimeout(logging.Global())
id := fmt.Sprintf("resource %s", name)
timeoutMsg := "reconfigure"
return timeoutErrorHelper(id, timeout, timeoutMsg)
}
// NewModuleStartUpTimeoutError is used when a module times out during startup.
func NewModuleStartUpTimeoutError(name string) error {
timeout := GetModuleStartupTimeout(logging.Global())
id := fmt.Sprintf("module %s", name)
timeoutMsg := "startup"
return timeoutErrorHelper(id, timeout, timeoutMsg)
}
func timeoutErrorHelper(id string, timeout time.Duration, timeoutMsg string) error {
return fmt.Errorf("%s timed out after %v during %v", id, timeout, timeoutMsg)
}