Skip to content

Instantly share code, notes, and snippets.

@karlmutch
Last active August 29, 2015 14:24
Show Gist options
  • Save karlmutch/47417bfdec8e6bb342ab to your computer and use it in GitHub Desktop.
Save karlmutch/47417bfdec8e6bb342ab to your computer and use it in GitHub Desktop.
func init() {
http.HandleFunc("/", errorHandler(betterHandler))
}
func errorHandler(f func(http.ResponseWriter, *http.Request) error) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
err := f(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Printf("handling %q: %v", r.RequestURI, err)
}
}
}
func betterHandler(w http.ResponseWriter, r *http.Request) error {
if err := doThis(); err != nil {
return fmt.Errorf("doing this: %v", err)
}
if err := doThat(); err != nil {
return fmt.Errorf("doing that: %v", err)
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment