-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
88 lines (70 loc) · 2.4 KB
/
decode.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
package errdecode
// ClassifiedError describes the wrapped error value matched by the
// classification rule set.
type ClassifiedError interface {
error
// Code returns the classification for the matched error.
Code() int
// Unwrap returns the underlying error.
Unwrap() error
}
// Rule represents criteria for matching error values.
type Rule struct {
// Code is an identifier for a class of errors.
Code int
// Message describes the error class, e.g., a friendly explanation or
// a string identifier for key-based lookups.
Message string
// Errors are values that fall under this classification.
Errors []error
// Match is a func that returns true if a given error is a match.
// It can be used to check error types by using a closure.
Match MatcherFunc
}
// MatcherFunc describes an error matcher.
type MatcherFunc func(err error) (isMatch bool)
// Decoder wraps a set of error translation rules, on which it provides
// classication and translation of error values.
type Decoder struct {
encoder EncoderFunc
msgTranslator MessageTranslatorFunc
}
// EncoderFunc describes an error classifier, i.e., a function that converts
// an error value into a recognized code and message.
type EncoderFunc func(err error) (code int, message string)
// MessageTranslatorFunc describes further transformations for decoded errors.
type MessageTranslatorFunc func(decoded string) (translated string)
// New returns a configured error decoder.
func New(rs []Rule, options ...Option) *Decoder {
d := &Decoder{
encoder: newDefaultEncoder(rs),
msgTranslator: defaultMessageTranslator,
}
for _, option := range options {
option(d)
}
return d
}
// Translate decodes an error value into a configured encoded mapping.
// If the error cannot be classified, it is returned as-is.
func (d *Decoder) Translate(err error) error {
code, msg := d.encoder(err)
if code == 0 {
return err
}
return &matchedError{code, err, d.msgTranslator(msg)}
}
// Compile-time check.
var _ ClassifiedError = (*matchedError)(nil)
// Represents an error matched by the encoder.
type matchedError struct {
code int
err error
msg string
}
// Code satisfies ClassifiedError interface.
func (e *matchedError) Code() int { return e.code }
// Unwrap satisfies ClassifiedError interface.
func (e *matchedError) Unwrap() error { return e.err }
// Error satisties the error interface.
func (e *matchedError) Error() string { return e.msg }