@@ -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
- try.Out1 (os.Remove (dst)).Logf (" cleaning error" )
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,12 +119,14 @@ 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
131
126
error-handling scenarios. And remember that you can have as many error handlers
132
- per function as you need.
127
+ per function as you need, as well as you can chain error handling functions per
128
+ ` err2.Handle ` that allows you to build new error handling middleware for your
129
+ own purposes.
133
130
134
131
#### Error Stack Tracing
135
132
@@ -248,7 +245,7 @@ notExist := try.Is(r2.err, plugin.ErrNotExist)
248
245
happens: nearest ` err2.Handle ` gets it first.
249
246
250
247
These ` try.Is ` functions help cleanup mesh idiomatic Go, i.e. mixing happy and
251
- error path, leads to.
248
+ error path, leads to.
252
249
253
250
For more information see the examples in the documentation of both functions.
254
251
@@ -328,7 +325,7 @@ func TestWebOfTrustInfo(t *testing.T) {
328
325
assert.SLen (common, 2 )
329
326
330
327
wot := dave.WebOfTrustInfo (eve.Node ) // <- this includes asserts as well!!
331
- // 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
332
329
// test failures for this TestWebOfTrustInfo -test.
333
330
334
331
assert.Equal (0 , wot.CommonInvider )
@@ -364,7 +361,7 @@ stack.**
364
361
When you are using ` err2` or ` assert` packages, i.e., just importing them, you
365
362
have an option to automatically support for err2 configuration flags through
366
363
Go's standard ` flag` package. See more information about err2 settings from
367
- [Error Stack Tracing](#error-stack-tracing) and [Asserters](#asserters).
364
+ [Error Stack Tracing](#error-stack-tracing) and [Asserters](#asserters).
368
365
369
366
Now you can always deploy your applications and services with the simple
370
367
end-user friendly error messages and no stack traces, **but you can switch them
@@ -421,10 +418,10 @@ support packages like `err2` and `glog` and their flags.
421
418
` ` ` go
422
419
PersistentPreRunE: func (cmd *cobra.Command , args []string ) (err error ) {
423
420
defer err2.Handle (&err)
424
-
421
+
425
422
// NOTE! Very important. Adds support for std flag pkg users: glog, err2
426
423
goflag.Parse ()
427
-
424
+
428
425
try.To (goflag.Set (" logtostderr" , " true" ))
429
426
handleViperFlags (cmd) // local helper with envs
430
427
glog.CopyStandardLogTo (" ERROR" ) // for err2
@@ -470,7 +467,7 @@ part of the algorithm itself.**
470
467
**there are no performance penalty at all**. However, the mandatory use of the
471
468
` defer ` might prevent some code optimisations like function inlining. And still,
472
469
we have cases where using the ` err2` and ` try` package simplify the algorithm so
473
- 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
474
471
benchmarks for ` io.Copy ` in the repo.**)
475
472
476
473
If you have a performance-critical use case, we always recommend you to write
@@ -527,14 +524,14 @@ Please see the full version history from [CHANGELOG](./CHANGELOG.md).
527
524
528
525
### Latest Release
529
526
530
- ##### 0.9.52
531
- - ` err2. Stderr ` helpers for ` Catch/Handle ` to direct auto-logging + snippets
532
- - ` assert ` package ` Shorter ` ` Longer ` helpers for automatic messages
533
- - ` asserter ` package remove deprecated slow reflection based funcs
534
- - cleanup and refactoring for sample apps
535
-
536
- ### Upcoming releases
537
-
538
- ##### 0.9.6
539
- - Continue removing unused parts and repairing for 1.0.0 release.
540
- - Always more and better documentation
527
+ ##### 1.0.0
528
+ - **Finally! We are very happy, and thanks to all who have helped!**
529
+ - Lots of documentation updates and cleanups for version 1.0.0
530
+ - ` Catch/Handle ` take unlimited amount error handler functions
531
+ - allows building e.g. error handling middlewares
532
+ - this is major feature because it allows building helpers/add-ons
533
+ - automatic outputs aren't overwritten by given args, only with ` assert. Plain `
534
+ - Minor API fixes to still simplify it:
535
+ - remove exported vars, obsolete types and funcs from ` assert ` pkg
536
+ - ` Result2. Def2 () ` sets only ` Val2 `
537
+ - technical refactorings: variadic function calls only in API level
0 commit comments