Created
March 12, 2017 18:29
-
-
Save nbari/c0b9bcfbaddf21af5d4dc9cf2bb96aa2 to your computer and use it in GitHub Desktop.
violetear prototype for a centralized HTTP error handling
This file contains 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
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"time" | |
v "github.com/nbari/violetear" | |
) | |
func handleHello(w http.ResponseWriter, r *http.Request) error { | |
t := time.Now() | |
if t.Second()%2 != 0 { | |
return v.Error{500, fmt.Errorf("Test error %d", t.Unix())} | |
} | |
fmt.Fprintf(w, "Hi, %s!", t) | |
return nil | |
} | |
func main() { | |
router := v.New() | |
router.Handle( | |
"/hello", | |
v.ErrorHandler(handleHello), | |
"GET, HEAD", | |
) | |
log.Fatal(http.ListenAndServe(":8000", router)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment