Skip to content
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

Closed
piotrrojek opened this issue May 13, 2015 · 11 comments
Closed

Custom 500 page #49

piotrrojek opened this issue May 13, 2015 · 11 comments

Comments

@piotrrojek
Copy link

Is there any way to add custom 500 page? Or any error page besides 404?

@ipfans
Copy link

ipfans commented Jul 10, 2015

You can use func (r *Router) NotFound(handlers ...Handler) to set default 404 page.

If you want to set custom 500 page, my solution is to define my own recovery middleware.

@unknwon
Copy link
Contributor

unknwon commented Jul 15, 2015

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!

@cgyy
Copy link

cgyy commented Jul 15, 2015

@unknwon 您看看如果 handler可以返回error 就可以自定义500页面了, 这样实现业务逻辑的代码也会短很多。就行echo这样 https://github.com/labstack/echo

@unknwon
Copy link
Contributor

unknwon commented Jul 15, 2015

@cgyy This is a good idea, thanks your info!

unknwon added a commit that referenced this issue Jul 28, 2015
unknwon added a commit that referenced this issue Jul 28, 2015
@unknwon
Copy link
Contributor

unknwon commented Jul 28, 2015

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
Copy link

cgyy commented Jul 30, 2015

赞一个,这样逻辑代码会简洁很多!

@unknwon
Copy link
Contributor

unknwon commented Jul 30, 2015

@cgyy thanks for confirmation!

@cgyy
Copy link

cgyy commented Aug 3, 2015

你好,@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。
这种问题怎么解决比较好,是不是从业务逻辑上讲在middleware中,在Next执行后发生错误不应该return error 而是直接panic更好一点。
期待您的建议.

@unknwon
Copy link
Contributor

unknwon commented Aug 3, 2015

@cgyy

I think your example does not explain well, from limited information, I think you should use ctx.Written() method to check if there are any thing has been written before you return error:

  m.Use(func(c *macaron.Context) error {
        c.Next()

    if ctx.Written() {
        return nil
    }
        return errors.New("middle ware error")
    })

@cgyy
Copy link

cgyy commented Aug 3, 2015

好的,明白了,谢谢~

@unknwon
Copy link
Contributor

unknwon commented Aug 24, 2015

Close as implemented. ⭐

@unknwon unknwon closed this as completed Aug 24, 2015
unknwon added a commit that referenced this issue Sep 16, 2016
unknwon added a commit that referenced this issue Sep 16, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants