-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from m-mizutani/feature/err-value-set
Add Builder feature
- Loading branch information
Showing
5 changed files
with
155 additions
and
11 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,40 @@ | ||
package goerr | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// Builder keeps a set of key-value pairs and can create a new error and wrap error with the key-value pairs. | ||
type Builder struct { | ||
values values | ||
} | ||
|
||
// NewBuilder creates a new Builder | ||
func NewBuilder() *Builder { | ||
return &Builder{values: make(values)} | ||
} | ||
|
||
// With copies the current Builder and adds a new key-value pair. | ||
func (x *Builder) With(key string, value any) *Builder { | ||
newVS := &Builder{values: x.values.clone()} | ||
newVS.values[key] = value | ||
return newVS | ||
} | ||
|
||
// New creates a new error with message | ||
func (x *Builder) New(format string, args ...any) *Error { | ||
err := newError() | ||
err.msg = fmt.Sprintf(format, args...) | ||
err.values = x.values.clone() | ||
|
||
return err | ||
} | ||
|
||
// Wrap creates a new Error with caused error and add message. | ||
func (x *Builder) Wrap(cause error, msg ...any) *Error { | ||
err := newError() | ||
err.msg = toWrapMessage(msg) | ||
err.cause = cause | ||
err.values = x.values.clone() | ||
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,32 @@ | ||
package goerr_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/m-mizutani/goerr" | ||
) | ||
|
||
func newErrorWithBuilder() *goerr.Error { | ||
return goerr.NewBuilder().With("color", "orange").New("error") | ||
} | ||
|
||
func TestBuilderNew(t *testing.T) { | ||
err := newErrorWithBuilder() | ||
|
||
if err.Values()["color"] != "orange" { | ||
t.Errorf("Unexpected value: %v", err.Values()) | ||
} | ||
} | ||
|
||
func TestBuilderWrap(t *testing.T) { | ||
cause := goerr.New("cause") | ||
err := goerr.NewBuilder().With("color", "blue").Wrap(cause, "error") | ||
|
||
if err.Values()["color"] != "blue" { | ||
t.Errorf("Unexpected value: %v", err.Values()) | ||
} | ||
|
||
if err.Unwrap().Error() != "cause" { | ||
t.Errorf("Unexpected cause: %v", err.Unwrap().Error()) | ||
} | ||
} |
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,30 @@ | ||
package main | ||
|
||
import ( | ||
"log/slog" | ||
|
||
"github.com/m-mizutani/goerr" | ||
) | ||
|
||
type object struct { | ||
id string | ||
color string | ||
} | ||
|
||
func (o *object) Validate() error { | ||
eb := goerr.NewBuilder().With("id", o.id) | ||
|
||
if o.color == "" { | ||
return eb.New("color is empty") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func main() { | ||
obj := &object{id: "object-1"} | ||
|
||
if err := obj.Validate(); err != nil { | ||
slog.Default().Error("Validation error", "err", err) | ||
} | ||
} |