-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add new method "Is" in httperror.go #2892
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2892 +/- ##
==========================================
+ Coverage 92.92% 92.94% +0.01%
==========================================
Files 43 43
Lines 4480 4491 +11
==========================================
+ Hits 4163 4174 +11
Misses 197 197
Partials 120 120 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
This is a good idea. I have to research a bit this. I know that |
|
Well, there is one argument against This would be errors.Is(
&echo.HTTPError{Code: 404, Message: "user not found"},
&echo.HTTPError{Code: 404, Message: "file missing"},
)At the moment same check can be achieved with func TestXXX(t *testing.T) {
//err := &echo.HTTPError{Code: http.StatusNotFound}
err := echo.ErrNotFound // &echo.httpError{http.StatusNotFound}
var sc echo.HTTPStatusCoder
if errors.As(err, &sc) && sc.StatusCode() != http.StatusNotFound {
t.Fail() // different code
}
}I am little bit afraid of this change. LLMs do not point me any larger projects using API errors equality check that way. |
|
maybe we could create function in Echo that checks/extracts status code from error so you can have one-liners like if http.StatusNotFound == echo.HTTPStatusCode(err) { // returns 0 if err is not or does not wrap HTTPStatusCoder interface
// is same code
}I have been working lately with different middlewares where I often need to get statusCode from error and this function would be usable in that use-case also. |
|
Regarding your point about |
|
@suwakei , LGTM but could you rename that method to |
|
@aldas |
aldas
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
@suwakei thank you! |
Overview
Implemented the
Ismethod on theHTTPErrorstruct, enabling error checking usingerrors.Is(particularly for comparing with sentinel errors).Background
Starting with Go 1.13,
errors.Isis recommended for error checking, but Echo'sHTTPErrordid not previously implement theIsmethod. Echo's predefined errors (such asecho.ErrNotFound) are of the internalhttpErrortype, while errors created withNewHTTPErrorare of type*HTTPError. Because these are distinct types in Go's type system,errors.Is(err, echo.ErrNotFound)would return false iferrwas of type*HTTPError, making intended error handling difficult. This change resolves the issue by adding logic that considers errors with matching status codes to be the same error.Changes
httperror.go: Added anIsmethod to theHTTPErrorstruct. When comparing against*HTTPErroror*httpError, it compares the status code.httperror_test.go: Added a test case for theIsmethod (TestHTTPError_Is).