From 7432680e0410ad9573b3992e822486aca2f41fa9 Mon Sep 17 00:00:00 2001 From: domenicsim1 <87625140+domenicsim1@users.noreply.github.com> Date: Thu, 28 Nov 2024 17:55:57 +1100 Subject: [PATCH] fix: panic on resource tenant project error handling (#824) --- .../resource_tenant_project.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/octopusdeploy_framework/resource_tenant_project.go b/octopusdeploy_framework/resource_tenant_project.go index a5f68eac7..408128023 100644 --- a/octopusdeploy_framework/resource_tenant_project.go +++ b/octopusdeploy_framework/resource_tenant_project.go @@ -2,7 +2,9 @@ package octopusdeploy_framework import ( "context" + "errors" "fmt" + internalErrors "github.com/OctopusDeploy/terraform-provider-octopusdeploy/internal/errors" "net/http" "strings" @@ -90,11 +92,10 @@ func (t *tenantProjectResource) Read(ctx context.Context, req resource.ReadReque tenant, err := tenants.GetByID(t.Client, spaceID, tenantID) if err != nil { - apiError := err.(*core.APIError) - if apiError.StatusCode != http.StatusNotFound { + if err := internalErrors.ProcessApiErrorV2(ctx, resp, data, err, "tenant"); err != nil { resp.Diagnostics.AddError("unable to load tenant", err.Error()) - return } + return } data.EnvironmentIDs = util.FlattenStringList(tenant.ProjectEnvironments[projectID]) @@ -162,10 +163,12 @@ func (t *tenantProjectResource) Delete(ctx context.Context, req resource.DeleteR tenant, err := tenants.GetByID(t.Client, spaceId, data.TenantID.ValueString()) if err != nil { - apiError := err.(*core.APIError) - if apiError.StatusCode == http.StatusNotFound { - tflog.Info(ctx, fmt.Sprintf("tenant (%s) no longer exists", data.TenantID.ValueString())) - return + var apiError *core.APIError + if errors.As(err, &apiError) { + if apiError.StatusCode == http.StatusNotFound { + tflog.Info(ctx, fmt.Sprintf("tenant (%s) no longer exists", data.TenantID.ValueString())) + return + } } else { resp.Diagnostics.AddError("cannot load tenant", err.Error()) return