Skip to content

Commit c3ae4a2

Browse files
committed
ref: consistent error handling
1 parent d8f5104 commit c3ae4a2

14 files changed

+28
-147
lines changed

internal/provider/data_source_invite.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ func (d *InviteDataSource) Read(ctx context.Context, req datasource.ReadRequest,
6464
var data InviteModel
6565

6666
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
67-
6867
if resp.Diagnostics.HasError() {
6968
return
7069
}
@@ -76,18 +75,11 @@ func (d *InviteDataSource) Read(ctx context.Context, req datasource.ReadRequest,
7675
if err != nil {
7776
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read, got error: %s", err))
7877
return
79-
}
80-
81-
if httpResp.StatusCode() != http.StatusOK {
78+
} else if httpResp.StatusCode() != http.StatusOK || httpResp.JSON200 == nil {
8279
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read, got status code %d: %s", httpResp.StatusCode(), string(httpResp.Body)))
8380
return
8481
}
8582

86-
if httpResp.JSON200 == nil {
87-
resp.Diagnostics.AddError("Client Error", "Unable to read, got empty response")
88-
return
89-
}
90-
9183
resp.Diagnostics.Append(data.Fill(ctx, *httpResp.JSON200)...)
9284
if resp.Diagnostics.HasError() {
9385
return

internal/provider/data_source_invites.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ func (d *InvitesDataSource) Read(ctx context.Context, req datasource.ReadRequest
9090
var data InvitesDataSourceModel
9191

9292
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
93-
9493
if resp.Diagnostics.HasError() {
9594
return
9695
}
@@ -108,9 +107,7 @@ func (d *InvitesDataSource) Read(ctx context.Context, req datasource.ReadRequest
108107
if err != nil {
109108
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read, got error: %s", err))
110109
return
111-
}
112-
113-
if httpResp.StatusCode() != http.StatusOK {
110+
} else if httpResp.StatusCode() != http.StatusOK || httpResp.JSON200 == nil {
114111
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read, got status code %d: %s", httpResp.StatusCode(), string(httpResp.Body)))
115112
return
116113
}

internal/provider/data_source_project.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ func (d *ProjectDataSource) Read(ctx context.Context, req datasource.ReadRequest
5656
var data ProjectModel
5757

5858
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
59-
6059
if resp.Diagnostics.HasError() {
6160
return
6261
}
@@ -68,9 +67,7 @@ func (d *ProjectDataSource) Read(ctx context.Context, req datasource.ReadRequest
6867
if err != nil {
6968
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read, got error: %s", err))
7069
return
71-
}
72-
73-
if httpResp.StatusCode() != http.StatusOK {
70+
} else if httpResp.StatusCode() != http.StatusOK || httpResp.JSON200 == nil {
7471
resp.Diagnostics.AddError("API Error", fmt.Sprintf("Unable to read, got status code %d: %s", httpResp.StatusCode(), string(httpResp.Body)))
7572
return
7673
}

internal/provider/data_source_project_rate_limits.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ func (d *ProjectRateLimitsDataSource) Read(ctx context.Context, req datasource.R
123123
var data ProjectRateLimitsDataSourceModel
124124

125125
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
126-
127126
if resp.Diagnostics.HasError() {
128127
return
129128
}
@@ -142,9 +141,7 @@ func (d *ProjectRateLimitsDataSource) Read(ctx context.Context, req datasource.R
142141
if err != nil {
143142
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read, got error: %s", err))
144143
return
145-
}
146-
147-
if httpResp.StatusCode() != http.StatusOK {
144+
} else if httpResp.StatusCode() != http.StatusOK || httpResp.JSON200 == nil {
148145
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read, got status code %d: %s", httpResp.StatusCode(), string(httpResp.Body)))
149146
return
150147
}

internal/provider/data_source_projects.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ func (d *ProjectsDataSource) Read(ctx context.Context, req datasource.ReadReques
8888
var data ProjectsDataSourceModel
8989

9090
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
91-
9291
if resp.Diagnostics.HasError() {
9392
return
9493
}
@@ -107,9 +106,7 @@ func (d *ProjectsDataSource) Read(ctx context.Context, req datasource.ReadReques
107106
if err != nil {
108107
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read, got error: %s", err))
109108
return
110-
}
111-
112-
if httpResp.StatusCode() != http.StatusOK {
109+
} else if httpResp.StatusCode() != http.StatusOK || httpResp.JSON200 == nil {
113110
resp.Diagnostics.AddError("API Error", fmt.Sprintf("Unable to read, got status code %d: %s", httpResp.StatusCode(), string(httpResp.Body)))
114111
return
115112
}

internal/provider/data_source_user.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ func (d *UserDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
5656
var data UserModel
5757

5858
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
59-
6059
if resp.Diagnostics.HasError() {
6160
return
6261
}
@@ -68,18 +67,11 @@ func (d *UserDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
6867
if err != nil {
6968
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read, got error: %s", err))
7069
return
71-
}
72-
73-
if httpResp.StatusCode() != http.StatusOK {
70+
} else if httpResp.StatusCode() != http.StatusOK || httpResp.JSON200 == nil {
7471
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read, got status code %d: %s", httpResp.StatusCode(), string(httpResp.Body)))
7572
return
7673
}
7774

78-
if httpResp.JSON200 == nil {
79-
resp.Diagnostics.AddError("Client Error", "Unable to read, got empty response")
80-
return
81-
}
82-
8375
resp.Diagnostics.Append(data.Fill(ctx, *httpResp.JSON200)...)
8476
if resp.Diagnostics.HasError() {
8577
return

internal/provider/data_source_users.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ func (d *UsersDataSource) Read(ctx context.Context, req datasource.ReadRequest,
8282
var data UsersDataSourceModel
8383

8484
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
85-
8685
if resp.Diagnostics.HasError() {
8786
return
8887
}
@@ -100,9 +99,7 @@ func (d *UsersDataSource) Read(ctx context.Context, req datasource.ReadRequest,
10099
if err != nil {
101100
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read, got error: %s", err))
102101
return
103-
}
104-
105-
if httpResp.StatusCode() != http.StatusOK {
102+
} else if httpResp.StatusCode() != http.StatusOK || httpResp.JSON200 == nil {
106103
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read, got status code %d: %s", httpResp.StatusCode(), string(httpResp.Body)))
107104
return
108105
}

internal/provider/resource_admin_api_key.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (r *AdminApiKeyResource) Create(ctx context.Context, req resource.CreateReq
111111
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create, got error: %s", err))
112112
return
113113
} else if httpResp.StatusCode() != http.StatusOK || httpResp.JSON200 == nil {
114-
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create, got status code %d", httpResp.StatusCode()))
114+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create, got status code %d: %s", httpResp.StatusCode(), string(httpResp.Body)))
115115
return
116116
}
117117

internal/provider/resource_invite.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ func (r *InviteResource) Create(ctx context.Context, req resource.CreateRequest,
8383
var data InviteModel
8484

8585
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
86-
8786
if resp.Diagnostics.HasError() {
8887
return
8988
}
@@ -123,7 +122,6 @@ func (r *InviteResource) Read(ctx context.Context, req resource.ReadRequest, res
123122
var data InviteModel
124123

125124
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
126-
127125
if resp.Diagnostics.HasError() {
128126
return
129127
}
@@ -136,18 +134,11 @@ func (r *InviteResource) Read(ctx context.Context, req resource.ReadRequest, res
136134
if err != nil {
137135
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read, got error: %s", err))
138136
return
139-
}
140-
141-
if httpResp.StatusCode() != http.StatusOK {
137+
} else if httpResp.StatusCode() != http.StatusOK || httpResp.JSON200 == nil {
142138
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read, got status code %d: %s", httpResp.StatusCode(), string(httpResp.Body)))
143139
return
144140
}
145141

146-
if httpResp.JSON200 == nil {
147-
resp.Diagnostics.AddError("Client Error", "Unable to read, got empty response body")
148-
return
149-
}
150-
151142
resp.Diagnostics.Append(data.Fill(ctx, *httpResp.JSON200)...)
152143
if resp.Diagnostics.HasError() {
153144
return
@@ -164,7 +155,6 @@ func (r *InviteResource) Delete(ctx context.Context, req resource.DeleteRequest,
164155
var data InviteModel
165156

166157
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
167-
168158
if resp.Diagnostics.HasError() {
169159
return
170160
}
@@ -177,9 +167,7 @@ func (r *InviteResource) Delete(ctx context.Context, req resource.DeleteRequest,
177167
if err != nil {
178168
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete, got error: %s", err))
179169
return
180-
}
181-
182-
if httpResp.StatusCode() != http.StatusOK {
170+
} else if httpResp.StatusCode() != http.StatusOK {
183171
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete, got status code %d: %s", httpResp.StatusCode(), httpResp.Body))
184172
return
185173
}

internal/provider/resource_project.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ func (r *ProjectResource) Create(ctx context.Context, req resource.CreateRequest
6464
var data ProjectModel
6565

6666
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
67-
6867
if resp.Diagnostics.HasError() {
6968
return
7069
}
@@ -79,9 +78,7 @@ func (r *ProjectResource) Create(ctx context.Context, req resource.CreateRequest
7978
if err != nil {
8079
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create, got error: %s", err))
8180
return
82-
}
83-
84-
if httpResp.StatusCode() != http.StatusCreated {
81+
} else if httpResp.StatusCode() != http.StatusCreated || httpResp.JSON201 == nil {
8582
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create, got status code %d: %s", httpResp.StatusCode(), string(httpResp.Body)))
8683
return
8784
}
@@ -98,7 +95,6 @@ func (r *ProjectResource) Read(ctx context.Context, req resource.ReadRequest, re
9895
var data ProjectModel
9996

10097
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
101-
10298
if resp.Diagnostics.HasError() {
10399
return
104100
}
@@ -111,9 +107,7 @@ func (r *ProjectResource) Read(ctx context.Context, req resource.ReadRequest, re
111107
if err != nil {
112108
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read, got error: %s", err))
113109
return
114-
}
115-
116-
if httpResp.StatusCode() != http.StatusOK {
110+
} else if httpResp.StatusCode() != http.StatusOK || httpResp.JSON200 == nil {
117111
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read, got status code %d: %s", httpResp.StatusCode(), string(httpResp.Body)))
118112
return
119113
}
@@ -130,7 +124,6 @@ func (r *ProjectResource) Update(ctx context.Context, req resource.UpdateRequest
130124
var data ProjectModel
131125

132126
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
133-
134127
if resp.Diagnostics.HasError() {
135128
return
136129
}
@@ -146,9 +139,7 @@ func (r *ProjectResource) Update(ctx context.Context, req resource.UpdateRequest
146139
if err != nil {
147140
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update, got error: %s", err))
148141
return
149-
}
150-
151-
if httpResp.StatusCode() != http.StatusOK {
142+
} else if httpResp.StatusCode() != http.StatusOK || httpResp.JSON200 == nil {
152143
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update, got status code %d: %s", httpResp.StatusCode(), string(httpResp.Body)))
153144
return
154145
}
@@ -165,7 +156,6 @@ func (r *ProjectResource) Delete(ctx context.Context, req resource.DeleteRequest
165156
var data ProjectModel
166157

167158
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
168-
169159
if resp.Diagnostics.HasError() {
170160
return
171161
}
@@ -178,9 +168,7 @@ func (r *ProjectResource) Delete(ctx context.Context, req resource.DeleteRequest
178168
if err != nil {
179169
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update, got error: %s", err))
180170
return
181-
}
182-
183-
if httpResp.StatusCode() != http.StatusOK {
171+
} else if httpResp.StatusCode() != http.StatusOK {
184172
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update, got status code %d: %s", httpResp.StatusCode(), string(httpResp.Body)))
185173
return
186174
}

internal/provider/resource_project_rate_limit.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ func (r *ProjectRateLimitResource) Create(ctx context.Context, req resource.Crea
102102
var data ProjectRateLimitResourceModel
103103

104104
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
105-
106105
if resp.Diagnostics.HasError() {
107106
return
108107
}
@@ -126,18 +125,11 @@ func (r *ProjectRateLimitResource) Create(ctx context.Context, req resource.Crea
126125
if err != nil {
127126
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create, got error: %s", err))
128127
return
129-
}
130-
131-
if httpResp.StatusCode() != http.StatusOK {
128+
} else if httpResp.StatusCode() != http.StatusOK || httpResp.JSON200 == nil {
132129
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create, got status code %d: %s", httpResp.StatusCode(), string(httpResp.Body)))
133130
return
134131
}
135132

136-
if httpResp.JSON200 == nil {
137-
resp.Diagnostics.AddError("Client Error", "Unable to create, got empty response")
138-
return
139-
}
140-
141133
resp.Diagnostics.Append(data.Fill(ctx, *httpResp.JSON200)...)
142134
if resp.Diagnostics.HasError() {
143135
return
@@ -150,7 +142,6 @@ func (r *ProjectRateLimitResource) Read(ctx context.Context, req resource.ReadRe
150142
var data ProjectRateLimitResourceModel
151143

152144
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
153-
154145
if resp.Diagnostics.HasError() {
155146
return
156147
}
@@ -170,18 +161,11 @@ out:
170161
if err != nil {
171162
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read, got error: %s", err))
172163
return
173-
}
174-
175-
if httpResp.StatusCode() != http.StatusOK {
164+
} else if httpResp.StatusCode() != http.StatusOK || httpResp.JSON200 == nil {
176165
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read, got status code %d: %s", httpResp.StatusCode(), string(httpResp.Body)))
177166
return
178167
}
179168

180-
if httpResp.JSON200 == nil {
181-
resp.Diagnostics.AddError("Client Error", "Unable to read, got empty response")
182-
return
183-
}
184-
185169
for _, rl := range httpResp.JSON200.Data {
186170
if rl.Model != data.Model.ValueString() {
187171
continue
@@ -203,7 +187,6 @@ func (r *ProjectRateLimitResource) Update(ctx context.Context, req resource.Upda
203187
var data ProjectRateLimitResourceModel
204188

205189
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
206-
207190
if resp.Diagnostics.HasError() {
208191
return
209192
}
@@ -227,18 +210,11 @@ func (r *ProjectRateLimitResource) Update(ctx context.Context, req resource.Upda
227210
if err != nil {
228211
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update, got error: %s", err))
229212
return
230-
}
231-
232-
if httpResp.StatusCode() != http.StatusOK {
213+
} else if httpResp.StatusCode() != http.StatusOK || httpResp.JSON200 == nil {
233214
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update, got status code %d: %s", httpResp.StatusCode(), string(httpResp.Body)))
234215
return
235216
}
236217

237-
if httpResp.JSON200 == nil {
238-
resp.Diagnostics.AddError("Client Error", "Unable to update, got empty response")
239-
return
240-
}
241-
242218
resp.Diagnostics.Append(data.Fill(ctx, *httpResp.JSON200)...)
243219
if resp.Diagnostics.HasError() {
244220
return

0 commit comments

Comments
 (0)