Last active
August 29, 2015 14:24
-
-
Save karlmutch/47417bfdec8e6bb342ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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