CloudWatch: Improve smithy error handling (#108523)

* CloudWatch: Improve smithy error handling

* gofmt
This commit is contained in:
Nathan Vērzemnieks
2025-07-28 02:43:37 -07:00
committed by GitHub
parent 8d48dbce66
commit 252fc67fbd
2 changed files with 7 additions and 7 deletions
+4 -4
View File
@@ -4,10 +4,10 @@ import (
"context"
"errors"
"fmt"
"strings"
oam "github.com/aws/aws-sdk-go-v2/service/oam"
oamtypes "github.com/aws/aws-sdk-go-v2/service/oam/types"
"github.com/aws/smithy-go"
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models"
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models/resources"
)
@@ -28,9 +28,9 @@ func (a *AccountsService) GetAccountsForCurrentUserOrRole(ctx context.Context) (
for {
response, err := a.ListSinks(ctx, &oam.ListSinksInput{NextToken: nextToken})
if err != nil {
// TODO: this is a bit hacky, figure out how to do it right in v2
if strings.Contains(err.Error(), "AccessDeniedException") {
return nil, fmt.Errorf("%w: %s", ErrAccessDeniedException, err.Error())
smithyErr := &smithy.GenericAPIError{}
if errors.As(err, &smithyErr) && smithyErr.Code == "AccessDeniedException" {
return nil, fmt.Errorf("%w: %s", ErrAccessDeniedException, smithyErr.Message)
}
return nil, fmt.Errorf("ListSinks error: %w", err)
}
@@ -2,13 +2,13 @@ package services
import (
"context"
"errors"
"fmt"
"testing"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/oam"
oamtypes "github.com/aws/aws-sdk-go-v2/service/oam/types"
"github.com/aws/smithy-go"
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/mocks"
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models/resources"
@@ -20,14 +20,14 @@ import (
func TestHandleGetAccounts(t *testing.T) {
t.Run("Should return an error in case of insufficient permissions from ListSinks", func(t *testing.T) {
fakeOAMClient := &mocks.FakeOAMClient{}
fakeOAMClient.On("ListSinks", mock.Anything).Return(&oam.ListSinksOutput{}, errors.New("AccessDeniedException"))
fakeOAMClient.On("ListSinks", mock.Anything).Return(&oam.ListSinksOutput{}, fmt.Errorf("%w", &smithy.GenericAPIError{Code: "AccessDeniedException", Message: "this is bad"}))
accounts := NewAccountsService(fakeOAMClient)
resp, err := accounts.GetAccountsForCurrentUserOrRole(context.Background())
assert.Error(t, err)
assert.Nil(t, resp)
assert.Equal(t, "access denied. please check your IAM policy: AccessDeniedException", err.Error())
assert.Equal(t, "access denied. please check your IAM policy: this is bad", err.Error())
assert.ErrorIs(t, err, ErrAccessDeniedException)
})