-
Notifications
You must be signed in to change notification settings - Fork 6
/
handlers.go
114 lines (103 loc) · 2.91 KB
/
handlers.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
106
107
108
109
110
111
112
113
114
package err2
import (
"fmt"
"os"
"github.com/lainio/err2/internal/handler"
)
// Stderr is a built-in helper to use with [Handle] and [Catch]. It prints the
// error to stderr and it resets the current error value. It's a handy [Catch]
// handler in main function.
//
// You can use it like this:
//
// func main() {
// defer err2.Catch(err2.Stderr)
func Stderr(err error) error {
if err == nil {
return nil
}
fmt.Fprintln(os.Stderr, err.Error())
return nil
}
// Stdout is a built-in helper to use with [Handle] and [Catch]. It prints the
// error to stdout and it resets the current error value. It's a handy [Catch]
// handler in main function.
//
// You can use it like this:
//
// func main() {
// defer err2.Catch(err2.Stdout)
func Stdout(err error) error {
if err == nil {
return nil
}
fmt.Fprintln(os.Stdout, err.Error())
return nil
}
// Noop is a built-in helper to use with [Handle] and [Catch]. It keeps the current
// error value the same. You can use it like this:
//
// defer err2.Handle(&err, err2.Noop)
func Noop(err error) error { return err }
// Reset is a built-in helper to use with [Handle] and [Catch]. It sets the current
// error value to nil. You can use it like this to reset the error:
//
// defer err2.Handle(&err, err2.Reset)
func Reset(error) error { return nil }
// Err is a built-in helper to use with [Handle] and [Catch]. It offers simplifier
// for error handling function for cases where you don't need to change the
// current error value. For instance, if you want to just write error to stdout,
// and don't want to use [SetLogTracer] and keep it to write to your logs.
//
// defer err2.Catch(err2.Err(func(err error) {
// fmt.Println("ERROR:", err)
// }))
//
// Note that since Err helper we have other helpers like [Stdout] that allows
// previous block be written as simple as:
//
// defer err2.Catch(err2.Stdout)
func Err(f func(err error)) Handler {
return func(err error) error {
f(err)
return err
}
}
const lvl = 10
// Log is a built-in helper to use with [Handle] and [Catch]. Log prints error
// string to the current log that is set by [SetLogTracer].
func Log(err error) error {
if err == nil {
return nil
}
_ = handler.LogOutput(lvl, err.Error())
return err
}
// StderrNoReset is a built-in helper to use with [Handle] and [Catch]. It prints
// the error to stderr. If you need to reset err value use Stderr instead.
//
// You can use it like this:
//
// func myFunction() {
// defer err2.Handle(err2.Noop, err2.StderrNoReset)
func StderrNoReset(err error) error {
if err == nil {
return nil
}
fmt.Fprintln(os.Stderr, err.Error())
return err
}
// StdoutNoReset is a built-in helper to use with [Handle] and [Catch]. It prints
// the error to stdout.
//
// You can use it like this:
//
// func main() {
// defer err2.Catch(err2.StdoutNoReset)
func StdoutNoReset(err error) error {
if err == nil {
return nil
}
fmt.Fprintln(os.Stdout, err.Error())
return err
}