Skip to content

Commit 70a24b0

Browse files
authored
fix(worker): improve error message + fix retry on cdnDownloadItem (#5838)
1 parent 65c6e95 commit 70a24b0

File tree

5 files changed

+25
-20
lines changed

5 files changed

+25
-20
lines changed

engine/worker/cmd_cache.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,13 @@ func cachePushCmd() func(cmd *cobra.Command, args []string) {
144144
if resp.StatusCode >= 300 {
145145
body, err := ioutil.ReadAll(resp.Body)
146146
if err != nil {
147-
sdk.Exit("cache push HTTP error %v", err)
147+
sdk.Exit("cache push HTTP %d error %v", resp.StatusCode, err)
148148
}
149-
cdsError := sdk.DecodeError(body)
150-
sdk.Exit("Error: http code %d : %v", resp.StatusCode, cdsError)
149+
var sdkErr sdk.Error
150+
if json.Unmarshal(body, &sdkErr); err != nil {
151+
sdk.Exit("unable to read error: %s: %v", string(body), err)
152+
}
153+
sdk.Exit("%v", sdkErr)
151154
}
152155

153156
fmt.Printf("Worker cache push with success (tag: %s)\n", args[0])
@@ -230,10 +233,13 @@ func cachePullCmd() func(cmd *cobra.Command, args []string) {
230233
if resp.StatusCode >= 300 {
231234
body, err := ioutil.ReadAll(resp.Body)
232235
if err != nil {
233-
sdk.Exit("cache pull HTTP error %v", err)
236+
sdk.Exit("cache pull HTTP %d error %v", resp.StatusCode, err)
237+
}
238+
var sdkErr sdk.Error
239+
if json.Unmarshal(body, &sdkErr); err != nil {
240+
sdk.Exit("unable to read error: %s: %v", string(body), err)
234241
}
235-
cdsError := sdk.DecodeError(body)
236-
sdk.Exit("Error: %v", cdsError)
242+
sdk.Exit("%v", sdkErr)
237243
}
238244

239245
fmt.Printf("Worker cache pull with success (tag: %s)\n", args[0])

engine/worker/internal/handler_cache.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func cachePullHandler(ctx context.Context, wk *CurrentWorker) http.HandlerFunc {
211211
r, err = wk.client.CDNItemDownload(ctx, wk.cdnHttpAddr, items.Items[0].APIRefHash, sdk.CDNTypeItemWorkerCache)
212212
if err != nil {
213213
err = sdk.Error{
214-
Message: "worker cache pull > Cannot pull cache: " + err.Error(),
214+
Message: "Cannot pull cache: " + err.Error(),
215215
Status: http.StatusNotFound,
216216
}
217217
log.Error(ctx, "%v", err)

sdk/cdsclient/client_cdn.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cdsclient
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"io"
78
"io/ioutil"
@@ -18,16 +19,22 @@ func (c *client) CDNItemDownload(ctx context.Context, cdnAddr string, hash strin
1819
auth := "Bearer " + c.config.SessionToken
1920
req.Header.Add("Authorization", auth)
2021
})
22+
if err != nil {
23+
return nil, err
24+
}
2125
if code >= 400 {
2226
var stringBody string
2327
if reader != nil {
2428
body, _ := ioutil.ReadAll(reader)
25-
if err := sdk.DecodeError(body); err != nil {
26-
return nil, err
29+
var errSdk sdk.Error
30+
if err := json.Unmarshal(body, &errSdk); err == nil && errSdk.Message != "" {
31+
stringBody = errSdk.Error()
32+
}
33+
if stringBody == "" {
34+
stringBody = string(body)
2735
}
28-
stringBody = string(body)
2936
}
30-
return nil, newAPIError(fmt.Errorf("HTTP %d: %s", code, stringBody))
37+
return nil, newAPIError(fmt.Errorf("%s", stringBody))
3138
}
3239
return reader, err
3340
}

sdk/cdsclient/http.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -301,18 +301,11 @@ func (c *client) Stream(ctx context.Context, httpClient HTTPClient, method strin
301301
c.config.SessionToken = ""
302302
}
303303

304-
if resp.StatusCode == 409 || resp.StatusCode > 500 {
304+
if resp.StatusCode == 409 || resp.StatusCode >= 500 {
305305
time.Sleep(250 * time.Millisecond)
306306
savederror = extractBodyErrorFromResponse(resp)
307307
continue
308308
}
309-
310-
// if no request error by status > 500, check CDS error
311-
// if there is a CDS errors, return it
312-
if resp.StatusCode == 500 {
313-
return nil, resp.Header, resp.StatusCode, extractBodyErrorFromResponse(resp)
314-
}
315-
316309
return resp.Body, resp.Header, resp.StatusCode, nil
317310
}
318311

sdk/error.go

-1
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,6 @@ func ExtractHTTPError(source error) Error {
719719
if httpError.Message == "" {
720720
httpError.Message = httpError.Translate()
721721
}
722-
723722
return httpError
724723
}
725724

0 commit comments

Comments
 (0)