-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
95 lines (83 loc) · 2.88 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
88
89
90
91
92
93
94
95
package awn
import (
"errors"
"fmt"
)
type errorType int
const (
_ errorType = iota // so we don't start at 0
errContextTimeoutExceeded
errMalformedDate
errRegexFailed
errAPIKeyMissing
errAppKeyMissing
errInvalidDateFormat
errMacAddressMissing
)
var (
ErrContextTimeoutExceeded = ClientError{kind: errContextTimeoutExceeded} //nolint:exhaustruct
ErrMalformedDate = ClientError{kind: errMalformedDate} //nolint:exhaustruct
ErrRegexFailed = ClientError{kind: errRegexFailed} //nolint:exhaustruct
ErrAPIKeyMissing = ClientError{kind: errAPIKeyMissing} //nolint:exhaustruct
ErrAppKeyMissing = ClientError{kind: errAppKeyMissing} //nolint:exhaustruct
ErrInvalidDateFormat = ClientError{kind: errInvalidDateFormat} //nolint:exhaustruct
ErrMacAddressMissing = ClientError{kind: errMacAddressMissing} //nolint:exhaustruct
)
// ClientError is a public custom error type that is used to return errors from the client.
type ClientError struct {
kind errorType // errKind in example
value int
err error
}
// Error is a public function that returns the error message.
func (c ClientError) Error() string {
switch c.kind {
case errContextTimeoutExceeded:
return fmt.Sprintf("context timeout exceeded: %v", c.value)
case errMalformedDate:
return fmt.Sprintf("date format is malformed. should be YYYY-MM-DD: %v", c.value)
case errRegexFailed:
return fmt.Sprintf("regex failed: %v", c.value)
case errAPIKeyMissing:
return fmt.Sprintf("api key is missing: %v", c.value)
case errAppKeyMissing:
return fmt.Sprintf("application key is missing: %v", c.value)
case errInvalidDateFormat:
return fmt.Sprintf("date is invalid. It should be in epoch time in milliseconds: %v", c.value)
case errMacAddressMissing:
return fmt.Sprintf("mac address is missing: %v", c.value)
default:
return fmt.Sprintf("unknown error: %v", c.value)
}
}
// from is a private function that returns an error with a particular location and the
// underlying error.
func (c ClientError) from(pos int, err error) ClientError { //nolint:unused
ce := c
ce.value = pos
ce.err = err
return ce
}
// with is a private function that returns an error with a particular value.
func (c ClientError) with(val int) ClientError { //nolint:unused
ce := c
ce.value = val
return ce
}
// Is is a public function that reports whether any error in the error's chain matches target.
func (c ClientError) Is(err error) bool {
var clientError ClientError
ok := errors.As(err, &clientError) // reflection
if !ok {
return false
}
return clientError.kind == c.kind
}
// Unwrap is a public function that returns the underlying error by unwrapping it.
func (c ClientError) Unwrap() error {
return c.err
}
// Wrap is a public function that allows for errors to be propagated up correctly.
func (c ClientError) Wrap() error {
return fmt.Errorf("error: %w", c)
}