Skip to content

Commit ece6e9a

Browse files
authored
Merge pull request #25 from lainio/new-optimizations
Zig error return trace & extended samle app
2 parents aa59314 + 7b66b1b commit ece6e9a

22 files changed

+518
-239
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
### Version history
44

5+
##### 1.1.0
6+
- `assert` package:
7+
- bug fix: call stack traversal during unit testing in some situations
8+
- **all generics-based functions are inline expansed**
9+
- *performance* is now *same as if-statements for all functions*
10+
- new assert functions: `MNil`, `CNil`, `Less`, `Greater`, etc.
11+
- all assert messages follow Go idiom: `got, want`
12+
- `Asserter` can be set per goroutine: `PushAsserter`
13+
- `try` package:
14+
- new check functions: `T`, `T1`, `T2`, `T3`, for quick refactoring from `To` functions to annotate an error locally
15+
- **all functions are inline expansed**: if-statement equal performance
16+
517
##### 1.0.0
618
- **Finally! We are very happy, and thanks to all who have helped!**
719
- Lots of documentation updates and cleanups for version 1.0.0

README.md

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ tolerant*. And, of course, it helps to make your code error-safe.
103103
handler.
104104
3. It helps us use design-by-contract type preconditions.
105105
4. It offers automatic stack tracing for every error, runtime error, or panic.
106-
If you are familiar with Zig, the `err2` error traces are same as Zig's.
106+
If you are familiar with Zig, the `err2` error return traces are same as
107+
Zig's.
107108

108109
You can use all of them or just the other. However, if you use `try` for error
109110
checks, you must remember to use Go's `recover()` by yourself, or your error
@@ -152,7 +153,11 @@ own purposes.
152153
153154
#### Error Stack Tracing
154155
155-
The err2 offers optional stack tracing. It's *automatic* and *optimized*.
156+
The err2 offers optional stack tracing in two different formats:
157+
1. Optimized call stacks (`-err2-trace`)
158+
1. Error return traces similar to Zig (`-err2-ret-trace`)
159+
160+
Both are *automatic* and fully *optimized*.
156161
157162
<details>
158163
<summary>The example of the optimized call stack:</summary>
@@ -177,12 +182,14 @@ main.main()
177182
178183
</details>
179184
180-
Just set the `err2.SetErrorTracer` or `err2.SetPanicTracer` to the stream you
181-
want traces to be written:
185+
Just set the `err2.SetErrorTracer`, `err2.SetErrRetTracer` or
186+
`err2.SetPanicTracer` to the stream you want traces to be written:
182187
183188
```go
184189
err2.SetErrorTracer(os.Stderr) // write error stack trace to stderr
185190
// or, for example:
191+
err2.SetErrRetTracer(os.Stderr) // write error return trace (like Zig)
192+
// or, for example:
186193
err2.SetPanicTracer(log.Writer()) // stack panic trace to std logger
187194
```
188195
@@ -382,12 +389,12 @@ func TestWebOfTrustInfo(t *testing.T) {
382389
// And if there's violations during the test run they are reported as
383390
// test failures for this TestWebOfTrustInfo -test.
384391

385-
assert.Equal(0, wot.CommonInvider)
386-
assert.Equal(1, wot.Hops)
392+
assert.Equal(wot.CommonInvider, 0)
393+
assert.Equal(wot.Hops, 1)
387394

388395
wot = NewWebOfTrust(bob.Node, carol.Node)
389-
assert.Equal(-1, wot.CommonInvider)
390-
assert.Equal(-1, wot.Hops)
396+
assert.Equal(wot.CommonInvider, hop.NotConnected)
397+
assert.Equal(wot.Hops, hop.NotConnected)
391398
...
392399
```
393400
@@ -592,6 +599,15 @@ been much easier.** There is an excellent [blog post](https://jesseduffield.com/
592599
about the issues you are facing with Go's error handling without the help of
593600
the err2 package.
594601
602+
- If you don't want to bubble up error from every function, we have learned that
603+
`Try` prefix convention is pretty cool way to solve limitations of Go
604+
programming language help to make your code more skimmable. If your internal
605+
functions normally would be something like `func CopyFile(s, t string) (err
606+
error)`, you can replace them with `func TryCopyFile(s, t string)`, where `Try`
607+
prefix remind you that the function throws errors. You can decide at what level
608+
of the call stack you will catch them with `err2.Handle` or `err2.Catch`,
609+
depending your case and API.
610+
595611
</details>
596612
597613
## Support And Contributions
@@ -607,14 +623,10 @@ Please see the full version history from [CHANGELOG](./CHANGELOG.md).
607623
608624
### Latest Release
609625
610-
##### 1.1.0
611-
- `assert` package:
612-
- bug fix: call stack traversal during unit testing in some situations
613-
- **all generics-based functions are inline expansed**
614-
- *performance* is now *same as if-statements for all functions*
615-
- new assert functions: `MNil`, `CNil`, `Less`, `Greater`, etc.
616-
- all assert messages follow Go idiom: `got, want`
617-
- `Asserter` can be set per goroutine: `PushAsserter`
618-
- `try` package:
619-
- new check functions: `T`, `T1`, `T2`, `T3`, for quick refactoring from `To` functions to annotate an error locally
620-
- **all functions are inline expansed**: if-statement equal performance
626+
##### 1.2.0
627+
- Now `-err2-ret-trace` and `err2.SetErrRetTracer` gives us *error return traces*
628+
which are even more readable than `-err2-trace`, `err2.SetErrorTracer` with
629+
long error return traces
630+
- A new automatic error formatter/generator added for `TryCopyFile` convention
631+
- New features for `sample/` to demonstrate latest features
632+
- Extended documentation

assert/assert.go

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,24 +1021,17 @@ func current() (curAsserter asserter) {
10211021
return curAsserter
10221022
}
10231023

1024-
// SetDefault sets the current default [Asserter] for assert pkg. It also
1025-
// returns the previous [Asserter].
1024+
// SetDefault sets the new default [Asserter] for the assert pkg instance you're
1025+
// currently using. It also returns the previous [Asserter]. The default
1026+
// asserter is [Production] that's best for most use cases and packages.
10261027
//
1027-
// Note that you should use this in TestMain function, and use [flag] package to
1028-
// set it for the app. For the tests you can set it to panic about every
1029-
// assertion fault, or to throw an error, or/and print the call stack
1030-
// immediately when assert occurs. The err2 package helps you to catch and
1031-
// report all types of the asserts.
1028+
// Note that for most cases [PushAsserter] is most suitable. [PushAsserter]
1029+
// allows you to set [Asserter] per goroutine or function, i.e., until you pop
1030+
// the asserter out, and the assert package uses the current default [Asserter]
1031+
// set by [SetDefault].
10321032
//
1033-
// Note that if you are using tracers you might get two call stacks, so test
1034-
// what's best for your case.
1035-
//
1036-
// Tip. If our own packages (client packages for assert) have lots of parallel
1037-
// testing and race detection, please try to use same [Asserter] for all of them
1038-
// and set [Asserter] only one in TestMain, or in init.
1039-
//
1040-
// func TestMain(m *testing.M) {
1041-
// SetDefault(assert.TestFull)
1033+
// Note that if you are using tracers you might get overlapping call stacks, so
1034+
// test what's best for your case.
10421035
func SetDefault(i Asserter) (old Asserter) {
10431036
// pkg lvl lock to allow only one pkg client call this at one of the time
10441037
// together with the indexing, i.e we don't need to switch asserter

doc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
Package err2 provides three main functionality:
3-
1. err2 package includes helper functions for error handling & automatic error
2+
Package err2 is error handling solution including three main functionality:
3+
1. err2 package offers helper functions for error handling & automatic error
44
stack tracing
55
2. [github.com/lainio/err2/try] sub-package is for error checking
66
3. [github.com/lainio/err2/assert] sub-package is for design-by-contract and

0 commit comments

Comments
 (0)