-
-
Notifications
You must be signed in to change notification settings - Fork 5.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
Handle function call errors more gracefully in routes #24120
Comments
In a sane language with exceptions, I would say Ideally there should be linting for this but I fear it might require us to implement this linting ourselves, or maybe some existing linters can already help. |
I like proposal 2, I have ever implemented a web framework https://gitea.com/lunny/tango support to receive one or two return error parameters like
|
I also have done similar thing in my Java/PHP/Golang web frameworks. Usually I make the return type as an interface, then the handler could have the full control for what it wants to output. The common responses could be provided by builtin response classes: func handler(ctx *web.Context) Response {
return ctx.JSON()
}
func handler(ctx *web.Context) Response {
return ctx.HTML()
}
func handler(ctx *web.Context) Response {
return ctx.PlainText()
}
func handler(ctx *web.Context) Response {
return ctx.ServerError()
}
func handler(ctx *web.Context) Response {
return &MyResponseWriter{ write status, write headers, write body ... }
} |
I agree, it surprised me the first time I saw this: v := DoSomething(ctx)
if ctx.Written() {
return
}
// then the v is valid |
No time for it. Close. |
Feature Description
Example: help more problems like #24118
Before
There are some problems:
return
could be forgotten, a lot of times.Proposal 1
Use generic, let a function like
ErrorProof
handles server errors automatically.ErrorProof
can use reflect to get the target function's name, it doesn't affect performance because in most cases there is no "internal server error".Proposal 2
Return the err directly, make our handler framework support such return value for handler functions (with stacktrace):
Feasible?
These are just some early ideas, haven't tried whether it is really feasible, but if people like this proposal, I will spend some time to try.
I really prefer the second one, it looks clear and intuitive
The text was updated successfully, but these errors were encountered: