Skip to content

Commit

Permalink
Wrap errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed May 6, 2024
1 parent 5b981a0 commit 70bc793
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions pocketic/do.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ func (pic PocketIC) do(method, url string, input, output any) error {
// No need to decode the response body.
return nil
}
return json.NewDecoder(resp.Body).Decode(output)
if err := json.NewDecoder(resp.Body).Decode(output); err != nil {
return fmt.Errorf("failed to decode response body: %w", err)
}
case http.StatusAccepted:
var response startedOrBusyResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return err
return fmt.Errorf("failed to decode accepted response body: %w", err)
}
pic.logger.Printf("[POCKETIC] Accepted: %s %s", response.StateLabel, response.OpID)
if method == http.MethodGet {
Expand Down Expand Up @@ -63,33 +65,35 @@ func (pic PocketIC) do(method, url string, input, output any) error {
return err
}
switch resp.StatusCode {
case http.StatusOK:
case http.StatusOK, http.StatusCreated:
if resp.Body == nil || output == nil {
// No need to decode the response body.
return nil
}
return json.NewDecoder(resp.Body).Decode(output)
if err := json.NewDecoder(resp.Body).Decode(output); err != nil {
return fmt.Errorf("failed to decode response body: %w", err)
}
case http.StatusAccepted, http.StatusConflict:
default:
var errResp ErrorMessage
if err := json.NewDecoder(resp.Body).Decode(&errResp); err != nil {
return err
return fmt.Errorf("failed to decode accepted/conflict response body: %w", err)
}
return errResp
}
}
case http.StatusConflict:
var response startedOrBusyResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return err
return fmt.Errorf("failed to decode conflict response body: %w", err)
}
pic.logger.Printf("[POCKETIC] Conflict: %s %s", response.StateLabel, response.OpID)
time.Sleep(pic.delay) // Retry after a short delay.
continue
default:
var errResp ErrorMessage
if err := json.NewDecoder(resp.Body).Decode(&errResp); err != nil {
return err
return fmt.Errorf("failed to decode error response body: %w", err)
}
return errResp
}
Expand Down

0 comments on commit 70bc793

Please sign in to comment.