|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package azdext |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestExtensionError_RoundTrip(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + inputErr error |
| 18 | + wantInfo errorInfo |
| 19 | + useErrorStringAsMessage bool |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "NilError", |
| 23 | + inputErr: nil, |
| 24 | + wantInfo: errorInfo{}, |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "SimpleError", |
| 28 | + inputErr: errors.New("simple error"), |
| 29 | + wantInfo: errorInfo{ |
| 30 | + message: "simple error", |
| 31 | + }, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "ExtensionResponseError", |
| 35 | + inputErr: &ExtensionResponseError{ |
| 36 | + Message: "Rate limit exceeded", |
| 37 | + Details: "Too many requests", |
| 38 | + ErrorCode: "RateLimitExceeded", |
| 39 | + StatusCode: 429, |
| 40 | + ServiceName: "openai.azure.com", |
| 41 | + }, |
| 42 | + wantInfo: errorInfo{ |
| 43 | + message: "Rate limit exceeded", |
| 44 | + details: "Too many requests", |
| 45 | + errorCode: "RateLimitExceeded", |
| 46 | + statusCode: 429, |
| 47 | + service: "openai.azure.com", |
| 48 | + }, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "AzCoreResponseError", |
| 52 | + inputErr: &azcore.ResponseError{ |
| 53 | + ErrorCode: "ResourceNotFound", |
| 54 | + StatusCode: 404, |
| 55 | + }, |
| 56 | + wantInfo: errorInfo{ |
| 57 | + errorCode: "ResourceNotFound", |
| 58 | + statusCode: 404, |
| 59 | + }, |
| 60 | + useErrorStringAsMessage: true, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + for _, tt := range tests { |
| 65 | + t.Run(tt.name, func(t *testing.T) { |
| 66 | + if tt.useErrorStringAsMessage { |
| 67 | + tt.wantInfo.message = tt.inputErr.Error() |
| 68 | + } |
| 69 | + |
| 70 | + // Helper to verify the proto message content |
| 71 | + type protoMessage interface { |
| 72 | + GetMessage() string |
| 73 | + GetDetails() string |
| 74 | + GetErrorCode() string |
| 75 | + GetStatusCode() int32 |
| 76 | + GetServiceName() string |
| 77 | + } |
| 78 | + verifyProto := func(t *testing.T, msg protoMessage) { |
| 79 | + assert.NotNil(t, msg) |
| 80 | + assert.Equal(t, tt.wantInfo.message, msg.GetMessage()) |
| 81 | + assert.Equal(t, tt.wantInfo.details, msg.GetDetails()) |
| 82 | + assert.Equal(t, tt.wantInfo.errorCode, msg.GetErrorCode()) |
| 83 | + assert.Equal(t, tt.wantInfo.statusCode, msg.GetStatusCode()) |
| 84 | + assert.Equal(t, tt.wantInfo.service, msg.GetServiceName()) |
| 85 | + } |
| 86 | + |
| 87 | + // Helper to verify the unwrapped error |
| 88 | + verifyUnwrapped := func(t *testing.T, err error) { |
| 89 | + var extErr *ExtensionResponseError |
| 90 | + if assert.ErrorAs(t, err, &extErr) { |
| 91 | + assert.Equal(t, tt.wantInfo.message, extErr.Message) |
| 92 | + assert.Equal(t, tt.wantInfo.details, extErr.Details) |
| 93 | + assert.Equal(t, tt.wantInfo.errorCode, extErr.ErrorCode) |
| 94 | + assert.Equal(t, int(tt.wantInfo.statusCode), extErr.StatusCode) |
| 95 | + assert.Equal(t, tt.wantInfo.service, extErr.ServiceName) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Test ServiceTarget wrapping |
| 100 | + stMsg := WrapErrorForServiceTarget(tt.inputErr) |
| 101 | + if tt.inputErr == nil { |
| 102 | + assert.Nil(t, stMsg) |
| 103 | + } else { |
| 104 | + verifyProto(t, stMsg) |
| 105 | + verifyUnwrapped(t, UnwrapErrorFromServiceTarget(stMsg)) |
| 106 | + } |
| 107 | + |
| 108 | + // Test FrameworkService wrapping |
| 109 | + fsMsg := WrapErrorForFrameworkService(tt.inputErr) |
| 110 | + if tt.inputErr == nil { |
| 111 | + assert.Nil(t, fsMsg) |
| 112 | + } else { |
| 113 | + verifyProto(t, fsMsg) |
| 114 | + verifyUnwrapped(t, UnwrapErrorFromFrameworkService(fsMsg)) |
| 115 | + } |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
0 commit comments