-
Notifications
You must be signed in to change notification settings - Fork 32
/
grpcresponse.go
56 lines (44 loc) · 1.05 KB
/
grpcresponse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"bytes"
"encoding/json"
"io"
"net/http"
)
func handleGRPCResponse(resp *http.Response) (*http.Response, error) {
// After Body.Read has returned io.EOF, Trailer will contain
// any trailer values sent by the server.
body := bytes.NewBuffer(nil)
_, err := io.Copy(body, resp.Body)
if err != nil {
return nil, err
}
code := metadata(resp, headerGRPCStatusCode)
if code != "0" && code != "" {
r := struct {
Error string `json:"error"`
Code string `json:"code"`
}{
Error: metadata(resp, headerGRPCMessage),
Code: code,
}
buff := bytes.NewBuffer(nil)
_ = json.NewEncoder(buff).Encode(r)
resp.StatusCode = 500
resp.Body = io.NopCloser(buff)
return resp, nil
}
// drop the first 5 bytes of the response body
prefix := make([]byte, 5)
_, _ = body.Read(prefix)
resp.Body = io.NopCloser(body)
resp.Header.Del(headerContentLength)
return resp, nil
}
func metadata(resp *http.Response, field string) string {
v := resp.Header.Get(field)
if v != "" {
return v
}
return resp.Trailer.Get(field)
}