Skip to content

Commit be238e6

Browse files
committed
add test/example for Catch fmt & bug fix
1 parent de3c035 commit be238e6

File tree

6 files changed

+44
-19
lines changed

6 files changed

+44
-19
lines changed

README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func CopyFile(src, dst string) (err error) {
2525
return fmt.Errorf("mixing traditional error checking: %w", err)
2626
}
2727
defer err2.Handle(&err, err2.Err(func(error) {
28-
os.Remove(dst)
28+
try.Out1(os.Remove(dst)).Logf("cleaning error")
2929
}))
3030
defer w.Close()
3131
try.To1(io.Copy(w, r))
@@ -430,30 +430,34 @@ Please see the full version history from [CHANGELOG](./CHANGELOG.md).
430430
### Latest Release
431431
432432
##### 0.9.40
433-
- Huge performance boost for: `defer err2.Handle/Catch()`
433+
- Significant performance boost for: `defer err2.Handle/Catch()`
434434
- **3x faster happy path than the previous version, which is now equal to
435-
simplest `defer` function in the err-returning function** . (Please see the
436-
`defer` benchmarks in the `err2_test.go` and run `make bench_reca`)
435+
simplest `defer` function in the `err`-returning function** . (Please see
436+
the `defer` benchmarks in the `err2_test.go` and run `make bench_reca`)
437437
- the solution caused a change to API, where the core reason is Go's
438438
optimization "bug". (We don't have confirmation yet)
439439
- Changed API for deferred error handling: `defer err2.Handle/Catch()`
440-
- deprecated:
440+
- Deprecated:
441441
```go
442-
defer err2.Handle(&err, func() { // THE OLD, deprecated API
443-
defer err2.Handle(&err, func(error) error { // The New CURRENT API
442+
defer err2.Handle(&err, func() { // <- relaying closure to access err val
444443
```
445-
- added:
444+
- Current version:
445+
```go
446+
defer err2.Handle(&err, func(error) error { // <- err val goes thru
447+
```
448+
- Added a new API:
446449
```go
447450
defer err2.Handle(&err, func(noerr bool) {
448451
assert.That(noerr) // noerr is always true!!
449452
doSomething()
450453
})
451454
```
455+
- Bug fixes: `ResultX.Logf()` now works as it should
452456
- More documentation
453457
454458
### Upcoming releases
455459
456460
##### 0.9.5
457-
- Go's standard lib's flag pkg integration (similar to `glog`)
461+
- Idea: Go's standard lib's flag pkg integration (similar to `glog`)
458462
- Continue removing unused parts from `assert` pkg
459463
- More documentation, repairing for some sort of marketing

err2_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,21 @@ func TestSetErrorTracer(t *testing.T) {
398398
test.Require(t, w == nil, "error tracer should be nil")
399399
}
400400

401+
func ExampleCatch_withFmt() {
402+
// Set default logger to stdout for this example
403+
oldLogW := err2.LogTracer()
404+
err2.SetLogTracer(os.Stdout)
405+
defer err2.SetLogTracer(oldLogW)
406+
407+
transport := func() {
408+
// See how Catch follows given format string similarly as Handle
409+
defer err2.Catch("catch")
410+
err2.Throwf("our error")
411+
}
412+
transport()
413+
// Output: catch: our error
414+
}
415+
401416
func ExampleHandle() {
402417
var err error
403418
defer err2.Handle(&err)

internal/handler/handler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ func (i *Info) workError() (err error) {
138138

139139
func (i *Info) fmtErr() {
140140
*i.Err = fmt.Errorf(i.Format+i.wrapStr(), append(i.Args, i.werr)...)
141+
i.werr = *i.Err // remember change both our errors!
141142
}
142143

143144
func (i *Info) buildFmtErr() {

samples/main-db-sample.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ import (
99
"github.com/lainio/err2/try"
1010
)
1111

12-
// TODO: test migrations with these samples!
13-
1412
func (db *Database) MoneyTransfer(from, to *Account, amount int) (err error) {
1513
defer err2.Handle(&err)
1614

1715
tx := try.To1(db.BeginTransaction())
18-
defer err2.Handle(&err, func() {
16+
defer err2.Handle(&err, func(err error) error {
1917
if errRoll := tx.Rollback(); errRoll != nil {
2018
// with go 1.20: err = fmt.Errorf("%w: ROLLBACK ERROR: %w", err, errRoll)
2119
err = fmt.Errorf("%v: ROLLBACK ERROR: %w", err, errRoll)
2220
}
21+
return err
2322
})
2423

2524
try.To(from.ReserveBalance(tx, amount))
2625

27-
defer err2.Handle(&err, func() { // optional, following sample's wording
26+
defer err2.Handle(&err, func(err error) error { // optional, following sample's wording
2827
err = fmt.Errorf("cannot %w", err)
28+
return err
2929
})
3030

3131
try.To(from.Withdraw(tx, amount))

samples/main-play.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ func CopyFile(src, dst string) (err error) {
2727
defer r.Close()
2828

2929
w := try.To1(os.Create(dst))
30-
defer err2.Handle(&err, func() {
30+
defer err2.Handle(&err, func(err error) error {
3131
try.Out(os.Remove(dst)).Logf()
32+
return err
3233
})
3334
defer w.Close()
3435

@@ -55,8 +56,9 @@ func OrgCopyFile(src, dst string) (err error) {
5556
if err != nil {
5657
return fmt.Errorf("mixing traditional error checking: %w", err)
5758
}
58-
defer err2.Handle(&err, func() {
59+
defer err2.Handle(&err, func(err error) error {
5960
try.Out(os.Remove(dst)).Logf("cleaning error")
61+
return err
6062
})
6163
defer w.Close()
6264
try.To1(io.Copy(w, r))

scripts/README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ functions or make something obsolete.
1111

1212
### `err2.Catch(func() {})` -> `err2.Catch(func(error) error {})` and others in v0.9.40
1313

14-
The version 0.9.40 has major update because of the performance. We have managed
15-
to eliminate `defer` slowdown. Our benchmarks are 3x faster and about equal to
16-
those function call stacks (100 level) that don't use `defer`. And because
17-
`try.To` is already as fast as `if err != nil` we reached our goal in speedwise!
14+
The version 0.9.40 is a major update because of the performance. We have managed
15+
to eliminate `defer` slowdown. Our benchmarks are 3x faster than previous
16+
version and about equal to those function call stacks (100 level) that don't use
17+
`defer`. We are exactly at the same level of performance with those functions
18+
that use deferred function that accept an argument. Transporting an argument to
19+
deferred function seems to be slower than functions that don't. And because
20+
`try.To` is already as fast as `if err != nil` we reached our goal for speed!
1821

1922
Because all of the error handler function signatures are now:
2023
```go

0 commit comments

Comments
 (0)