Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/api/api/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,12 @@ func (s *ApiService) RestoreInstance(ctx context.Context, request oapi.RestoreIn
result, err := s.InstanceManager.RestoreInstance(ctx, inst.Id)
if err != nil {
switch {
case errors.Is(err, context.Canceled):
log.DebugContext(ctx, "restore request canceled")
return oapi.RestoreInstance499JSONResponse{
Code: "client_closed_request",
Message: "request canceled",
}, nil
case errors.Is(err, instances.ErrNotFound):
return oapi.RestoreInstance404JSONResponse{
Code: "not_found",
Expand Down
49 changes: 46 additions & 3 deletions cmd/api/api/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -899,9 +899,7 @@ func TestCreateInstance_ErrorStatusMapping(t *testing.T) {
}

// errActionInstanceManager is a fake whose lifecycle actions always fail with a
// preset error, used to assert action handlers map a deleted-image
// images.ErrNotFound (resolved at action time, e.g. start/restore/fork of a
// stopped instance whose image was removed) to a 404 instead of a blanket 500.
// preset error.
type errActionInstanceManager struct {
instances.Manager
err error
Expand All @@ -923,6 +921,51 @@ func (m *errActionInstanceManager) RestoreSnapshot(context.Context, string, stri
return nil, m.err
}

func TestRestoreInstance_ErrorMapping(t *testing.T) {
t.Parallel()

resolved := &instances.Instance{
StoredMetadata: instances.StoredMetadata{Id: "inst-restore-error"},
}
tests := []struct {
name string
err error
wantResponse oapi.RestoreInstanceResponseObject
}{
{
name: "client cancellation -> 499",
err: fmt.Errorf("restore from snapshot: %w", context.Canceled),
wantResponse: oapi.RestoreInstance499JSONResponse{
Code: "client_closed_request",
Message: "request canceled",
},
},
{
name: "internal failure -> 500",
err: fmt.Errorf("restore from snapshot: hypervisor failed"),
wantResponse: oapi.RestoreInstance500JSONResponse{
Code: "internal_error",
Message: "failed to restore instance",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
svc := newTestService(t)
svc.InstanceManager = &errActionInstanceManager{Manager: svc.InstanceManager, err: tt.err}

resp, err := svc.RestoreInstance(
mw.WithResolvedInstance(ctx(), resolved.Id, resolved),
oapi.RestoreInstanceRequestObject{Id: resolved.Id},
)
require.NoError(t, err)
require.Equal(t, tt.wantResponse, resp)
})
}
}

func TestInstanceActions_ImageNotFoundMapsTo404(t *testing.T) {
t.Parallel()

Expand Down
113 changes: 65 additions & 48 deletions lib/oapi/oapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2995,6 +2995,12 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/Error"
"499":
description: Client closed request
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
500:
description: Internal server error
content:
Expand Down
Loading