Skip to content

Commit 9d653ce

Browse files
committed
Updated readme
1 parent 24c22e3 commit 9d653ce

File tree

1 file changed

+63
-9
lines changed

1 file changed

+63
-9
lines changed

README.md

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@ Import it in your code:
1919
import "github.com/primalskill/errors"
2020
```
2121

22-
## Basic Usage
22+
## Running the Tests
23+
24+
Navigate to the module's directory and execute:
25+
26+
```bash
27+
make test
28+
```
29+
30+
## Example - Basic Usage
31+
32+
You can find more examples in the [docs](https://pkg.go.dev/github.com/primalskill/errors#pkg-examples).
2333

2434
```go
2535
package main
@@ -30,21 +40,65 @@ import (
3040
)
3141

3242
func main() {
33-
err1 := errors.E("this is an error", errors.WithMeta("metaKey1", "meta value 1", "isAuth", true))
34-
err2 := errors.E("embedded error", err1, errors.WithMeta("additionalMeta", 246))
3543

36-
fmt.Println(err2.Error()) // outputs the error message: embedded error
44+
// Define an error
45+
err1 := errors.E(
46+
"some error",
47+
errors.WithMeta(
48+
"metaKey1", "meta value",
49+
"isAuth", true,
50+
),
51+
)
52+
53+
// Define another error and wrap err1
54+
err2 := errors.E(
55+
"wrapped error",
56+
err1,
57+
errors.WithMeta(
58+
"additionalMeta", 246,
59+
),
60+
)
61+
62+
fmt.Println(err2.Error()) // output: "wrapped error"
3763

3864
// Convert stdlib error to errors.Error
3965
var ee *errors.Error
4066
errors.As(err2, &ee)
67+
fmt.Printf("%+v", ee.Meta) // output: [additionalMeta:246]
68+
69+
// GetMeta helper func
70+
m, ok := errors.GetMeta(err1)
71+
fmt.Printf("\n%+v\n%+v\n", ok, m) // output: true \n [metaKey1:meta value isAuth:true]
72+
73+
// Unwrap err2 to get err1
74+
uErr := errors.Unwrap(err2)
75+
fmt.Println(uErr.Error()) // output: "some error"
76+
}
77+
```
78+
79+
## Example - Mirror an Existing Error
4180

42-
fmt.Printf("%+v", ee.Meta) // outputs err2 Meta
81+
```go
82+
package main
4383

44-
m := errors.GetMeta(err2) // get the Meta with a helper func
45-
fmt.Printf("%+v", m) // outputs the Meta attached to err2
84+
import (
85+
"fmt"
86+
"github.com/primalskill/errors"
87+
)
4688

47-
uErr := errors.Unwrap(err2) // unwraps err2 to get err1
48-
fmt.Println(uErr.Error()) // outputs: this is an error
89+
func main() {
90+
91+
// Define an error
92+
err1 := errors.E("this is an error", errors.WithMeta("key1", "val1"))
93+
94+
// Preload or "mirror" err1 in err2 with a few rules:
95+
// - err2 preloads err1 message
96+
// - err2 preloads err1 Meta key/value pairs
97+
// - if additional metas are defined on err2 it will merge it to the others
98+
// - err2 overwrites err1 source location to correctly show the location where err2 was executed
99+
err2 := errors.M(err1, errors.WithMeta("key2", "val2"))
100+
101+
fmt.Printf("%+v", errors.PrettyPrint(err2))
49102
}
50103
```
104+

0 commit comments

Comments
 (0)