コード例 #1
0
func Example_httpPanic() {
	hdlr := func(w http.ResponseWriter, req *http.Request) error {
		panic(ehttp.NewErrorf(http.StatusTeapot, "big fail"))
	}
	http.HandleFunc("/", ehttp.MWErrorPanic(hdlr))
	log.Fatal(http.ListenAndServe(":8080", nil))
}
コード例 #2
0
func ExampleServeMux_customized() {
	// Define our error format and how to expose it to the client.
	type customError struct {
		Error    string `json:"error"`
		HTTPCode int    `json:"http_code"`
	}
	errorHandler := func(w ehttp.ResponseWriter, req *http.Request, err error) {
		_ = json.NewEncoder(w).Encode(customError{
			Error:    err.Error(),
			HTTPCode: w.Code(),
		})
	}

	// Define a cutom logger for unexpected events (double header send).
	logger := log.New(os.Stderr, "", log.LstdFlags)

	// Create the mux.
	mux := ehttp.NewServeMux(errorHandler, "application/text; charset=utf-8", false, logger)

	// Register the handler.
	mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) error {
		// Return an error.
		return ehttp.NewErrorf(http.StatusTeapot, "fail")
	})

	// Start serve the mux.
	log.Fatal(http.ListenAndServe(":8080", mux))
}
コード例 #3
0
func Example_panic() {
	hdlr := func(w http.ResponseWriter, req *http.Request, p httprouter.Params) error {
		panic(ehttp.NewErrorf(http.StatusTeapot, "fail"))
	}
	router := httprouter.New()
	router.GET("/", ehttprouter.MWErrorPanic(hdlr))
	log.Fatal(http.ListenAndServe(":8080", router))
}
コード例 #4
0
func Example_gorilla() {
	hdlr := func(w http.ResponseWriter, req *http.Request) error {
		return ehttp.NewErrorf(http.StatusTeapot, "fail")
	}
	router := mux.NewRouter()
	router.HandleFunc("/", ehttp.MWError(hdlr))
	log.Fatal(http.ListenAndServe(":8080", router))
}
コード例 #5
0
func Example_customErrorHandler() {
	// Define how to send errors to the user.
	errorHandler := func(w ehttp.ResponseWriter, req *http.Request, err error) {
		fmt.Fprintf(w, "<<<<<<%s>>>>>>", err)
	}
	mux := ehttp.NewServeMux(errorHandler, "application/text; charset=utf-8", false, nil)
	mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) error {
		return ehttp.NewErrorf(http.StatusTeapot, "fail")
	})
	log.Fatal(http.ListenAndServe(":8080", mux))
}
コード例 #6
0
func TestMWErrorPanicEHTTP(t *testing.T) {
	hdlr := func(w http.ResponseWriter, req *http.Request, p httprouter.Params) error {
		panic(ehttp.NewErrorf(http.StatusTeapot, "fail"))
	}
	router := httprouter.New()
	router.GET("/", MWErrorPanic(hdlr))

	ts := httptest.NewServer(router)
	defer ts.Close()

	resp, err := http.Get(ts.URL)
	if err != nil {
		t.Fatal(err)
	}
	assertInt(t, http.StatusTeapot, resp.StatusCode)
	body, err := ioutil.ReadAll(resp.Body)
	_ = resp.Body.Close()
	if err != nil {
		t.Fatal(err)
	}
	assertJSONError(t, "fail", string(body))
}
コード例 #7
0
func TestWrappedHelperMethods(t *testing.T) {
	router := New(nil, "", true, nil)

	testHandler := func(w http.ResponseWriter, req *http.Request, params httprouter.Params) error {
		return ehttp.NewErrorf(http.StatusTeapot, "fail")
	}

	methods := map[string]func(string, Handle){
		"GET":     router.GET,
		"DELETE":  router.DELETE,
		"OPTIONS": router.OPTIONS,
		"PATCH":   router.PATCH,
		"POST":    router.POST,
		"PUT":     router.PUT,
	}
	for _, method := range methods {
		method("/", testHandler)
	}

	ts := httptest.NewServer(router)
	defer ts.Close()

	for methodName := range methods {
		req, err := http.NewRequest(methodName, ts.URL, nil)
		if err != nil {
			t.Fatalf("Error creating new request for method %s: %s", methodName, err)
		}
		resp, err := http.DefaultClient.Do(req)
		if err != nil {
			t.Fatalf("Error connecting to test server for method %s: %s", methodName, err)
		}
		body, err := ioutil.ReadAll(resp.Body)
		_ = resp.Body.Close()
		if err != nil {
			t.Fatalf("Error reading response from test server for method %s: %s", methodName, err)
		}
		assertInt(t, http.StatusTeapot, resp.StatusCode)
		assertString(t, "fail", string(body))
		if t.Failed() {
			t.Logf("Assert error for method %s", methodName)
			break
		}
	}

	// Specicif test for HEAD.
	router = New(nil, "", false, nil)
	ch := make(chan struct{}, 1)
	defer close(ch)
	testHandler = func(w http.ResponseWriter, req *http.Request, params httprouter.Params) error {
		ch <- struct{}{}
		return ehttp.NewErrorf(http.StatusTeapot, "fail")
	}
	router.HEAD("/", testHandler)
	ts2 := httptest.NewServer(router)
	defer ts2.Close()

	resp, err := http.Head(ts2.URL)
	if err != nil {
		t.Fatalf("Error connecting to test server for method %s: %s", "HEAD", err)
	}
	assertInt(t, http.StatusTeapot, resp.StatusCode)

	timer := time.NewTimer(2 * time.Second)
	defer timer.Stop()
	select {
	case <-timer.C:
		t.Fatal("Timeout waiting fro the HEAD call")
	case <-ch:
	}
}