Skip to content

Commit e6a1b99

Browse files
committed
use error struct directly
1 parent 84386a8 commit e6a1b99

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

add_error.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,23 @@ type Error struct {
55
Message string `json:"message"`
66
Field string `json:"field,omitempty"`
77
Code int `json:"code,omitempty"`
8-
// Err error `json:"-"` TODO: Unwrap
8+
Err error `json:"-"`
99
}
1010

1111
// Error prints error message.
1212
func (e Error) Error() string {
1313
return e.Message
1414
}
1515

16-
// NewError creates an error with field and message.
17-
func NewError(message string, field string) error {
18-
return NewErrorWithCode(message, field, 0)
19-
}
20-
21-
// NewErrorWithCode creates an error with code.
22-
func NewErrorWithCode(message string, field string, code int) error {
23-
return Error{message, field, code}
16+
// Unwrap internal error.
17+
func (e Error) Unwrap() error {
18+
return e.Err
2419
}
2520

2621
// AddError adds an error to changeset.
2722
// ch := changeset.Cast(user, params, fields)
2823
// changeset.AddError(ch, "field", "error")
2924
// ch.Errors() // []errors.Error{{Field: "field", Message: "error"}}
3025
func AddError(ch *Changeset, field string, message string) {
31-
ch.errors = append(ch.errors, NewError(message, field))
26+
ch.errors = append(ch.errors, Error{Message: message, Field: field})
3227
}

constraint.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ func (constraints Constraints) GetError(err rel.ConstraintError) error {
3535
continue
3636
}
3737

38-
return NewErrorWithCode(c.Message, c.Field, c.Code)
38+
return Error{
39+
Message: c.Message,
40+
Field: c.Field,
41+
Code: c.Code,
42+
Err: err,
43+
}
3944
}
4045
}
4146

constraint_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ func TestConstraint_GetError(t *testing.T) {
2121
{
2222
name: "unique",
2323
err: rel.ConstraintError{Key: "slug_unique_index", Type: rel.UniqueConstraint},
24-
expected: NewError("slug has already been taken", "slug"),
24+
expected: Error{Message: "slug has already been taken", Field: "slug", Err: rel.ConstraintError{Key: "slug_unique_index", Type: rel.UniqueConstraint}},
2525
},
2626
{
2727
name: "fk",
2828
err: rel.ConstraintError{Key: "user_id_ibfk1", Type: rel.ForeignKeyConstraint},
29-
expected: NewError("does not exist", "user_id"),
29+
expected: Error{Message: "does not exist", Field: "user_id", Err: rel.ConstraintError{Key: "user_id_ibfk1", Type: rel.ForeignKeyConstraint}},
3030
},
3131
{
3232
name: "check",
3333
err: rel.ConstraintError{Key: "state_check", Type: rel.CheckConstraint},
34-
expected: NewError("state is invalid", "state"),
34+
expected: Error{Message: "state is invalid", Field: "state", Err: rel.ConstraintError{Key: "state_check", Type: rel.CheckConstraint}},
3535
},
3636
{
3737
name: "undefined unique",

0 commit comments

Comments
 (0)