From 4501e72ceb12e2fb254f344aa2ef2a6a71d430b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Cie=C5=9Blak?= Date: Tue, 7 May 2024 13:10:01 +0200 Subject: [PATCH] Re-export internal SDK error and use it --- pkg/resources/grant_privileges_to_account_role.go | 3 +-- pkg/resources/grant_privileges_to_database_role.go | 3 +-- pkg/resources/grant_privileges_to_role.go | 2 +- pkg/resources/role.go | 3 ++- pkg/sdk/errors.go | 4 ++++ 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/resources/grant_privileges_to_account_role.go b/pkg/resources/grant_privileges_to_account_role.go index fbe53ae1e7..56fab09757 100644 --- a/pkg/resources/grant_privileges_to_account_role.go +++ b/pkg/resources/grant_privileges_to_account_role.go @@ -741,8 +741,7 @@ func ReadGrantPrivilegesToAccountRole(ctx context.Context, d *schema.ResourceDat client := meta.(*provider.Context).Client - // TODO(SNOW-891217): Use custom error. Right now, "object does not exist" error is hidden in sdk/internal/collections package - if _, err := client.Roles.ShowByID(ctx, id.RoleName); err != nil && err.Error() == "object does not exist" { + if _, err := client.Roles.ShowByID(ctx, id.RoleName); err != nil && errors.Is(err, sdk.ErrObjectNotFound) { d.SetId("") return diag.Diagnostics{ diag.Diagnostic{ diff --git a/pkg/resources/grant_privileges_to_database_role.go b/pkg/resources/grant_privileges_to_database_role.go index 0cc23661a1..75b151f11c 100644 --- a/pkg/resources/grant_privileges_to_database_role.go +++ b/pkg/resources/grant_privileges_to_database_role.go @@ -656,8 +656,7 @@ func ReadGrantPrivilegesToDatabaseRole(ctx context.Context, d *schema.ResourceDa } client := meta.(*provider.Context).Client - // TODO(SNOW-891217): Use custom error. Right now, "object does not exist" error is hidden in sdk/internal/collections package - if _, err := client.DatabaseRoles.ShowByID(ctx, id.DatabaseRoleName); err != nil && err.Error() == "object does not exist" { + if _, err := client.DatabaseRoles.ShowByID(ctx, id.DatabaseRoleName); err != nil && errors.Is(err, sdk.ErrObjectNotFound) { d.SetId("") return diag.Diagnostics{ diag.Diagnostic{ diff --git a/pkg/resources/grant_privileges_to_role.go b/pkg/resources/grant_privileges_to_role.go index 21b5ac2e30..357e26d36b 100644 --- a/pkg/resources/grant_privileges_to_role.go +++ b/pkg/resources/grant_privileges_to_role.go @@ -828,7 +828,7 @@ func setRolePrivilegeOptions(privileges []string, allPrivileges bool, onAccount } func readRoleGrantPrivileges(ctx context.Context, client *sdk.Client, grantedOn sdk.ObjectType, id GrantPrivilegesToRoleID, opts *sdk.ShowGrantOptions, d *schema.ResourceData) error { - if _, err := client.Roles.ShowByID(ctx, sdk.NewAccountObjectIdentifier(id.RoleName)); err != nil && err.Error() == "object does not exist" { + if _, err := client.Roles.ShowByID(ctx, sdk.NewAccountObjectIdentifier(id.RoleName)); err != nil && errors.Is(err, sdk.ErrObjectNotFound) { d.SetId("") log.Printf("[DEBUG] Failed to retrieve account role. Marking the resource as removed.") return nil diff --git a/pkg/resources/role.go b/pkg/resources/role.go index 75469c8524..bf585ddf7f 100644 --- a/pkg/resources/role.go +++ b/pkg/resources/role.go @@ -2,6 +2,7 @@ package resources import ( "context" + "errors" "fmt" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" @@ -77,7 +78,7 @@ func ReadAccountRole(ctx context.Context, d *schema.ResourceData, meta any) diag accountRole, err := client.Roles.ShowByID(ctx, id) if err != nil { - if err.Error() == "object does not exist" { + if errors.Is(err, sdk.ErrObjectNotFound) { d.SetId("") return diag.Diagnostics{ diag.Diagnostic{ diff --git a/pkg/sdk/errors.go b/pkg/sdk/errors.go index 2d7bbc6089..9b0e22c802 100644 --- a/pkg/sdk/errors.go +++ b/pkg/sdk/errors.go @@ -3,6 +3,7 @@ package sdk import ( "errors" "fmt" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/collections" "log" "regexp" "runtime" @@ -13,6 +14,9 @@ var ( ErrNilOptions = NewError("options cannot be nil") ErrPatternRequiredForLikeKeyword = NewError("pattern must be specified for like keyword") + // re-importing from internal package + ErrObjectNotFound = collections.ErrObjectNotFound + // go-snowflake errors. ErrObjectNotExistOrAuthorized = NewError("object does not exist or not authorized") ErrAccountIsEmpty = NewError("account is empty")