Skip to content

Commit a2d461d

Browse files
committed
fix: error handling
1 parent 4b36cf7 commit a2d461d

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

internal/diagutils/diagutils.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ func NewClientError(action string, err error) diag.ErrorDiagnostic {
1010
return diag.NewErrorDiagnostic("Client error", fmt.Sprintf("Unable to %s, got error: %s", action, err))
1111
}
1212

13+
func NewClientStatusError(action string, status int, body []byte) diag.ErrorDiagnostic {
14+
return diag.NewErrorDiagnostic("Client error", fmt.Sprintf("Unable to %s, got status %d: %s", action, status, string(body)))
15+
}
16+
1317
func NewNotFoundError(resource string) diag.ErrorDiagnostic {
1418
return diag.NewErrorDiagnostic("Not found", fmt.Sprintf("No matching %s found", resource))
1519
}

internal/provider/data_source_organization.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,17 @@ func (d *OrganizationDataSource) Read(ctx context.Context, req datasource.ReadRe
7676
ctx,
7777
data.Slug.ValueString(),
7878
)
79+
if err != nil {
80+
resp.Diagnostics.Append(diagutils.NewClientError("read", err))
81+
return
82+
}
83+
7984
if httpResp.StatusCode() == http.StatusNotFound {
8085
resp.Diagnostics.Append(diagutils.NewNotFoundError("organization"))
86+
resp.State.RemoveResource(ctx)
8187
return
82-
}
83-
if err != nil {
84-
resp.Diagnostics.Append(diagutils.NewClientError("read", err))
88+
} else if httpResp.StatusCode() != http.StatusOK {
89+
resp.Diagnostics.Append(diagutils.NewClientStatusError("read", httpResp.StatusCode(), httpResp.Body))
8590
return
8691
}
8792

internal/provider/data_source_project.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,17 @@ func (d *ProjectDataSource) Read(ctx context.Context, req datasource.ReadRequest
114114
data.Organization.ValueString(),
115115
data.Slug.ValueString(),
116116
)
117+
if err != nil {
118+
resp.Diagnostics.Append(diagutils.NewClientError("read", err))
119+
return
120+
}
117121

118122
if httpResp.StatusCode() == http.StatusNotFound {
119123
resp.Diagnostics.Append(diagutils.NewNotFoundError("project"))
124+
resp.State.RemoveResource(ctx)
120125
return
121-
}
122-
if err != nil {
123-
resp.Diagnostics.Append(diagutils.NewClientError("read", err))
126+
} else if httpResp.StatusCode() != http.StatusOK {
127+
resp.Diagnostics.Append(diagutils.NewClientStatusError("read", httpResp.StatusCode(), httpResp.Body))
124128
return
125129
}
126130

internal/provider/resource_project_spike_protection.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,17 @@ func (r *ProjectSpikeProtectionResource) Read(ctx context.Context, req resource.
118118
data.Organization.ValueString(),
119119
data.Project.ValueString(),
120120
)
121+
if err != nil {
122+
resp.Diagnostics.Append(diagutils.NewClientError("read", err))
123+
return
124+
}
125+
121126
if httpResp.StatusCode() == http.StatusNotFound {
122127
resp.Diagnostics.Append(diagutils.NewNotFoundError("project"))
123128
resp.State.RemoveResource(ctx)
124129
return
125-
}
126-
if err != nil {
127-
resp.Diagnostics.Append(diagutils.NewClientError("read", err))
130+
} else if httpResp.StatusCode() != http.StatusOK {
131+
resp.Diagnostics.Append(diagutils.NewClientStatusError("read", httpResp.StatusCode(), httpResp.Body))
128132
return
129133
}
130134

0 commit comments

Comments
 (0)