From a174f1592ba92daeca26e984b840835eab812207 Mon Sep 17 00:00:00 2001 From: Saikat Roychowdhury Date: Wed, 7 Jun 2023 19:48:57 +0000 Subject: [PATCH] Handle user error which is not wrapped as googleapi.Err --- pkg/cloud_provider/file/file.go | 24 +++++++++++++++++++++++- pkg/cloud_provider/file/file_test.go | 20 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/pkg/cloud_provider/file/file.go b/pkg/cloud_provider/file/file.go index 498df70b4..8474703d9 100644 --- a/pkg/cloud_provider/file/file.go +++ b/pkg/cloud_provider/file/file.go @@ -649,7 +649,8 @@ func isUserError(err error) *codes.Code { // Upwrap the error var apiErr *googleapi.Error if !errors.As(err, &apiErr) { - return nil + // Fallback to check for expected error code in the error string + return containsUserErrStr(err) } userErrors := map[int]codes.Code{ @@ -664,6 +665,27 @@ func isUserError(err error) *codes.Code { return nil } +func containsUserErrStr(err error) *codes.Code { + if err == nil { + return nil + } + + // Error string picked up from https://cloud.google.com/apis/design/errors#handling_errors + if strings.Contains(err.Error(), "PERMISSION_DENIED") { + return util.ErrCodePtr(codes.PermissionDenied) + } + if strings.Contains(err.Error(), "RESOURCE_EXHAUSTED") { + return util.ErrCodePtr(codes.ResourceExhausted) + } + if strings.Contains(err.Error(), "INVALID_ARGUMENT") { + return util.ErrCodePtr(codes.InvalidArgument) + } + if strings.Contains(err.Error(), "NOT_FOUND") { + return util.ErrCodePtr(codes.NotFound) + } + return nil +} + // isContextError returns a pointer to the grpc error code DeadlineExceeded // if the passed in error contains the "context deadline exceeded" string and returns // the grpc error code Canceled if the error contains the "context canceled" string. diff --git a/pkg/cloud_provider/file/file_test.go b/pkg/cloud_provider/file/file_test.go index 27665d4d6..799a32468 100644 --- a/pkg/cloud_provider/file/file_test.go +++ b/pkg/cloud_provider/file/file_test.go @@ -630,6 +630,26 @@ func TestIsUserError(t *testing.T) { err: fmt.Errorf("got error: %w", &googleapi.Error{Code: http.StatusForbidden}), expectedErrCode: util.ErrCodePtr(codes.PermissionDenied), }, + { + name: "RESOURCE_EXHAUSTED error", + err: fmt.Errorf("got error: RESOURCE_EXHAUSTED: Operation rate exceeded"), + expectedErrCode: util.ErrCodePtr(codes.ResourceExhausted), + }, + { + name: "INVALID_ARGUMENT error", + err: fmt.Errorf("got error: INVALID_ARGUMENT"), + expectedErrCode: util.ErrCodePtr(codes.InvalidArgument), + }, + { + name: "PERMISSION_DENIED error", + err: fmt.Errorf("got error: PERMISSION_DENIED"), + expectedErrCode: util.ErrCodePtr(codes.PermissionDenied), + }, + { + name: "NOT_FOUND error", + err: fmt.Errorf("got error: NOT_FOUND"), + expectedErrCode: util.ErrCodePtr(codes.NotFound), + }, } for _, test := range cases {