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
Return error if reverse() does not find a match #2615
Comments
This comment was marked as outdated.
This comment was marked as outdated.
How about a new function to avoid breaking backwards compatibility:
Would a compile-time error be possible? |
This comment was marked as outdated.
This comment was marked as outdated.
ok, after reading this another time I see that I got the problem wrong. You are pointing that registering route with empty path and reversing that gives same result as reversing non existent path func TestTest(t *testing.T) {
e := echo.New()
handler := func(c echo.Context) error {
return c.String(http.StatusNotImplemented, "Nope")
}
r := e.GET("", handler) // path is registered as ""
r.Name = "test"
existingEmpty := e.Reverse("test")
assert.Equal(t, "", existingEmpty)
notExistingEmpty := e.Reverse("not-existing")
assert.Equal(t, "", notExistingEmpty)
} this reveals something else. We actually never register empty routes as empty. Echo sets them to Lines 201 to 209 in d549290
but routes are stored little bit before that we switch themto Lines 188 to 198 in d549290
we probably could add changing from empty string to |
I agree that a run-time error does not improve the situation. A compile-time error, however, would not require any API changes, and would catch reverse-non-existing-routes errors at compile-time, which is a huge win. |
I do not know that you mean by compile time error. English is not my native language so there is probably language barrier here. Do you mean that if there is |
Yes! |
Hey @akhayyat 👋, perhaps I am overlooking something, but I'm unaware that Go Compiler supports compile-time assertions. How would something like that be implemented? I found the following issue on Github, indicating that Go doesn't and won't support compile-time checks shortly. |
I am closing this issue as I do not think we can do anything more than #2616 did. |
Issue Description
Currently,
reverse()
fails silently in case a matching route is not found, returning an empty string.Would it be possible to make it fail a little louder, returning an error?
Ideally, a compile-time error would be perfect, but a run-time error is still better than failing silently.
Expected behaviour
Compile-time or run-time error if the provided route name does not exist.
Actual behaviour
Empty string URL.
Steps to reproduce
ctx.Echo().Reverse("undefined-route-name")
Version/commit
v4.11.3
The text was updated successfully, but these errors were encountered: