Skip to content

Commit

Permalink
Handle user error which is not wrapped as googleapi.Err
Browse files Browse the repository at this point in the history
  • Loading branch information
saikat-royc committed Jun 13, 2023
1 parent b5e4c03 commit a174f15
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
24 changes: 23 additions & 1 deletion pkg/cloud_provider/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions pkg/cloud_provider/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a174f15

Please sign in to comment.