Skip to content

Commit

Permalink
fix: correctly propagate errors whenever the fetcher fails to downloa…
Browse files Browse the repository at this point in the history
…d the artifacts (#15758)
  • Loading branch information
agparadiso authored Dec 19, 2024
1 parent 37d3081 commit d5e2184
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
4 changes: 4 additions & 0 deletions core/services/workflows/syncer/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,9 @@ func (s *FetcherService) Fetch(ctx context.Context, url string) ([]byte, error)
return nil, err
}

if payload.ExecutionError {
return nil, fmt.Errorf("execution error from gateway: %s", payload.ErrorMessage)
}

return payload.Body, nil
}
34 changes: 34 additions & 0 deletions core/services/workflows/syncer/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,40 @@ func TestNewFetcherService(t *testing.T) {
expectedPayload := []byte("response body")
require.Equal(t, expectedPayload, payload)
})

t.Run("NOK-response_payload_too_large", func(t *testing.T) {
headers := map[string]string{"Content-Type": "application/json"}
responsePayload, err := json.Marshal(ghcapabilities.Response{
StatusCode: 400,
Headers: headers,
ErrorMessage: "http: request body too large",
ExecutionError: true,
})
require.NoError(t, err)
gatewayResponse := &api.Message{
Body: api.MessageBody{
MessageId: msgID,
Method: ghcapabilities.MethodWebAPITarget,
Payload: responsePayload,
},
}

connector.EXPECT().AddHandler([]string{capabilities.MethodWorkflowSyncer}, mock.Anything).Return(nil)

fetcher := NewFetcherService(lggr, wrapper)
require.NoError(t, fetcher.Start(ctx))
defer fetcher.Close()

connector.EXPECT().SignAndSendToGateway(mock.Anything, "gateway1", mock.Anything).Run(func(ctx context.Context, gatewayID string, msg *api.MessageBody) {
fetcher.och.HandleGatewayMessage(ctx, "gateway1", gatewayResponse)
}).Return(nil).Times(1)
connector.EXPECT().DonID().Return("don-id")
connector.EXPECT().AwaitConnection(matches.AnyContext, "gateway1").Return(nil)
connector.EXPECT().GatewayIDs().Return([]string{"gateway1", "gateway2"})

_, err = fetcher.Fetch(ctx, url)
require.Error(t, err, "execution error from gateway: http: request body too large")
})
}

func gatewayResponse(t *testing.T, msgID string) *api.Message {
Expand Down
6 changes: 3 additions & 3 deletions core/services/workflows/syncer/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,19 +497,19 @@ func (h *eventHandler) getWorkflowArtifacts(
if err != nil {
binary, err2 := h.fetcher(ctx, payload.BinaryURL)
if err2 != nil {
return nil, nil, fmt.Errorf("failed to fetch binary from %s : %w", payload.BinaryURL, err)
return nil, nil, fmt.Errorf("failed to fetch binary from %s : %w", payload.BinaryURL, err2)
}

decodedBinary, err2 := base64.StdEncoding.DecodeString(string(binary))
if err2 != nil {
return nil, nil, fmt.Errorf("failed to decode binary: %w", err)
return nil, nil, fmt.Errorf("failed to decode binary: %w", err2)
}

var config []byte
if payload.ConfigURL != "" {
config, err2 = h.fetcher(ctx, payload.ConfigURL)
if err2 != nil {
return nil, nil, fmt.Errorf("failed to fetch config from %s : %w", payload.ConfigURL, err)
return nil, nil, fmt.Errorf("failed to fetch config from %s : %w", payload.ConfigURL, err2)
}
}
return decodedBinary, config, nil
Expand Down

0 comments on commit d5e2184

Please sign in to comment.