Add new function "StatusCode in httperror.go by suwakei · Pull Request #2892 · labstack/echo
Overview
Implemented the Is method on the HTTPError struct, enabling error checking using errors.Is (particularly for comparing with sentinel errors).
Background
Starting with Go 1.13, errors.Is is recommended for error checking, but Echo's HTTPError did not previously implement the Is method. Echo's predefined errors (such as echo.ErrNotFound) are of the internal httpError type, while errors created with NewHTTPError are of type *HTTPError. Because these are distinct types in Go's type system, errors.Is(err, echo.ErrNotFound) would return false if err was 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).