MCPcopy Create free account
hub / github.com/DoNewsCode/core / ExampleResponseEncoder_EncodeError

Function ExampleResponseEncoder_EncodeError

srvhttp/example_test.go:62–83  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

60}
61
62func ExampleResponseEncoder_EncodeError() {
63 handler := func(writer http.ResponseWriter, request *http.Request) {
64 encoder := srvhttp.NewResponseEncoder(writer)
65 encoder.EncodeError(unierr.New(codes.NotFound, "foo is missing"))
66 }
67 req := httptest.NewRequest("GET", "http://example.com/foo", nil)
68 w := httptest.NewRecorder()
69 handler(w, req)
70
71 resp := w.Result()
72 defer resp.Body.Close()
73 body, _ := ioutil.ReadAll(resp.Body)
74
75 fmt.Println(resp.StatusCode)
76 fmt.Println(resp.Header.Get("Content-Type"))
77 fmt.Println(string(body))
78
79 // Output:
80 // 404
81 // application/json; charset=utf-8
82 // {"code":5,"message":"foo is missing"}
83}

Callers

nothing calls this directly

Calls 6

EncodeErrorMethod · 0.95
NewResponseEncoderFunction · 0.92
NewFunction · 0.92
handlerInterface · 0.85
CloseMethod · 0.80
GetMethod · 0.65

Tested by

no test coverage detected