-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherr.go
105 lines (93 loc) · 2.43 KB
/
err.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
96
97
98
99
100
101
102
103
104
105
// Copyright 2016 Andreas Pannewitz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package do
// ===========================================================================
// Err represents some action
// which might go wrong (for some reason).
//
// The null value is useful: its Do() never returns a non-nil error.
type Err func() error
// Do evaluates Err iff Err is not nil,
// and makes Err an Errer.
func (a Err) Do() error {
if a != nil {
return a()
}
return nil
}
// ===========================================================================
// ErrJoin returns a closure around given fs.
//
// Iff there are no fs, nil is returned, and
// iff there is only one fs, this single fs is returned.
//
// Evaluate the returned function
// by invoking its Do() or
// by invoking it directly, iff not nil.
//
// Note: Order matters - evaluation terminates on first exceptional (non-default) result.
func ErrJoin(fs ...Err) Err {
switch len(fs) {
case 0:
return nil
case 1:
return fs[0]
default:
return func() error {
var err error
for _, f := range fs {
err = f.Do()
if err != nil {
return err
}
}
return nil
}
}
}
// ===========================================================================
// Set sets all errs as the new err
// when the returned Option is applied.
func (err *Err) Set(errs ...Err) Option {
return func(any interface{}) Opt {
prev := *err
*err = ErrJoin(errs...)
return func() Opt {
return (*err).Set(prev)(any)
}
}
}
// Add appends all errs to the existing err
// when the returned Option is applied.
func (err *Err) Add(errs ...Err) Option {
if err == nil || *err == nil {
return (*err).Set(errs...)
}
return func(any interface{}) Opt {
prev := *err
*err = ErrJoin(append([]Err{prev}, errs...)...)
return func() Opt {
return (*err).Set(prev)(any)
}
}
}
// ===========================================================================
// ErrIt returns an Err function
// which Do()es the Join of the given its
// and returns the default, namely: `nil`,
// upon evaluation.
//
// Evaluate the returned function
// by invoking it's Do() or
// by invoking it directly, iff not nil.
//
// ErrIt is a convenient wrapper.
func ErrIt(its ...It) Err {
return func() error {
it := ItJoin(its...)
(&it).Do()
return nil
}
}
// ===========================================================================