Skip to content

Commit 0b68aaa

Browse files
committed
Return error on 202 responses
- Start implementing the retry/poll flow
1 parent 85f79ed commit 0b68aaa

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

cmd/ghcs/create.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"strings"
9+
"time"
910

1011
"github.com/AlecAivazis/survey/v2"
1112
"github.com/fatih/camelcase"
@@ -87,8 +88,20 @@ func create(opts *createOptions) error {
8788

8889
log.Println("Creating your codespace...")
8990

90-
codespace, err := apiClient.CreateCodespace(ctx, userResult.User, repository, machine, branch, locationResult.Location)
91+
codespace, err := apiClient.CreateCodespace(
92+
ctx, userResult.User, repository, machine, branch, locationResult.Location,
93+
)
9194
if err != nil {
95+
if err == api.ErrCreateAsyncRetry {
96+
createRetryCtx, cancelRetry := context.WithTimeout(ctx, 2*time.Minute)
97+
defer cancelRetry()
98+
99+
codespace, err = pollForProvisionedCodespace(createRetryCtx, codespace)
100+
if err != nil {
101+
return fmt.Errorf("error creating codespace after retry: %w", err)
102+
}
103+
}
104+
92105
return fmt.Errorf("error creating codespace: %w", err)
93106
}
94107

@@ -105,6 +118,10 @@ func create(opts *createOptions) error {
105118
return nil
106119
}
107120

121+
func pollForProvisionedCodespace(ctx context.Context, provisioningCodespace *api.Codespace) (*api.Codespace, error) {
122+
return nil, nil
123+
}
124+
108125
// showStatus polls the codespace for a list of post create states and their status. It will keep polling
109126
// until all states have finished. Once all states have finished, we poll once more to check if any new
110127
// states have been introduced and stop polling otherwise.

internal/api/api.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,8 @@ type createCodespaceRequest struct {
401401
SkuName string `json:"sku_name"`
402402
}
403403

404+
var ErrCreateAsyncRetry = errors.New("initial creation failed, retrying async")
405+
404406
func (a *API) CreateCodespace(ctx context.Context, user *User, repository *Repository, sku, branch, location string) (*Codespace, error) {
405407
requestBody, err := json.Marshal(createCodespaceRequest{repository.ID, branch, location, sku})
406408
if err != nil {
@@ -424,8 +426,11 @@ func (a *API) CreateCodespace(ctx context.Context, user *User, repository *Repos
424426
return nil, fmt.Errorf("error reading response body: %w", err)
425427
}
426428

427-
if resp.StatusCode > http.StatusAccepted {
429+
switch {
430+
case resp.StatusCode > http.StatusAccepted:
428431
return nil, jsonErrorResponse(b)
432+
case resp.StatusCode == http.StatusAccepted:
433+
return nil, ErrCreateAsyncRetry
429434
}
430435

431436
var response Codespace

0 commit comments

Comments
 (0)