-
Notifications
You must be signed in to change notification settings - Fork 3
/
errors.go
60 lines (49 loc) · 1.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
package rrd
import (
"errors"
"fmt"
"strings"
)
var (
// ErrNilOption is returned by NewClient if an option is nil.
ErrNilOption = errors.New("nil option")
)
// Error represents a error returned from the rrdcached server.
type Error struct {
Code int
Msg string
}
// NewError returns a new Error.
func NewError(code int, msg string) *Error {
return &Error{Code: code, Msg: msg}
}
func (e *Error) Error() string {
return fmt.Sprintf("%v (%v)", e.Msg, e.Code)
}
// IsExist returns true if err represents a failure due to a existing rrd, false otherwise.
func IsExist(err error) bool {
err2, ok := err.(*Error)
return ok && err2.Code == -1 && strings.Contains(err2.Msg, "File exists")
}
// IsNotExist returns true if err represents a failure due to a non-existing rrd, false otherwise.
func IsNotExist(err error) bool {
err2, ok := err.(*Error)
return ok && err2.Code == -1 && strings.HasPrefix(err2.Msg, "No such file:")
}
// IsIllegalUpdate returns true if err represents a failure due to an illegal update, false otherwise.
func IsIllegalUpdate(err error) bool {
err2, ok := err.(*Error)
return ok && err2.Code == -1 && strings.HasPrefix(err2.Msg, "illegal attempt to update using time")
}
// InvalidResponseError is the error returned when the response data was invalid.
type InvalidResponseError struct {
Reason string
Data []string
}
// NewInvalidResponseError returns a new InvalidResponseError from lines.
func NewInvalidResponseError(reason string, lines ...string) *InvalidResponseError {
return &InvalidResponseError{Reason: reason, Data: lines}
}
func (e *InvalidResponseError) Error() string {
return fmt.Sprintf("%v (%v)", e.Reason, strings.Join(e.Data, ", "))
}