forked from m-mizutani/goerr
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e162722
commit 61f2f67
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/abyssparanoia/goerr" | ||
"go.uber.org/zap" | ||
) | ||
|
||
var ( | ||
testErr = goerr.New("test error"). | ||
WithCategory("test_category"). | ||
WithCode("TEST123"). | ||
WithDetail("test detail"). | ||
WithValue("key", "value") | ||
) | ||
|
||
func main() { | ||
l, _ := zap.NewDevelopment() | ||
if err := wrapDoSomething(); err != nil { | ||
l.Error("error", goerr.ZapError(err)) | ||
} | ||
} | ||
|
||
func wrapDoSomething() error { | ||
if err := doSomething(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func doSomething() error { | ||
err := testErr.Errorf("failed to do something"). | ||
WithValue("other_key", "other_value") | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package goerr | ||
|
||
import ( | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
func ZapError(err error) zapcore.Field { | ||
if goErr := Unwrap(err); goErr != nil { | ||
return zap.Object("error", goErr) | ||
} | ||
return zap.Error(err) | ||
} |