-
Notifications
You must be signed in to change notification settings - Fork 0
/
errs.go
114 lines (102 loc) · 2.91 KB
/
errs.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
// Copyright (c) 2015, Ben Morgan. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
// Package errs provides error handlers for functions.
//
// The purpose of the errs (short for error handler) package is to
// provide an error handler base for functions that might encounter
// errors which they do not know what to do with.
//
// For example, there are many valid ways to respond to an error
// when processing a directory of files. You might want to ignore
// them, print them, handle them specially depending on the error.
//
// When writing a function that takes an error handler, allow the
// user to set a default handler and then pass in nil to specify
// that the default is desired:
//
// func ACME(h errs.Handler, args ...interface{}) error {
// errs.Init(&h)
// ...
// if err = h(err); err != nil {
// return err
// }
// ...
// return nil
// }
//
// Additionally, the MultipleErrors type is provided to wrap multiple
// errors, in case no error handler is desired.
package errs
import (
"fmt"
"io"
"os"
)
// Handler is used by many functions to deal with errors, most of
// which will be nil errors.
//
// There are several Handlers already available for use.
// Most functions expect that you return nil. Program functionality
// may be impaired otherwise.
type Handler func(error) error
// Default is the default Handler that should be used when nil
// is supplied, see AssertHandler.
var Default Handler = Print(os.Stderr)
// Init is called by functions that take an Handler to
// make sure that an Handler is in fact available. It is usually
// valid to provide functions that expect an Handler a nil, because
// this function replaces that value with the default Handler,
// which you can set yourself.
func Init(h *Handler) {
if *h == nil {
*h = Default
}
}
// Quit returns whatever error it receives, which causes functions
// to quit whenever a non-nil error is received. If in doubt, do not
// use this.
func Quit(err error) error {
return err
}
// Ignore simply ignores all errors. Not recommended.
func Ignore(_ error) error {
return nil
}
// Print prints any errors to the writer supplied here. The
// format it uses is:
//
// error: %s.\n
func Print(w io.Writer) Handler {
if w == nil {
return Ignore
}
return func(err error) error {
if err != nil {
fmt.Fprintf(w, "error: %s.\n", err)
}
return nil
}
}
// List is a list of errors, and is used by Bundle.
type List []error
// Bundle bundles all received errors into an ErrorList, which you
// have to supply (as el).
func Bundle(el *List) Handler {
return func(err error) error {
if err != nil {
*el = append(*el, err)
}
return nil
}
}
// Channel sends all received errors that are not nil to the
// supplied channel.
func Channel(ch chan<- error) Handler {
return func(err error) error {
if err != nil {
ch <- err
}
return nil
}
}