-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelay.go
More file actions
47 lines (39 loc) · 1.09 KB
/
relay.go
File metadata and controls
47 lines (39 loc) · 1.09 KB
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
// Package relay provides a simple mechanism for relaying control flow based
// upon whether a checked error is nil or not.
package relay
// Relay tracks an error and a related error handler.
type Relay struct {
err error
h func(error)
}
// New constructs a new *Relay.
func New(handler ...func(error)) *Relay {
h := DefaultHandler()
if len(handler) > 0 {
h = handler[0]
}
return &Relay{
h: h,
}
}
// Check will do nothing if the error argument is nil. Otherwise, it kicks-off
// an event (i.e. the relay is "tripped") and should be handled by a deferred
// call to Handle().
func (r *Relay) Check(err error) {
if err == nil {
return
}
r.err = err
panic(r)
}
// CodedCheck will do nothing if the error argument is nil. Otherwise, it
// kicks-off an event (i.e. the relay is "tripped") and should be handled by a
// deferred call to Handle(). Any provided error will be wrapped in a
// codedError instance in order to trigger special behavior in the default
// error handler.
func (r *Relay) CodedCheck(code int, err error) {
if err == nil {
return
}
r.Check(&CodedError{err, code})
}