-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert.go
More file actions
29 lines (24 loc) · 795 Bytes
/
assert.go
File metadata and controls
29 lines (24 loc) · 795 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package logw
import (
"context"
"fmt"
)
// Assert asserts that the predicate is true and logw.Fatals/panics otherwise.
func Assert(ctx context.Context, predicate bool, format string, args ...interface{}) {
if !predicate {
fail(ctx, format, args...)
}
}
// AssertNil asserts that the err is nil and logw.Fatals/panics otherwise. The error is
// implicitly appended to the format as ": %v" and passed as a trailing arg.
func AssertNil(ctx context.Context, err error, format string, args ...interface{}) {
if err != nil {
args = append(args, err)
fail(ctx, format+": %v", args...)
}
}
func fail(ctx context.Context, format string, args ...interface{}) {
FatalDepthf(ctx, 2, format, args...)
// Backup panic, in case the logw.Fatal doesn't.
panic(fmt.Sprintf(format, args...))
}