feat: Add IdentifierStartsWith#27
Conversation
4af34f8 to
14661e4
Compare
| Expect(IdentifierStartsWith(e, "1")).To(BeTrue()) | ||
| }) | ||
|
|
||
| It("should return true when error's identifier starts with the prefix", func() { |
There was a problem hiding this comment.
could you add some tests with error Identifier = 31-22-12 and test with the following prefix:
- 3
- 31
- 31-2
- 31-22
- 31-22-1
- 31-22-12
There was a problem hiding this comment.
For sure, I can, but what is the difference with these tests:
It("should return true when error's identifier starts with the prefix", func() {
e1_1 := Wrap(ErrForbidden, WithIdentifier(1))
e1_2 := Wrap(e1_1, WithIdentifier(2))
e1_3 := Wrap(e1_2, WithIdentifier(3))
Expect(IdentifierStartsWith(e1_3, "3")).To(BeTrue())
Expect(IdentifierStartsWith(e1_3, "3-2")).To(BeTrue())
Expect(IdentifierStartsWith(e1_3, "3-2-1")).To(BeTrue())
})There was a problem hiding this comment.
I want to know if you expect false for 3, 31-2 and 31-21-1 Meaning ig you treat the prefix as a pure string or if the value in dash means something
There was a problem hiding this comment.
I made some changes to catch edge cases
| // IdentifierStartsWith checks if the error's identifier starts with the given prefix. | ||
| func IdentifierStartsWith(err error, prefix string) bool { | ||
| var e *Error | ||
| if ok := errors.As(err, &e); !ok { | ||
| return false | ||
| } | ||
| return strings.HasPrefix(e.GetIdentifier(), prefix) | ||
| } | ||
|
|
There was a problem hiding this comment.
Since Identifier is a slice of int32, we can do this:
func IdentifierStartsWith(err error, prefix ...uint32) bool {
var e *Error
if !errors.As(err, &e) {
return false
}
if len(prefix) > len(e.Identifier) {
return false
}
n := len(e.Identifier)
for i, p := range prefix {
if e.Identifier[n-1-i] != p {
return false
}
}
return true
}There was a problem hiding this comment.
I found another implementation based on string. Your solution should work, but it seems to me more complicated as we have to deal with the reverse order
| It("should return true when error's identifier starts with the prefix", func() { | ||
| e := Wrap(ErrForbidden, WithIdentifier(1)) | ||
| Expect(IdentifierStartsWith(e, "1")).To(BeTrue()) | ||
| }) | ||
|
|
||
| It("should return true when error's identifier starts with the prefix", func() { |
There was a problem hiding this comment.
I will change this
| It("should return false when error's identifier does not start with the prefix", func() { | ||
| e := Wrap(ErrForbidden, WithIdentifier(1)) | ||
| Expect(IdentifierStartsWith(e, "2")).To(BeFalse()) | ||
| }) | ||
|
|
||
| It("should return false when error's identifier does not start with the prefix", func() { |
| ### Specific use-case with IdentifierStartsWith() | ||
|
|
||
| `IdentifierStartsWith(error, prefix)` checks whether this error's identifier, formatted as a string, starts with the given prefix. | ||
|
|
||
| For example: | ||
| If e.Identifier: "3-2-1", then | ||
| ```go | ||
| IdentifierStartsWith(e, "3-2") return True | ||
| IdentifierStartsWith(e, "2-1") return False | ||
| ``` | ||
|
|
There was a problem hiding this comment.
Maybe to explain the differences between Is() and IdentifierStartsWith()
e := Wrap(Wrap(Wrap(Base, WithIdentifier(1)), WithIdentifier(2)), WithIdentifier(3))
IdentifierStartsWith(e, 3, 2) // true — matches the most recent wraps (outermost)
e.Is(parent) // matches an ancestor (innermost)644e9ef to
10e3c1b
Compare
| // As is a wrapper around errors.As to check if the error is of a specific type. | ||
| func As(err error, target any) bool { return errors.As(err, target) } | ||
|
|
||
| // IdentifierStartsWith checks if the error's identifier starts with the given prefix. |
There was a problem hiding this comment.
| // IdentifierStartsWith checks if the error's identifier starts with the given prefix. | |
| // IdentifierStartsWith checks if the error's identifier string starts with the given prefix. |
10e3c1b to
2a30dd3
Compare
Signed-off-by: Anthony TREUILLIER <anthony.treuillier@scality.com>
2a30dd3 to
81f2317
Compare
IdentifierStartsWithallows error comparison with a prefix. Useful when matching specific errors with HTTP Code in API handlersExample: If e.Identifier: "3-2-1"