forked from lainio/err2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc.go
70 lines (53 loc) · 2.08 KB
/
doc.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
/*
Package try provides two main functionality:
1. handlers for error recovery and handling
2. checks for returning non-nil errors
The traditional error handling idiom in Go is roughly akin to
if err != nil { return err }
The try package drives programmers to focus more on error handling rather than
checking errors. We think that checks should be so easy that we never forget
them. The CopyFile example shows how it works:
// CopyFile copies source file to the given destination. If any error occurs it
// returns error value describing the reason.
func CopyFile(src, dst string) (err error) {
// Add first error handler just to annotate the error properly.
defer try.Handlef(&err, "copy %s %s", src, dst)
// Try to open the file. If error occurs now, err will be annotated and
// returned properly thanks to above try.Check
r, err := os.Open(src)
try.Check(err)
defer r.Close()
// Try to create a file. If error occurs now, err will be annotated and
// returned properly.
w, err := os.Create(dst)
try.Try(err, try.Cleanup(func() {
os.Remove(dst)
})
defer w.Close()
// Try to copy the file. If error occurs now, all previous error handlers
// will be called in the reversed order. And final return error is
// properly annotated in all the cases.
_, err = io.Copy(w, r)
try.Check(err)
// All OK, just return nil.
return nil
}
# Error checks
The try package provides convenient helpers to check the errors. For example,
instead of
b, err := ioutil.ReadAll(r)
if err != nil {
return err
}
we can write
b, err := ioutil.ReadAll(r)
try.Check(err)
# Stack Tracing
By default, try.Try and try.Check will wrap the error so that it has a stack trace
This can be disabled by setting the `AddStackTrace = false`
# Error handling
The beginning of every function should contain an `try.Handle*` to ensure that
errors are caught. Otherwise errors will escape the function as a panic and you
will be relying on calling functions to properly recover from panics.
*/
package try