Example #1
0
func ExampleWrite() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		// Response data which can be marshal to json.
		oh.WriteData(nil, w, r, map[string]interface{}{
			"version": "1.0",
			"count":   100,
		})
	})

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		// Response unknown error with HTTP/500
		oh.WriteError(nil, w, r, fmt.Errorf("System error"))
	})

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		// Response known error {code:xx}
		oh.WriteError(nil, w, r, oh.SystemError(100))
	})

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		// Response known complex error {code:xx,data:"xxx"}
		oh.WriteError(nil, w, r, oh.SystemComplexError{oh.SystemError(100), "Error description"})
	})

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		// Response known complex error {code:xx,data:"xxx"}
		oh.WriteCplxError(nil, w, r, oh.SystemError(100), "Error description")
	})
}
Example #2
0
func ExampleHttpTest_Error() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		// Response unknown error with HTTP/500
		oh.Error(nil, fmt.Errorf("System error")).ServeHTTP(w, r)
	})

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		// Response known error {code:xx}
		oh.Error(nil, oh.SystemError(100)).ServeHTTP(w, r)
	})

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		// Response known complex error {code:xx,data:"xxx"}
		oh.Error(nil, oh.SystemComplexError{oh.SystemError(100), "Error description"}).ServeHTTP(w, r)
	})

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		// Response known complex error {code:xx,data:"xxx"}
		oh.CplxError(nil, oh.SystemError(100), "Error description").ServeHTTP(w, r)
	})
}