We are building a web site with go using only the built-in stuff like http.HandleFunc
to set up routing and the html.template
package to render pages. Nothing fancy really and it works fine. Now I got around to thinking about error handling and would like to catch panics and whatnots and redirect to a static error page. What is the best way to do it with go?
The brute force approach I have been pondering first is to just redirect when an error is detected but it would be nice with a generic catch-all also. I didn't find anything in the docs about it.
(Also thinking about 404s along the same lines.)
There's a great write up on the golang blog about error handling and it specifically covers web related errors towards the end.
http://blog.golang.org/2011/07/error-handling-and-go.html
Basically, you wrap the regular http handlers with your own that can return an error. The wrapper checks if your new handlers return an error and if so react. I use something similar to this where i also call recover. Here's a snippet of the code I use (which is free and open).
While I dont yet provide a custom page for errors, it's been on my mind and should be trivial to add.
// Error is the expected return of a dae.Handler, or nil otherwise.
type Error struct {
Error error
Code int
Message string
}
// NewError is a helper for creating an Error pointer.
func NewError(err error, code int, msg string) *Error {
return &Error{err, code, msg}
}
// Handler is used to cast functions to its type to implement ServeHTTP.
// Code that panics is automatically recovered and delivers a server 500 error.
type Handler func(http.ResponseWriter, *http.Request) *Error
// NewHandler is a helper to chain multiple functions together.
func New(fn ...Handler) Handler {
if len(fn) == 0 {
panic("No Handlers passed in.")
}
h := Handler(fn[0])
for i := 1; i < len(fn); i++ {
h = h.Add(fn[i])
}
return h
}
// ServeHTTP implements the http.Handler interface. If an appHandler returns an
// error, the error is inspected and an appropriate response is written out.
func (fn Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer func() {
if r := recover(); r != nil {
log.Printf("%v", r)
http.Error(w, "A serious error has occured.", 500)
if Debug {
panic(r.(error))
}
}
}()
if e := fn(w, r); e != nil {
log.Printf("Code: %v, Message: \"%s\", Error: %v", e.Code, e.Message, e.Error)
switch e.Code {
case 500:
http.Error(w, e.Message, e.Code)
case 404:
http.NotFound(w, r)
fmt.Fprint(w, e.Message)
case 200:
fmt.Fprint(w, e.Message)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With