diff --git a/internal/worker/api/api.gen.go b/internal/worker/api/api.gen.go index 5a315d34a..b2bebfcd0 100644 --- a/internal/worker/api/api.gen.go +++ b/internal/worker/api/api.gen.go @@ -10,6 +10,11 @@ import ( "net/http" ) +// Error defines model for Error. +type Error struct { + Message string `json:"message"` +} + // RequestJobJSONBody defines parameters for RequestJob. type RequestJobJSONBody map[string]interface{} diff --git a/internal/worker/api/openapi.yml b/internal/worker/api/openapi.yml index 8f208fba5..e1b132c1f 100644 --- a/internal/worker/api/openapi.yml +++ b/internal/worker/api/openapi.yml @@ -23,6 +23,17 @@ paths: - OK required: - status + 4XX: + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + 5XX: + description: '' + content: + application/json: + schema: + $ref: '#/components/schemas/Error' operationId: GetStatus description: Simple status handler to check whether the service is up. /jobs: @@ -53,6 +64,17 @@ paths: - manifest - location - id + 4XX: + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + 5XX: + description: '' + content: + application/json: + schema: + $ref: '#/components/schemas/Error' operationId: RequestJob requestBody: content: @@ -85,6 +107,18 @@ paths: type: boolean required: - canceled + 4XX: + description: '' + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + 5XX: + description: '' + content: + application/json: + schema: + $ref: '#/components/schemas/Error' operationId: GetJob description: '' patch: @@ -127,6 +161,17 @@ paths: responses: '200': description: OK + 4XX: + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + 5XX: + description: '' + content: + application/json: + schema: + $ref: '#/components/schemas/Error' operationId: UploadJobArtifact requestBody: content: @@ -134,4 +179,12 @@ paths: schema: type: string components: - schemas: {} + schemas: + Error: + title: Error + type: object + properties: + message: + type: string + required: + - message diff --git a/internal/worker/client.go b/internal/worker/client.go index 824ddaee7..8b6db3aed 100644 --- a/internal/worker/client.go +++ b/internal/worker/client.go @@ -5,7 +5,6 @@ import ( "context" "crypto/tls" "encoding/json" - "errors" "fmt" "io" "net" @@ -94,9 +93,7 @@ func (c *Client) RequestJob() (Job, error) { defer response.Body.Close() if response.StatusCode != http.StatusCreated { - var er errorResponse - _ = json.NewDecoder(response.Body).Decode(&er) - return nil, fmt.Errorf("couldn't create job, got %d: %s", response.StatusCode, er.Message) + return nil, errorFromResponse(response, "error requesting job") } var jr requestJobResponse @@ -157,7 +154,7 @@ func (j *job) Update(status common.ImageBuildState, result *osbuild.Result) erro defer response.Body.Close() if response.StatusCode != http.StatusOK { - return errors.New("error setting job status") + return errorFromResponse(response, "error setting job status") } return nil @@ -171,7 +168,7 @@ func (j *job) Canceled() (bool, error) { defer response.Body.Close() if response.StatusCode != http.StatusOK { - return false, fmt.Errorf("unexpected return value: %v", response.StatusCode) + return false, errorFromResponse(response, "error fetching job info") } var jr getJobResponse @@ -205,10 +202,26 @@ func (j *job) UploadArtifact(name string, reader io.Reader) error { req.Header.Add("Content-Type", "application/octet-stream") - _, err = j.requester.Do(req) + response, err := j.requester.Do(req) if err != nil { - return fmt.Errorf("error uploading artifcat: %v", err) + return fmt.Errorf("error uploading artifact: %v", err) + } + + if response.StatusCode != 200 { + return errorFromResponse(response, "error uploading artifact") } return nil } + +// Parses an api.Error from a response and returns it as a golang error. Other +// errors, such failing to parse the response, are returned as golang error as +// well. If client code expects an error, it gets one. +func errorFromResponse(response *http.Response, message string) error { + var e api.Error + err := json.NewDecoder(response.Body).Decode(&e) + if err != nil { + return fmt.Errorf("failed to parse error response: %v", err) + } + return fmt.Errorf("%v: %v — %v", message, response.StatusCode, e.Message) +} diff --git a/internal/worker/json.go b/internal/worker/json.go index 5e50feb22..60f95fc5c 100644 --- a/internal/worker/json.go +++ b/internal/worker/json.go @@ -29,10 +29,6 @@ type statusResponse struct { Status string `json:"status"` } -type errorResponse struct { - Message string `json:"message"` -} - type requestJobResponse struct { Id uuid.UUID `json:"id"` Manifest distro.Manifest `json:"manifest"`