Skip to content

Commit ed379b0

Browse files
committed
Stdnull added
1 parent c18ffb4 commit ed379b0

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

err2.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ var (
3333
// same error. These error are mainly for that purpose.
3434
ErrNotRecoverable = errors.New("cannot recover")
3535
ErrRecoverable = errors.New("recoverable")
36+
37+
// Stdnull is helper variable for io.Writer need e.g. err2.SetLogTracer in
38+
// cases you don't want to use automatic log writer, i.e. LogTracer == nil.
39+
// It's usually used to change how the Catch works, e.g., in CLI apps.
40+
Stdnull = &nullDev{}
3641
)
3742

3843
// Handle is the general purpose error handling function. What makes it so
@@ -116,7 +121,11 @@ func Handle(err *error, a ...any) {
116121
//
117122
// The preceding line catches the errors and panics and prints an annotated
118123
// error message about the error source (from where the error was thrown) to the
119-
// currently set log.
124+
// currently set log. Note, when log stream isn't set, the standard log is used.
125+
// It can be bound to, e.g., glog. And if you want to suppress automatic logging
126+
// use the following setup:
127+
//
128+
// err2.SetLogTracer(err2.Stdnull)
120129
//
121130
// The next one stops errors and panics, but allows you handle errors, like
122131
// cleanups, etc. The error handler function has same signature as Handle's
@@ -228,6 +237,10 @@ func Err(f func(err error)) func(error) error {
228237
}
229238
}
230239

240+
type nullDev struct{}
241+
242+
func (nullDev) Write([]byte) (int, error) { return 0, nil }
243+
231244
func doTrace(err error) {
232245
if err == nil || err.Error() == "" {
233246
return

0 commit comments

Comments
 (0)