forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reason.go
54 lines (42 loc) · 1003 Bytes
/
reason.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
package reason
import "github.com/elastic/beats/libbeat/common"
type Reason interface {
error
Type() string
}
type ValidateError struct {
err error
}
type IOError struct {
err error
}
func ValidateFailed(err error) Reason {
if err == nil {
return nil
}
return ValidateError{err}
}
func IOFailed(err error) Reason {
if err == nil {
return nil
}
return IOError{err}
}
func (e ValidateError) Error() string { return e.err.Error() }
func (ValidateError) Type() string { return "validate" }
func (e IOError) Error() string { return e.err.Error() }
func (IOError) Type() string { return "io" }
func FailError(typ string, err error) common.MapStr {
return common.MapStr{
"type": typ,
"message": err.Error(),
}
}
func Fail(r Reason) common.MapStr {
return common.MapStr{
"type": r.Type(),
"message": r.Error(),
}
}
func FailIO(err error) common.MapStr { return Fail(IOError{err}) }
func FailValidate(err error) common.MapStr { return Fail(ValidateError{err}) }