Skip to content

Conversation

@suwakei
Copy link

@suwakei suwakei commented Feb 9, 2026

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 an Is method to the HTTPError struct. When comparing against *HTTPError or *httpError, it compares the status code.
  • httperror_test.go: Added a test case for the Is method (TestHTTPError_Is).

@codecov
Copy link

codecov bot commented Feb 10, 2026

Codecov Report

❌ Patch coverage is 77.77778% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.89%. Comparing base (48f25a6) to head (a16859c).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
httperror.go 77.77% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2892      +/-   ##
==========================================
- Coverage   92.92%   92.89%   -0.04%     
==========================================
  Files          43       43              
  Lines        4480     4489       +9     
==========================================
+ Hits         4163     4170       +7     
- Misses        197      198       +1     
- Partials      120      121       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@aldas
Copy link
Contributor

aldas commented Feb 10, 2026

This is a good idea. I have to research a bit this. I know that Is Unwrap etc are recursive and our HTTPError contains also wrapped error field. I do not want to merge things that I am not fully sure. It is easy to add new things but hard to remove them.

@aldas
Copy link
Contributor

aldas commented Feb 11, 2026

Well, there is one argument against Is implementation.

This would be true but these are different errors from different domains.

	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 errors.As and using echo.HTTPStatusCoder interface

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.

@aldas
Copy link
Contributor

aldas commented Feb 11, 2026

maybe we could create function in Echo that checks/extracts status code from error so you can have one-liners like errors.Is allows.
ala

	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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants