Skip to content

Commit affb69e

Browse files
authored
fix: task file uploads handling of HTTP and GraphQL errors (#92)
1 parent af7244e commit affb69e

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

api/lagoon/client/tasks.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,31 @@ func (c *Client) UploadFilesForTask(ctx context.Context,
138138
return fmt.Errorf("couldn't read response from API: %w", err)
139139
}
140140

141+
type apiErrors struct {
142+
Message string `json:"message"`
143+
}
144+
145+
type apiResponse struct {
146+
Errors []apiErrors `json:"errors"`
147+
Data map[string]any `json:"data"`
148+
}
149+
141150
// unmarshal the response into a map
142-
var respDat map[string]map[string]interface{}
143-
err = json.Unmarshal([]byte(bodyText), &respDat)
151+
var apiResp apiResponse
152+
err = json.Unmarshal([]byte(bodyText), &apiResp)
144153
if err != nil {
154+
if resp.StatusCode != http.StatusOK {
155+
return fmt.Errorf("graphql: server returned a non-200 status code: %v", resp.StatusCode)
156+
}
145157
return fmt.Errorf("couldn't unmarshal response from API: %w", err)
146158
}
147159

160+
if len(apiResp.Errors) > 0 {
161+
return fmt.Errorf("api error: %v", apiResp.Errors[0])
162+
}
163+
148164
// then extract our specific uploadFilesForTask data
149-
b, _ := json.Marshal(respDat["data"]["uploadFilesForTask"])
165+
b, _ := json.Marshal(apiResp.Data["uploadFilesForTask"])
150166
err = json.Unmarshal([]byte(b), out)
151167
if err != nil {
152168
return fmt.Errorf("couldn't unmarshal response from API: %w", err)

0 commit comments

Comments
 (0)