-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
73 lines (63 loc) · 1.52 KB
/
example_test.go
File metadata and controls
73 lines (63 loc) · 1.52 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package errors_test
import (
"fmt"
"log/slog"
"os"
"github.com/frederik-jatzkowski/errors"
)
func Example_onlyHumanReadable() {
err := errors.Errorf(
"call failed: %w",
errors.Errorf(
"processing id %d:%w",
123,
errors.Join(
errors.Errorf("two things failed:%w,%w",
errors.New("something bad happened"),
errors.Errorf(
"doing somthing: %w",
errors.Errorf("failed"),
),
),
errors.New("something else happened"),
),
),
)
fmt.Printf("%s", err)
// Output:
// call failed: processing id 123:
// => two things failed:
// => something bad happened,
// => doing somthing: failed
// => something else happened
}
func Example_includeStackTraces_minimal() {
errors.GlobalFormatSettings(
errors.WithStrippedFileNamePrefix("github.com/frederik-jatzkowski/errors/"),
)
err := errors.Errorf(
"call failed: %w",
errors.Errorf(
"processing id %d:%w",
123,
errors.Join(
errors.Errorf("two things failed: %w, %w",
errors.New("something bad happened"),
errors.Errorf(
"doing somthing: %w",
errors.Errorf("failed"),
),
),
errors.New("something else happened"),
),
),
)
fmt.Printf("%+v", err)
}
func Example_slog() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
err := errors.New("test")
// All errors from this package implement slog.LogValuer.
// They will format itself into an object shaped like {"short": "...", "long": "... with stacks"}.
logger.Error("an error occurred", "error", err)
}