|
| 1 | +package bedrock |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + smithy "github.com/aws/smithy-go" |
| 11 | + smithyhttp "github.com/aws/smithy-go/transport/http" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + "github.com/docker/docker-agent/pkg/modelerrors" |
| 16 | +) |
| 17 | + |
| 18 | +func makeTestBedrockError(statusCode int, retryAfterValue string) error { |
| 19 | + header := http.Header{} |
| 20 | + if retryAfterValue != "" { |
| 21 | + header.Set("Retry-After", retryAfterValue) |
| 22 | + } |
| 23 | + |
| 24 | + httpResp := &http.Response{ |
| 25 | + StatusCode: statusCode, |
| 26 | + Header: header, |
| 27 | + } |
| 28 | + resp := &smithyhttp.Response{Response: httpResp} |
| 29 | + |
| 30 | + return &smithy.OperationError{ |
| 31 | + ServiceID: "BedrockRuntime", |
| 32 | + OperationName: "ConverseStream", |
| 33 | + Err: &smithyhttp.ResponseError{ |
| 34 | + Response: resp, |
| 35 | + Err: &smithy.GenericAPIError{ |
| 36 | + Code: "ThrottlingException", |
| 37 | + Message: "Rate exceeded", |
| 38 | + }, |
| 39 | + }, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestWrapBedrockError(t *testing.T) { |
| 44 | + t.Parallel() |
| 45 | + |
| 46 | + t.Run("nil returns nil", func(t *testing.T) { |
| 47 | + t.Parallel() |
| 48 | + assert.NoError(t, wrapBedrockError(nil)) |
| 49 | + }) |
| 50 | + |
| 51 | + t.Run("non-AWS error passes through unchanged", func(t *testing.T) { |
| 52 | + t.Parallel() |
| 53 | + orig := errors.New("some network error") |
| 54 | + result := wrapBedrockError(orig) |
| 55 | + assert.Equal(t, orig, result) |
| 56 | + var se *modelerrors.StatusError |
| 57 | + assert.NotErrorAs(t, result, &se) |
| 58 | + }) |
| 59 | + |
| 60 | + t.Run("429 without Retry-After wraps with zero RetryAfter", func(t *testing.T) { |
| 61 | + t.Parallel() |
| 62 | + awsErr := makeTestBedrockError(429, "") |
| 63 | + result := wrapBedrockError(awsErr) |
| 64 | + var se *modelerrors.StatusError |
| 65 | + require.ErrorAs(t, result, &se) |
| 66 | + assert.Equal(t, 429, se.StatusCode) |
| 67 | + assert.Equal(t, time.Duration(0), se.RetryAfter) |
| 68 | + // Original error still accessible |
| 69 | + assert.ErrorIs(t, result, awsErr) |
| 70 | + }) |
| 71 | + |
| 72 | + t.Run("429 with Retry-After header sets RetryAfter", func(t *testing.T) { |
| 73 | + t.Parallel() |
| 74 | + awsErr := makeTestBedrockError(429, "20") |
| 75 | + result := wrapBedrockError(awsErr) |
| 76 | + var se *modelerrors.StatusError |
| 77 | + require.ErrorAs(t, result, &se) |
| 78 | + assert.Equal(t, 429, se.StatusCode) |
| 79 | + assert.Equal(t, 20*time.Second, se.RetryAfter) |
| 80 | + }) |
| 81 | + |
| 82 | + t.Run("500 wraps with correct status code", func(t *testing.T) { |
| 83 | + t.Parallel() |
| 84 | + awsErr := makeTestBedrockError(500, "") |
| 85 | + result := wrapBedrockError(awsErr) |
| 86 | + var se *modelerrors.StatusError |
| 87 | + require.ErrorAs(t, result, &se) |
| 88 | + assert.Equal(t, 500, se.StatusCode) |
| 89 | + assert.Equal(t, time.Duration(0), se.RetryAfter) |
| 90 | + }) |
| 91 | + |
| 92 | + t.Run("wrapped error is classified correctly by ClassifyModelError", func(t *testing.T) { |
| 93 | + t.Parallel() |
| 94 | + awsErr := makeTestBedrockError(429, "15") |
| 95 | + result := wrapBedrockError(awsErr) |
| 96 | + retryable, rateLimited, retryAfter := modelerrors.ClassifyModelError(result) |
| 97 | + assert.False(t, retryable) |
| 98 | + assert.True(t, rateLimited) |
| 99 | + assert.Equal(t, 15*time.Second, retryAfter) |
| 100 | + }) |
| 101 | + |
| 102 | + t.Run("wrapped in fmt.Errorf still classified correctly", func(t *testing.T) { |
| 103 | + t.Parallel() |
| 104 | + awsErr := makeTestBedrockError(500, "") |
| 105 | + wrapped := fmt.Errorf("bedrock converse stream failed: %w", wrapBedrockError(awsErr)) |
| 106 | + retryable, rateLimited, _ := modelerrors.ClassifyModelError(wrapped) |
| 107 | + assert.True(t, retryable) |
| 108 | + assert.False(t, rateLimited) |
| 109 | + }) |
| 110 | +} |
0 commit comments