-
Notifications
You must be signed in to change notification settings - Fork 285
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
Custom 500 page #49
Comments
You can use If you want to set custom 500 page, my solution is to define my own recovery middleware. |
Hi @piotrrojek , as @ipfans said, func (r *Router) NotFound(handlers ...Handler) can be used to set custom 404 page. 404 is easy to detect because it's on the router layer, but most of 500 is based on your handler logic, framework has no idea about it, and it often happens in the middle of handler logic. Similiar solution to @ipfans , you can make a middleware/function/method to handle 500 page and call it whenever you need it. For example, in Gogs, we use a custom method to show 500 page: https://github.com/gogits/gogs/blob/master/modules/middleware/context.go#L131 Hope helps! |
@unknwon 您看看如果 handler可以返回error 就可以自定义500页面了, 这样实现业务逻辑的代码也会短很多。就行echo这样 https://github.com/labstack/echo |
@cgyy This is a good idea, thanks your info! |
Hi all, Right now you can trigger 500 page handling by: m.Get("/", func() error {
return errors.New("this is an error")
}) So default 500 handler will be called. If you want to use custom handler(s): m.InternalServerError(handler1, handler2, ...) Be sure that you write response status 500 in your last handler. But if there is no error, just return nil: m.Get("/", func() error {
return nil
}) Feedbacks welcome! |
赞一个,这样逻辑代码会简洁很多! |
@cgyy thanks for confirmation! |
你好,@unknwon, 在实际使用中发现这样的问题: m.Use(func(c *macaron.Context) error {
c.Next()
return errors.New("middle ware error")
})
m.Get("/", func() string {
return "Hello world!"
}) 最后会输出 Hello world!middle ware error。 |
I think your example does not explain well, from limited information, I think you should use m.Use(func(c *macaron.Context) error {
c.Next()
if ctx.Written() {
return nil
}
return errors.New("middle ware error")
}) |
好的,明白了,谢谢~ |
Close as implemented. ⭐ |
Is there any way to add custom 500 page? Or any error page besides 404?
The text was updated successfully, but these errors were encountered: