Skip to content

Commit

Permalink
chore: Remove details from govy.RuleError (#60)
Browse files Browse the repository at this point in the history
## Motivation

We're moving towards a more unified error message creation. The goal is
to allow users to manipulate the error message however they see fit.
With that in mind, `govy.RuleError` should have its `Message` field
populated with a created error that is ready to be used as is and passed
downstream.

## Related Changes

- #10
- #59

## Breaking Changes

Removed `govy.RuleError.Details` field, the error's details are now part
of the `govy.RuleError.Message` and not just the
`govy.RuleError.Error()` output.
  • Loading branch information
nieomylnieja authored Dec 20, 2024
1 parent 37c523c commit 10fa133
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
9 changes: 1 addition & 8 deletions pkg/govy/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,19 +211,12 @@ func NewRuleError(message string, codes ...ErrorCode) *RuleError {
type RuleError struct {
Message string `json:"error"`
Code ErrorCode `json:"code,omitempty"`
Details string `json:"details,omitempty"`
Description string `json:"description,omitempty"`
}

// Error implements the error interface.
func (r *RuleError) Error() string {
if r.Details == "" {
return r.Message
}
if r.Message == "" {
return r.Details
}
return r.Message + "; " + r.Details
return r.Message
}

// AddCode extends the [RuleError] with the given error code.
Expand Down
16 changes: 12 additions & 4 deletions pkg/govy/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ func (r Rule[T]) Validate(v T) error {
switch ev := err.(type) {
case *RuleError:
if len(r.message) > 0 {
ev.Message = r.message
ev.Message = addDetailsToErrorMessage(r.message, r.details)
}
ev.Details = r.details
ev.Description = r.description
return ev.AddCode(r.errorCode)
case *PropertyError:
Expand All @@ -66,9 +65,8 @@ func (r Rule[T]) Validate(v T) error {
msg = r.message
}
return &RuleError{
Message: msg,
Message: addDetailsToErrorMessage(msg, r.details),
Code: r.errorCode,
Details: r.details,
Description: r.description,
}
}
Expand Down Expand Up @@ -118,3 +116,13 @@ func (r Rule[T]) plan(builder planBuilder) {
}
*builder.children = append(*builder.children, builder)
}

func addDetailsToErrorMessage(message, details string) string {
if details == "" {
return message
}
if message == "" {
return details
}
return message + "; " + details
}
3 changes: 1 addition & 2 deletions pkg/govy/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,8 @@ func TestRule_WithDescription(t *testing.T) {
err := r.Validate(-1)
assert.Require(t, assert.Error(t, err))
assert.Equal(t, &govy.RuleError{
Message: "must be positive",
Message: "must be positive; some details",
Code: "test",
Details: "some details",
Description: "the integer must be positive",
}, err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/rules/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -943,10 +943,10 @@ func TestStringGitRef(t *testing.T) {
if tc.expectedErr != nil {
assert.ErrorContains(t, err, tc.expectedErr.Error())
assert.True(t, govy.HasErrorCode(err, ErrorCodeStringGitRef))
assert.Equal(
assert.ErrorContains(
t,
err,
"see https://git-scm.com/docs/git-check-ref-format for more information on Git reference naming rules",
err.(*govy.RuleError).Details,
)
} else {
assert.NoError(t, err)
Expand Down

0 comments on commit 10fa133

Please sign in to comment.