Skip to content

Commit 91a8ed8

Browse files
author
Ronen Hilewicz
committed
Fix formatting of inner errors
The Go convention is for errors to be formatted from outermost to innermost separated by colons. However, `AsertoError` prints the inner error messages _before_ the outer. For example, consider the following snippet: ```go // An AsertoError ErrInvalidPermission := cerr.NewAsertoError("E20008", codes.InvalidArgument, http.StatusBadRequest, "invalid permission") // Regular Go errors ErrInvalidIdentifier := errors.New("invalid identifier") innerError := errors.Wrap(ErrInvalidIdentifier, "'GET'") // Final error err := ErrInvalidPermission.Err(innerError).Msg("'resource:can_read'") fmt.Println(err) ``` We expect the output to be: ``` E20008 invalid permission: 'resource:can_read': 'GET': invalid identifier ``` But instead we get: ``` E20008 invalid permission: 'GET': invalid identifier: 'resource:can_read' ``` This change to the `Error()` function fixes the formatting.
1 parent 971bab0 commit 91a8ed8

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

errors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ func (e *AsertoError) Error() string {
9090
for k, v := range e.data {
9191
if k == "msg" {
9292
if innerMessage != "" {
93-
innerMessage += colon
93+
innerMessage = colon + innerMessage
9494
}
95-
innerMessage += v
95+
innerMessage = v + innerMessage
9696
}
9797
}
9898
}

0 commit comments

Comments
 (0)