...

Source file src/github.com/bluekeyes/hatpear/example_test.go

Documentation: github.com/bluekeyes/hatpear

     1  package hatpear_test
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"github.com/bluekeyes/hatpear"
     9  )
    10  
    11  func Example() {
    12  	std := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    13  		err := errors.New("this failed!")
    14  		hatpear.Store(r, err)
    15  	})
    16  
    17  	pear := hatpear.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
    18  		return errors.New("this also failed!")
    19  	})
    20  
    21  	mux := http.NewServeMux()
    22  	mux.Handle("/std", std)
    23  	mux.Handle("/hatpear", hatpear.Try(pear))
    24  
    25  	catch := hatpear.Catch(func(w http.ResponseWriter, r *http.Request, err error) {
    26  		fmt.Printf("[ERROR]: %s\n", err.Error())
    27  		http.Error(w, err.Error(), http.StatusInternalServerError)
    28  	})
    29  
    30  	http.ListenAndServe(":8000", catch(mux))
    31  }
    32  

View as plain text