Skip to content

Instantly share code, notes, and snippets.

@nbari
Created March 12, 2017 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbari/c0b9bcfbaddf21af5d4dc9cf2bb96aa2 to your computer and use it in GitHub Desktop.
Save nbari/c0b9bcfbaddf21af5d4dc9cf2bb96aa2 to your computer and use it in GitHub Desktop.
violetear prototype for a centralized HTTP error handling
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