Skip to content

Commit bfd8dc5

Browse files
committed
continuing go doc format fixes, we are almost there
1 parent 7a1a1ed commit bfd8dc5

File tree

6 files changed

+182
-56
lines changed

6 files changed

+182
-56
lines changed

assert/assert.go

Lines changed: 136 additions & 25 deletions
Large diffs are not rendered by default.

assert/doc.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Package assert includes runtime assertion helpers both for normal execution as
33
well as a assertion package for Go's testing. What makes solution unique is its
44
capable to support both modes with the same API. Only thing you need to do is to
5-
add a PushTester line at the beginning of your unit tests:
5+
add a [PushTester] line at the beginning of your unit tests:
66
77
func TestInvite(t *testing.T) {
88
defer assert.PushTester(t)() // push testing variable t beginning of any test
@@ -59,8 +59,8 @@ Please see the code examples for more information.
5959
6060
# Flag Package Support
6161
62-
The assert package supports Go's flags. All you need to do is to call flag.Parse.
63-
And the following flags are supported (="default-value"):
62+
The assert package supports Go's flags. All you need to do is to call
63+
[flag.Parse]. And the following flags are supported (="default-value"):
6464
6565
-asserter="Prod"
6666
A name of the asserter Plain, Prod, Dev, Debug
@@ -70,15 +70,15 @@ And assert package's configuration flags are inserted.
7070
7171
# Performance
7272
73-
assert.That's performance is equal to the if-statement thanks for inlining. And
73+
[assert.That]'s performance is equal to the if-statement thanks for inlining. And
7474
the most of the generics-based versions are about the equally fast. Practice has
75-
thought that we should prefer other than assert.That because by using detailed
75+
thought that we should prefer other than [assert.That] because by using detailed
7676
version like [assert.Shorter] we get precise error messages automatically. Some
7777
also prefer readability of specific asserters.
7878
7979
If your algorithm is performance-critical please run `make bench` in the err2
8080
repo and decide case by case. Also you can make an issue or even PR if you would
81-
like to have something similar like glog.V() function.
81+
like to have something similar like [glog.V] function.
8282
8383
# Naming
8484

doc.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ them. The CopyFile example shows how it works:
3131
// following try.To check. We place it here that the next deferred
3232
// close is called before our Remove a file call.
3333
defer err2.Handle(&err, err2.Err(func(error) {
34-
os.Remove(dst)
34+
try.Out(os.Remove(dst)).Logf("cleanup failed")
3535
}))
3636
defer w.Close()
3737
@@ -75,12 +75,23 @@ programmatically (before [flag.Parse] if you are using that):
7575
or
7676
err2.SetPanicTracer(log.Writer()) // panic stack trace to std logger
7777
78-
Note. Since [Catch]'s default mode is to catch panics, the panic tracer's
79-
default values is os.Stderr. The default error tracer is nil.
78+
Note. Since [Catch]'s default mode is to recover from panics, it's a good
79+
practice still print their stack trace. The panic tracer's default values is
80+
[os.Stderr]. The default error tracer is nil.
8081
8182
err2.SetPanicTracer(os.Stderr) // panic stack tracer's default is stderr
8283
err2.SetErrorTracer(nil) // error stack tracer's default is nil
8384
85+
Note, that both panic and error traces are optimized by err2 package. That means
86+
that the head of the stack trace isn't the panic function, but an actual line
87+
that caused it. It works for all three categories:
88+
- normal error values
89+
- [runtime.Error] values
90+
- any types of the panics
91+
92+
The last two types are handled as panics in the error handling functions given
93+
to [Handle] and [Catch].
94+
8495
# Automatic Logging
8596
8697
Same err2 capablities support automatic logging like the [Catch] and

err2.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ type (
1414
Handler = handler.ErrorFn
1515
)
1616

17-
// Sentinel error value helpers. They are convenient thanks to [try.IsNotFound]
18-
// and similar functions.
17+
// Sentinel error value helpers. They are convenient thanks to
18+
// [github.com/lainio/err2/try.IsNotFound] and similar functions.
1919
//
2020
// [ErrNotFound] ... [ErrNotEnabled] are similar no-error like [io.EOF] for
2121
// those who really want to use error return values to transport non errors.
@@ -38,7 +38,7 @@ var (
3838
)
3939

4040
// Stdnull implements [io.Writer] that writes nothing, e.g.,
41-
// [SetLogTracer] in cases you don't want to use automatic log writer,
41+
// [SetLogTracer] in cases you don't want to use automatic log writer (=nil),
4242
// i.e., [LogTracer] == /dev/null. It can be used to change how the [Catch]
4343
// works, e.g., in CLI apps.
4444
var Stdnull = &nullDev{}
@@ -145,7 +145,7 @@ func Handle(err *error, a ...any) {
145145
// cleanups, etc. The error handler function has same signature as Handle's
146146
// error handling function [Handler]. By returning nil resets the
147147
// error, which allows e.g. prevent automatic error logs to happening.
148-
// Otherwise, the output results depends on the current [Tracer] and assert
148+
// Otherwise, the output results depends on the current trace and assert
149149
// settings. The default trace setting prints call stacks for panics but not for
150150
// errors:
151151
//
@@ -155,15 +155,15 @@ func Handle(err *error, a ...any) {
155155
//
156156
// defer err2.Catch(err2.Noop)
157157
//
158-
// You can have unlimited amount of error handlers. They are called if error
158+
// You can give unlimited amount of error handlers. They are called if error
159159
// happens and they are called in the same order as they are given or until one
160160
// of them resets the error like [Reset] in the next sample:
161161
//
162162
// defer err2.Catch(err2.Noop, err2.Reset, err2.Log) // err2.Log not called!
163163
//
164-
// The last one calls your error handler, and you have an explicit panic handler
165-
// as well, where you can e.g. continue panicking to propagate it for above
166-
// callers or stop it like below:
164+
// The next sample calls your error handler, and you have an explicit panic
165+
// handler as well, where you can e.g. continue panicking to propagate it for
166+
// above callers or stop it like below:
167167
//
168168
// defer err2.Catch(func(err error) error { return err }, func(p any) {})
169169
func Catch(a ...any) {
@@ -183,7 +183,7 @@ func Catch(a ...any) {
183183
doTrace(err)
184184
}
185185

186-
// Throwf builds and throws (panics) an error. For creation it's similar to
186+
// Throwf builds and throws an error (panic). For creation it's similar to
187187
// [fmt.Errorf]. Because panic is used to transport the error instead of error
188188
// return value, it's called only if you want to non-local control structure for
189189
// error handling, i.e. your current function doesn't have error return value.

formatter.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ func init() {
1010
}
1111

1212
// SetFormatter sets the current formatter for the err2 package. The default
13-
// formatter.Decamel tries to process function names to human readable and the
14-
// idiomatic Go format, i.e. all lowercase, space delimiter, etc.
13+
// [formatter.Decamel] processes function names to human readable and the
14+
// idiomatic Go format, i.e. all lowercase, space delimiter, package names colon
15+
// separated. The example how a quite complex method name gives a proper error
16+
// message prefix:
17+
//
18+
// "ssi.(*DIDAgent).CreateWallet" -> "ssi: didagent create wallet"
1519
//
1620
// Following line sets a noop formatter where errors are taken as function names
1721
// are in the call stack.

formatter/formatter.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ type Formatter struct {
2424
DoFmt
2525
}
2626

27-
var (
28-
// Decamel is preimplemented and default formatter to produce human
29-
// readable error strings from function names.
30-
// func CopyFile(..) -> "copy file: file not exists"
31-
// ^-------^ -> generated from CopyFile
32-
Decamel = &Formatter{DoFmt: str.Decamel}
27+
// Decamel is preimplemented and default formatter to produce human
28+
// readable error strings from function names.
29+
//
30+
// func CopyFile(..) -> "copy file: file not exists"
31+
// ^-------^ -> generated from 'func CopyFile'
32+
var Decamel = &Formatter{DoFmt: str.Decamel}
3333

34-
// Noop is preimplemented formatter that does nothing to function name.
35-
// func CopyFile(..) -> "CopyFile: file not exists"
36-
// ^------^ -> function name as it is: CopyFile
37-
Noop = &Formatter{DoFmt: func(i string) string { return i }}
38-
)
34+
// Noop is preimplemented formatter that does nothing to function name.
35+
//
36+
// func CopyFile(..) -> "CopyFile: file not exists"
37+
// ^------^ -> function name as it is: CopyFile
38+
var Noop = &Formatter{DoFmt: func(i string) string { return i }}
3939

4040
// Format just calls function set in the DoFmt field.
4141
func (f *Formatter) Format(input string) string {

0 commit comments

Comments
 (0)