Skip to content

Commit 0bf3b4c

Browse files
authored
Merge pull request #666 from kzys/har-error
Make http-address-resolver's errors more descriptive
2 parents 34263c0 + 7d5e5fd commit 0bf3b4c

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

snapshotter/demux/proxy/address/http_resolver.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ func requestURL(url string, namespace string) string {
4747

4848
// Get queries the proxy network type and address for the specified namespace.
4949
func (h HTTPResolver) Get(namespace string) (Response, error) {
50-
httpResponse, err := h.client.Get(requestURL(h.url, namespace))
50+
url := requestURL(h.url, namespace)
51+
52+
httpResponse, err := h.client.Get(url)
5153
if err != nil {
5254
return Response{}, err
5355
}
@@ -58,11 +60,15 @@ func (h HTTPResolver) Get(namespace string) (Response, error) {
5860
return Response{}, err
5961
}
6062

63+
code := httpResponse.StatusCode
64+
if code != 200 {
65+
return Response{}, fmt.Errorf("failed to GET %s: status=%d, body=%s", url, code, body)
66+
}
67+
6168
var response Response
6269
err = json.Unmarshal(body, &response)
6370
if err != nil {
6471
return Response{}, err
6572
}
66-
6773
return response, nil
6874
}

snapshotter/demux/proxy/address/http_resolver_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,12 @@ func happyPath() error {
6969
"test2": "label2"
7070
}
7171
}`), err: io.EOF}
72-
client := mockClient{getError: nil, getResponse: http.Response{Body: &reader}}
72+
client := mockClient{getError: nil, getResponse: http.Response{StatusCode: 200, Body: &reader}}
7373
uut := HTTPResolver{url: "localhost:10001", client: &client}
7474

7575
actual, err := uut.Get("namespace")
7676
if err != nil {
77-
fmt.Println(err)
78-
return errors.New("expected no error from HTTP resolver")
77+
return fmt.Errorf("expected no error from HTTP resolver, but got %+v", err)
7978
}
8079

8180
if actual.Network != "unix" {

snapshotter/internal/http_address_resolver.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,45 +107,44 @@ func queryAddress(writ http.ResponseWriter, req *http.Request) {
107107
writ.Header().Set("Content-Type", "application/json")
108108

109109
keys, ok := req.URL.Query()["namespace"]
110-
111110
if !ok {
112111
http.Error(writ, "Missing 'namespace' query", http.StatusBadRequest)
113112
return
114113
}
115114

116-
namespace := keys[0]
117-
118-
fcClient, err := client.New(containerdSockPath + ".ttrpc")
115+
sock := containerdSockPath + ".ttrpc"
116+
fcClient, err := client.New(sock)
119117
if err != nil {
120118
logger.WithError(err).Error("could not create firecracker client")
121-
http.Error(writ, "Internal server error", http.StatusInternalServerError)
119+
http.Error(writ, fmt.Sprintf("failed to connect %q", sock), http.StatusInternalServerError)
122120
return
123121
}
124122
defer fcClient.Close()
125123

124+
namespace := keys[0]
126125
ctx := namespaces.WithNamespace(req.Context(), namespace)
127126
vmInfo, err := fcClient.GetVMInfo(ctx, &proto.GetVMInfoRequest{VMID: namespace})
128127
if err != nil {
129128
logger.WithField("VMID", namespace).WithError(err).Error("unable to retrieve VM Info")
130-
http.Error(writ, "Internal server error", http.StatusInternalServerError)
129+
http.Error(writ, fmt.Sprintf("failed to get VM %q", namespace), http.StatusNotFound)
131130
return
132131
}
133132

134133
writ.WriteHeader(http.StatusOK)
135134

136-
response, err := json.Marshal(proxyaddress.Response{
135+
result := proxyaddress.Response{
137136
Network: "unix",
138137
Address: vmInfo.VSockPath,
139138
SnapshotterPort: strconv.Itoa(remotePort),
140139
MetricsPort: strconv.Itoa(metricsRemotePort),
141140
Labels: map[string]string{
142141
"VMID": namespace,
143142
},
144-
})
143+
}
144+
serialized, err := json.Marshal(&result)
145145
if err != nil {
146-
http.Error(writ, "Internal server error", http.StatusInternalServerError)
146+
http.Error(writ, fmt.Sprintf("failed to marshal %+v", result), http.StatusInternalServerError)
147147
return
148148
}
149-
150-
writ.Write(response)
149+
writ.Write(serialized)
151150
}

0 commit comments

Comments
 (0)