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
51 changes: 0 additions & 51 deletions Makefile

This file was deleted.

20 changes: 20 additions & 0 deletions openshell/v1/context_errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package v1

import "context"

func contextError(err error) error {
if err == nil {
return nil
}
switch err {
case context.DeadlineExceeded:
return &StatusError{Code: ErrorDeadlineExceeded, Message: err.Error(), Cause: err}
case context.Canceled:
return &StatusError{Code: ErrorCancelled, Message: err.Error(), Cause: err}
default:
return &StatusError{Code: ErrorInternal, Message: err.Error(), Cause: err}
}
}
49 changes: 49 additions & 0 deletions openshell/v1/context_errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package v1

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestContextError_Nil(t *testing.T) {
result := contextError(nil)
assert.Nil(t, result)
}

func TestContextError_DeadlineExceeded(t *testing.T) {
result := contextError(context.DeadlineExceeded)

require.Error(t, result)
var se *StatusError
require.True(t, errors.As(result, &se))
assert.Equal(t, ErrorDeadlineExceeded, se.Code)
assert.True(t, errors.Is(result, context.DeadlineExceeded))
}

func TestContextError_Canceled(t *testing.T) {
result := contextError(context.Canceled)

require.Error(t, result)
var se *StatusError
require.True(t, errors.As(result, &se))
assert.Equal(t, ErrorCancelled, se.Code)
assert.True(t, errors.Is(result, context.Canceled))
}

func TestContextError_Default(t *testing.T) {
orig := errors.New("unexpected context error")
result := contextError(orig)

require.Error(t, result)
var se *StatusError
require.True(t, errors.As(result, &se))
assert.Equal(t, ErrorInternal, se.Code)
assert.True(t, errors.Is(result, orig))
}
6 changes: 4 additions & 2 deletions openshell/v1/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ func TestStatusError_Error(t *testing.T) {
assert.Contains(t, s, "sandbox not found")
}

func TestStatusError_ErrorWithDetails(t *testing.T) {
func TestStatusError_ErrorWithCause(t *testing.T) {
cause := errors.New("underlying error")
err := &StatusError{
Code: ErrorInvalidArgument,
Message: "bad name",
Details: map[string]string{"field": "name"},
Cause: cause,
}
s := err.Error()
assert.Contains(t, s, "InvalidArgument")
assert.Contains(t, s, "bad name")
assert.Equal(t, cause, errors.Unwrap(err))
}

func TestIsNotFound(t *testing.T) {
Expand Down
10 changes: 9 additions & 1 deletion openshell/v1/fake/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,15 @@ func (c *fakeSandboxClient) WaitReady(ctx context.Context, workspace, name strin

select {
case <-ctx.Done():
return nil, ctx.Err()
err := ctx.Err()
switch err {
case context.DeadlineExceeded:
return nil, &types.StatusError{Code: types.ErrorDeadlineExceeded, Message: err.Error(), Cause: err}
case context.Canceled:
return nil, &types.StatusError{Code: types.ErrorCancelled, Message: err.Error(), Cause: err}
default:
Comment thread
rhuss marked this conversation as resolved.
Comment thread
rhuss marked this conversation as resolved.
Comment thread
rhuss marked this conversation as resolved.
return nil, &types.StatusError{Code: types.ErrorInternal, Message: err.Error(), Cause: err}
}
default:
}

Expand Down
14 changes: 14 additions & 0 deletions openshell/v1/fake/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,20 @@ func TestSandbox_WaitReady_ContextCancellation(t *testing.T) {
assert.ErrorIs(t, err, context.Canceled)
}

func TestSandbox_WaitReady_ContextDeadlineExceeded(t *testing.T) {
sc := newTestSandboxClient()

_, err := sc.Create(context.Background(), "default", "test-sb", &types.SandboxSpec{}, nil)
require.NoError(t, err)

ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(-time.Second))
defer cancel()

_, err = sc.WaitReady(ctx, "default", "test-sb")
require.Error(t, err)
assert.True(t, types.IsDeadlineExceeded(err), "WaitReady must wrap context.DeadlineExceeded in StatusError")
}

func TestSandbox_WaitReady_AlreadyReady(t *testing.T) {
sc := newTestSandboxClient()
ctx := context.Background()
Expand Down
6 changes: 0 additions & 6 deletions openshell/v1/grpc_errors.go

This file was deleted.

Loading
Loading