Skip to content

Commit

Permalink
Merge pull request #26 from stealthrocket/api-404
Browse files Browse the repository at this point in the history
Better handle 404's when interacting with Dispatch API
  • Loading branch information
chriso authored Apr 23, 2024
2 parents 00ad544 + f54e21b commit 4065147
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,12 +428,19 @@ func invoke(ctx context.Context, client *http.Client, url, requestID string, bri
}
bridgePostRes, err := client.Do(bridgePostReq)
if err != nil {
return fmt.Errorf("failed to contact Dispatch API or write response: %v", err)
return fmt.Errorf("failed to contact Dispatch API or send response: %v", err)
}
if bridgePostRes.StatusCode != http.StatusAccepted {
return fmt.Errorf("failed to contact Dispatch API: response code %d", bridgePostRes.StatusCode)
switch bridgePostRes.StatusCode {
case http.StatusAccepted:
return nil
case http.StatusNotFound:
// A 404 is expected if there's a timeout upstream that's hit
// before the response can be sent.
slog.Debug("request is no longer available", "request_id", requestID, "method", "post")
return nil
default:
return fmt.Errorf("failed to contact Dispatch API to send response: response code %d", bridgePostRes.StatusCode)
}
return nil
}

func cleanup(ctx context.Context, client *http.Client, url, requestID string) error {
Expand All @@ -450,12 +457,20 @@ func cleanup(ctx context.Context, client *http.Client, url, requestID string) er
}
res, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to contact Dispatch API: %v", err)
return fmt.Errorf("failed to contact Dispatch API to cleanup request: %v", err)
}
if res.StatusCode != http.StatusOK {
return fmt.Errorf("failed to contact Dispatch API: response code %d", res.StatusCode)
switch res.StatusCode {
case http.StatusOK:
return nil
case http.StatusNotFound:
// A 404 can occur if the request is cleaned up concurrently, either
// because a response was received upstream but the CLI didn't realize
// the response went through, or because a timeout was reached upstream.
slog.Debug("request is no longer available", "request_id", requestID, "method", "delete")
return nil
default:
return fmt.Errorf("failed to contact Dispatch API to cleanup request: response code %d", res.StatusCode)
}
return nil
}

func withoutEnv(env []string, prefixes ...string) []string {
Expand Down

0 comments on commit 4065147

Please sign in to comment.