@@ -10,24 +10,19 @@ and propagation** like other modern programming languages: **Zig**, Rust, Swift,
10
10
etc. ` err2 ` isn't an exception handling library, but an entirely orthogonal
11
11
package with Go's existing error handling mechanism.
12
12
13
- ``` go
13
+ ``` go
14
14
func CopyFile (src , dst string ) (err error ) {
15
15
defer err2.Handle (&err)
16
16
17
- assert.NotEmpty (src)
18
- assert.NotEmpty (dst)
19
-
20
17
r := try.To1 (os.Open (src))
21
18
defer r.Close ()
22
19
23
- w , err := os.Create (dst)
24
- if err != nil {
25
- return fmt.Errorf (" mixing traditional error checking: % w" , err)
26
- }
20
+ w := try.To1 (os.Create (dst))
27
21
defer err2.Handle (&err, err2.Err (func (error ) {
28
22
try.Out (os.Remove (dst)).Logf (" cleaning error" )
29
23
}))
30
24
defer w.Close ()
25
+
31
26
try.To1 (io.Copy (w, r))
32
27
return nil
33
28
}
@@ -88,7 +83,7 @@ little error handling. But most importantly, it doesn't help developers with
88
83
> resilience. -- Gregor Hohpe
89
84
90
85
Automatic error propagation is crucial because it makes your code change
91
- tolerant. And, of course, it helps to make your code error-safe:
86
+ tolerant. And, of course, it helps to make your code error-safe:
92
87
93
88
![ Never send a human to do a machine's job] ( https://www.magicalquote.com/wp-content/uploads/2013/10/Never-send-a-human-to-do-a-machines-job.jpg )
94
89
@@ -99,7 +94,7 @@ The err2 package is your automation buddy:
99
94
line exactly similar as
100
95
[ Zig's ` errdefer ` ] ( https://ziglang.org/documentation/master/#errdefer ) .
101
96
2 . It helps to check and transport errors to the nearest (the defer-stack) error
102
- handler.
97
+ handler.
103
98
3 . It helps us use design-by-contract type preconditions.
104
99
4 . It offers automatic stack tracing for every error, runtime error, or panic.
105
100
If you are familiar with Zig, the ` err2 ` error traces are same as Zig's.
@@ -124,7 +119,7 @@ This is the simplest form of `err2` automatic error handler:
124
119
``` go
125
120
func doSomething () (err error ) {
126
121
// below: if err != nil { return ftm.Errorf("%s: %w", CUR_FUNC_NAME, err) }
127
- defer err2.Handle (&err)
122
+ defer err2.Handle (&err)
128
123
` ` `
129
124
130
125
See more information from ` err2.Handle ` 's documentation. It supports several
@@ -250,7 +245,7 @@ notExist := try.Is(r2.err, plugin.ErrNotExist)
250
245
happens: nearest ` err2.Handle ` gets it first.
251
246
252
247
These ` try.Is ` functions help cleanup mesh idiomatic Go, i.e. mixing happy and
253
- error path, leads to.
248
+ error path, leads to.
254
249
255
250
For more information see the examples in the documentation of both functions.
256
251
@@ -330,7 +325,7 @@ func TestWebOfTrustInfo(t *testing.T) {
330
325
assert.SLen (common, 2 )
331
326
332
327
wot := dave.WebOfTrustInfo (eve.Node ) // <- this includes asserts as well!!
333
- // And if there's violations during the test run they are reported as
328
+ // And if there's violations during the test run they are reported as
334
329
// test failures for this TestWebOfTrustInfo -test.
335
330
336
331
assert.Equal (0 , wot.CommonInvider )
@@ -366,7 +361,7 @@ stack.**
366
361
When you are using ` err2` or ` assert` packages, i.e., just importing them, you
367
362
have an option to automatically support for err2 configuration flags through
368
363
Go's standard ` flag` package. See more information about err2 settings from
369
- [Error Stack Tracing](#error-stack-tracing) and [Asserters](#asserters).
364
+ [Error Stack Tracing](#error-stack-tracing) and [Asserters](#asserters).
370
365
371
366
Now you can always deploy your applications and services with the simple
372
367
end-user friendly error messages and no stack traces, **but you can switch them
@@ -423,10 +418,10 @@ support packages like `err2` and `glog` and their flags.
423
418
` ` ` go
424
419
PersistentPreRunE: func (cmd *cobra.Command , args []string ) (err error ) {
425
420
defer err2.Handle (&err)
426
-
421
+
427
422
// NOTE! Very important. Adds support for std flag pkg users: glog, err2
428
423
goflag.Parse ()
429
-
424
+
430
425
try.To (goflag.Set (" logtostderr" , " true" ))
431
426
handleViperFlags (cmd) // local helper with envs
432
427
glog.CopyStandardLogTo (" ERROR" ) // for err2
@@ -472,7 +467,7 @@ part of the algorithm itself.**
472
467
**there are no performance penalty at all**. However, the mandatory use of the
473
468
` defer ` might prevent some code optimisations like function inlining. And still,
474
469
we have cases where using the ` err2` and ` try` package simplify the algorithm so
475
- that it's faster than the return value if err != nil version. (**See the
470
+ that it's faster than the return value if err != nil version. (**See the
476
471
benchmarks for ` io.Copy ` in the repo.**)
477
472
478
473
If you have a performance-critical use case, we always recommend you to write
@@ -536,7 +531,7 @@ Please see the full version history from [CHANGELOG](./CHANGELOG.md).
536
531
- allows building e.g. error handling middlewares
537
532
- this is major feature because it allows building helpers/add-ons
538
533
- automatic outputs aren't overwritten by given args, only with ` assert.Plain `
539
- - Minor API fixes to still simplify it:
534
+ - Minor API fixes to still simplify it:
540
535
- remove exported vars, obsolete types and funcs from ` assert` pkg
541
536
- ` Result2.Def2 ()` sets only Val2
542
537
- technical refactorings: variadic function calls only in API level
0 commit comments