Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func doStuff() error {
// full name of the current function as the Op.
const op = errors.Op("doStuff")

err := fmt.Errorf("some error")
err := errors.Errorf("some error")

return errors.With(err,
errors.SeverityRuntime, op,
Expand All @@ -37,6 +37,20 @@ func doStuff() error {
}
```

You can use an option pattern to build the error:

``` go
// Start with an empty slice or one with default values
errorOpts := []errors.KeyValuer{}
// Add more values as needed...
// if ... then add key1
errorOpts = append(errorOpts, errors.KV("key1", "value1"))
// if ... then add Severity
errorOpts = append(errorOpts, errors.SeverityInput)
// Build the final error and return
return errors.With(err, errorOpts...)
```

Fetch values with `errors.Value*` functions:

``` go
Expand Down Expand Up @@ -67,10 +81,14 @@ The key-value pair is any type that implements the `KeyValuer` interface:
type KeyValuer interface {
Key() any
Value() any
String() string
}
```

> Tip: It helps if the key and value both implement the `fmt.Stringer`
> interface (`String() string`).
> This will help the error formatter to print the key-value pair during calls
> to `Error() string`.

The `errors.With()` functions will wrap the given error with a list of
key-values. If more than one key-value is given, they will be chained together.

Expand Down Expand Up @@ -123,7 +141,7 @@ should be notified about an error on their application.
### Code

This is a simple string to be used as error codes so your application can
differentiate different errors.
differentiate errors.

Whoever receives this error code should write a switch case to handle the
different possible codes.
Expand All @@ -135,16 +153,16 @@ switch errors.GetCode(err) {
case MyCode2:
// handle MyCode2
default:
// handle unknown code
// handle unknown code
}
```

### KV

This is an arbitrary key-value pair that can be used to inject extra content in
This is an arbitrary key-value pair that can be used to inject extra context in
the error.

All values are printed by the default error formatter as `{key: value, ...}`.
All values are printed by the default error formatter as `{key=value, ...}`.

The key can be any comparable value.

Expand All @@ -164,7 +182,7 @@ The `errors.FullFormater` is the default and will print something like (from the
example package):

``` text
customOpExample: main.doGreetings.func1 (line 58): main.doGreetings: main.greetings: main.greeter[...].sayHello: [fatal] (RUNTIME_ERROR) name cannot be empty {context3: value3, context2: value2, context1: value1}
customOpExample: main.doGreetings.func1 (main.go:58): main.doGreetings: main.greetings: main.greeter[...].sayHello: [fatal] (RUNTIME_ERROR) name cannot be empty {context3=value3, context2=value2, context1=value1}
```

The `errors.RootErrorFormatter` will only print the root error:
Expand All @@ -177,6 +195,6 @@ And there is a variation `errors.RootErrorKVFormatter` that will print the
root error with the KV as context.

``` text
name cannot be empty {context3: value3, context2: value2, context1: value1}
name cannot be empty {context3=value3, context2=value2, context1=value1}
```

2 changes: 1 addition & 1 deletion dont_panic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestDontPanic(t *testing.T) {
func Test_getPanicOp(t *testing.T) {
op := getPanicOp()
if op != opUnknownFunction {
t.Errorf("expected unkown function, got %v", op)
t.Errorf("expected unknown function, got %v", op)
}

err := DontPanic(func() {
Expand Down
Loading