-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
🔥 Support returning response from handler #218
Comments
Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! |
I do agree that appending a Your example can be written in different ways to avoid using app.Get("/", func(c *fiber.Ctx) {
var err error
if user, err := getUser(); err == nil {
c.JSON(user)
} else {
c.JSON(errStruct)
}
}) If we want to implement a app.Get("/", func(c *fiber.Ctx) error {
user, err := getUser()
if err != nil {
return c.JSON(errStruct)
}
return c.JSON(user)
}) This also means that we have to document which Ctx methods support error returns. What are your thoughts? @koddr |
Oh, right. So
But, the repetition isn't really an issue for me. It's mainly a source of errors I trip over again and again. This is what really bugs me. |
@alinnert, the only solution I see is to create an middleware for this because we don't want to break any code. I'm closing this for now, thank you for your input and ideas, might be a thing for |
Is your feature request related to a problem?
It's a very common pattern while building an API to call a function, check the
err
and send a response to the client. At this point the handler should also end executing. Normally this looks like following:For demonstration: in this example this pattern repeats 29 times in ~210 LOC.
The problem is: it's super super easy to forget one of these
return
s. This leads to hard to detect bugs that can't be caught by the language.Describe the solution you'd like
One solution would be to make it possible to return results instead of calling a function to send them. Like:
(plus: this would also reduce the linked example code by 29 LOC)
There are multiple ways to implement this. Some alternatives are:
This feature would be purely additive. It should not be necessary to use this over
c.Send()
orc.Write()
. Therefore it also shouldn't break any existing code.Describe alternatives you've considered
What I could do is write a wrapper function that calls the real handler, accepts anything the handler returns and takes care of sending the response. But I would need to wrap every single handler (possibly also middleware) with this wrapper. That would look like this:
I want to avoid that if that's possible.
Additional context
I found a few frameworks that actually support this way of sending a response, for Go and other languages. One example for Go would be Atreugo.
The text was updated successfully, but these errors were encountered: