Skip to content

Commit

Permalink
Impl clone method (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkanenobu authored Dec 18, 2024
1 parent 23f878e commit b5e001f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
8 changes: 8 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ func (x *Error) ID(id string) *Error {
return x
}

// Clone creates a new error extends receiver error's info
// No side effect to receiver
func (x *Error) Clone() *Error {
err := newError()
x.copy(err)
return err
}

// Wrap creates a new Error and copy message and id to new one.
func (x *Error) Wrap(cause error) *Error {
err := newError()
Expand Down
11 changes: 10 additions & 1 deletion examples/logging_with_zap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,20 @@ func main() {
},
}

err := goerr.Wrap(errors.New("some error")).WithValue("key", "value").WithValue("num_key", 1).WithValue("struct", st)
err := goerr.Wrap(errors.New("some error")).
WithValue("key", "value").
WithValue("num_key", 1).
WithValue("struct", st)

if goErr := goerr.Unwrap(err); goErr != nil {
log.Printf("%s\n", goErr.LogValue())
}

logger.Error("something happening", zap.Object("error", err))

if goErr := goerr.Unwrap(err); goErr != nil {
err2 := goErr.Clone()
err2 = err2.WithValue("___2", "This is err2")
logger.Error("something happening2", zap.Object("error2", err2))
}
}

0 comments on commit b5e001f

Please sign in to comment.