2
2
Package err2 provides three main functionality:
3
3
1. err2 package includes helper functions for error handling & automatic error
4
4
stack tracing
5
- 2. try package is for error checking
6
- 3. assert package is for design-by-contract and preconditions both for normal
7
- runtime and for testing
8
-
9
- The traditional error handling idiom in Go is roughly akin to
10
-
11
- if err != nil { return err }
12
-
13
- which applied recursively.
5
+ 2. [github.com/lainio/err2/try] sub-package is for error checking
6
+ 3. [github.com/lainio/err2/assert] sub-package is for design-by-contract and
7
+ preconditions both for normal runtime and for unit testing
14
8
15
9
The err2 package drives programmers to focus on error handling rather than
16
10
checking errors. We think that checks should be so easy that we never forget
@@ -19,7 +13,7 @@ them. The CopyFile example shows how it works:
19
13
// CopyFile copies source file to the given destination. If any error occurs it
20
14
// returns error value describing the reason.
21
15
func CopyFile(src, dst string) (err error) {
22
- // Add first error handler just to annotate the error properly.
16
+ // Add first error handler is to catch and annotate the error properly.
23
17
defer err2.Handle(&err)
24
18
25
19
// Try to open the file. If error occurs now, err will be
@@ -32,16 +26,18 @@ them. The CopyFile example shows how it works:
32
26
// Try to create a file. If error occurs now, err will be annotated and
33
27
// returned properly.
34
28
w := try.To1(os.Create(dst))
35
- // Add error handler to clean up the destination file. Place it here that
36
- // the next deferred close is called before our Remove call.
29
+ // Add error handler to clean up the destination file in case of
30
+ // error. Handler fn is called only if there has been an error at the
31
+ // following try.To check. We place it here that the next deferred
32
+ // close is called before our Remove a file call.
37
33
defer err2.Handle(&err, err2.Err(func(error) {
38
34
os.Remove(dst)
39
35
}))
40
36
defer w.Close()
41
37
42
38
// Try to copy the file. If error occurs now, all previous error handlers
43
- // will be called in the reversed order. And final return error is
44
- // properly annotated in all the cases.
39
+ // will be called in the reversed order. And a final error value is
40
+ // properly annotated and returned in all the cases.
45
41
try.To1(io.Copy(w, r))
46
42
47
43
// All OK, just return nil.
@@ -62,8 +58,9 @@ we can write
62
58
63
59
b := try.To1(io.ReadAll(r))
64
60
65
- Note that [try.To] functions are as fast as if err != nil statements. Please see
66
- the try package documentation for more information about the error checks.
61
+ Note that try.To functions are as fast as if err != nil statements. Please see
62
+ the [github.com/lainio/err2/try] package documentation for more information
63
+ about the error checks.
67
64
68
65
# Automatic Stack Tracing
69
66
0 commit comments