...
1
16
17 package volume
18
19 import (
20 "fmt"
21 )
22
23 const (
24
25 ErrCodeNotSupported int = iota + 1
26 ErrCodeNoPathDefined
27 ErrCodeFsInfoFailed
28 )
29
30
31 func NewNotSupportedError() *MetricsError {
32 return &MetricsError{
33 Code: ErrCodeNotSupported,
34 Msg: "metrics are not supported for MetricsNil Volumes",
35 }
36 }
37
38
39 func NewNotImplementedError(reason string) *MetricsError {
40 return &MetricsError{
41 Code: ErrCodeNotSupported,
42 Msg: fmt.Sprintf("metrics support is not implemented: %s", reason),
43 }
44 }
45
46
47
48 func NewNotSupportedErrorWithDriverName(name string) *MetricsError {
49 return &MetricsError{
50 Code: ErrCodeNotSupported,
51 Msg: fmt.Sprintf("metrics are not supported for %s volumes", name),
52 }
53 }
54
55
56 func NewNoPathDefinedError() *MetricsError {
57 return &MetricsError{
58 Code: ErrCodeNoPathDefined,
59 Msg: "no path defined for disk usage metrics.",
60 }
61 }
62
63
64 func NewFsInfoFailedError(err error) *MetricsError {
65 return &MetricsError{
66 Code: ErrCodeFsInfoFailed,
67 Msg: fmt.Sprintf("failed to get FsInfo due to error %v", err),
68 }
69 }
70
71
72 type MetricsError struct {
73 Code int
74 Msg string
75 }
76
77 func (e *MetricsError) Error() string {
78 return e.Msg
79 }
80
81
82 func IsNotSupported(err error) bool {
83 return isErrCode(err, ErrCodeNotSupported)
84 }
85
86 func isErrCode(err error, code int) bool {
87 if err == nil {
88 return false
89 }
90 if e, ok := err.(*MetricsError); ok {
91 return e.Code == code
92 }
93 return false
94 }
95
View as plain text