Skip to content

Commit 92893ba

Browse files
authored
more error handling improvements (#370)
This fixes some cases where the underlying errors can be swallowed and not visible to the user.
1 parent d3d2cee commit 92893ba

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

internal/provider/resource_streaming_topic.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -402,16 +402,12 @@ func (r *StreamingTopicResource) Read(ctx context.Context, req resource.ReadRequ
402402

403403
//schema := &StreamingTopicSchema{}
404404
params := pulsaradmin.SchemasResourceGetSchemaParams{}
405-
schemaResp, err := r.clients.pulsarAdminClient.SchemasResourceGetSchemaWithResponse(ctx, tenant, namespace, topic,
406-
&params, streamingRequestHeaders)
405+
schemaResp, err := r.clients.pulsarAdminClient.SchemasResourceGetSchemaWithResponse(ctx, tenant, namespace, topic, &params, streamingRequestHeaders)
407406
if err != nil {
408-
resp.Diagnostics.AddError(
409-
"Failed to get topic schema",
410-
err.Error(),
411-
)
407+
resp.Diagnostics.AddError("Failed to get topic schema", err.Error())
412408
return
413409
} else if schemaResp.StatusCode() > 299 && schemaResp.StatusCode() != 404 {
414-
resp.Diagnostics.Append(HTTPResponseDiagWarn(schemaResp.HTTPResponse, err, "Failed to get topic schema")...)
410+
resp.Diagnostics.Append(HTTPResponseDiagWarnWithBody(schemaResp.StatusCode(), schemaResp.Body, err, "failed to get topic schema")...)
415411
} else if schemaResp.JSON200 != nil {
416412
state.Schema = &StreamingTopicSchema{
417413
Type: (*string)(schemaResp.JSON200.Type),

internal/provider/types_streaming_namespace_policies.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func getPulsarNamespacePolicies(ctx context.Context, pulsarAdminClient *pulsarad
262262
policiesAttrTypes := plan.Policies.AttributeTypes(ctx)
263263

264264
resp, err := pulsarAdminClient.NamespacesGetPoliciesWithResponse(ctx, plan.Tenant.ValueString(), plan.Namespace.ValueString(), requestEditors...)
265-
diags.Append(HTTPResponseDiagErr(resp.HTTPResponse, err, "failed to get namespace policies")...)
265+
diags.Append(HTTPResponseDiagErrWithBody(resp.StatusCode(), resp.Body, err, "failed to get namespace policies")...)
266266
if diags.HasError() {
267267
return types.ObjectNull(policiesAttrTypes), diags
268268
}

internal/provider/util_terraform.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func MergeTerraformObjects(old, new types.Object, attributeTypes map[string]attr
9494
return basetypes.NewObjectValue(attributeTypes, attributes)
9595
}
9696

97-
// HTTPResponseDiagErr takes an HTTP response and error code and creates a Terraform Error Diagnostic if there is an error
97+
// HTTPResponseDiagErr takes an HTTP response and error and creates a Terraform Error Diagnostic if there is an error
9898
func HTTPResponseDiagErr(resp *http.Response, err error, errorSummary string) diag.Diagnostics {
9999
diags := diag.Diagnostics{}
100100
if err != nil {
@@ -112,7 +112,19 @@ func HTTPResponseDiagErr(resp *http.Response, err error, errorSummary string) di
112112
return diags
113113
}
114114

115-
// HTTPResponseDiagWarn takes an HTTP response and error code and creates a Terraform Warn Diagnostic if there is an error
115+
// HTTPResponseDiagErrWithBody takes an HTTP status code, body, and error and creates a Terraform Error Diagnostic if there is an error
116+
func HTTPResponseDiagErrWithBody(statusCode int, body []byte, err error, errorSummary string) diag.Diagnostics {
117+
diags := diag.Diagnostics{}
118+
if err != nil {
119+
diags.AddError(errorSummary, err.Error())
120+
} else if statusCode >= 300 {
121+
details := fmt.Sprintf("Received status code: '%v', with message: %s", statusCode, body)
122+
diags.AddError(errorSummary, details)
123+
}
124+
return diags
125+
}
126+
127+
// HTTPResponseDiagWarn takes an HTTP response and error and creates a Terraform Warn Diagnostic if there is an error
116128
// or if the status code is not in the 2xx range
117129
func HTTPResponseDiagWarn(resp *http.Response, err error, errorSummary string) diag.Diagnostics {
118130
diags := diag.Diagnostics{}
@@ -131,6 +143,18 @@ func HTTPResponseDiagWarn(resp *http.Response, err error, errorSummary string) d
131143
return diags
132144
}
133145

146+
// HTTPResponseDiagWarnWithBody takes an HTTP status code, body, and error and creates a Terraform Error Diagnostic if there is an error
147+
func HTTPResponseDiagWarnWithBody(statusCode int, body []byte, err error, errorSummary string) diag.Diagnostics {
148+
diags := diag.Diagnostics{}
149+
if err != nil {
150+
diags.AddWarning(errorSummary, err.Error())
151+
} else if statusCode >= 300 {
152+
details := fmt.Sprintf("Received status code: '%v', with message: %s", statusCode, body)
153+
diags.AddWarning(errorSummary, details)
154+
}
155+
return diags
156+
}
157+
134158
// planModifierStringValueChanged is a terraform plan modifier function to use with 'RequiresReplaceIf' to check if a string value
135159
// changed from one value to another, not including null values.
136160
func planModifierStringValueChanged() stringplanmodifier.RequiresReplaceIfFunc {

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func main() {
4747
providerserver.NewProtocol6(provider.New(version)()),
4848
}
4949

50+
// Combine the legacy SDK provider with the newer Framework provider
5051
muxServer, err := tf6muxserver.NewMuxServer(ctx, providers...)
5152

5253
if err != nil {

0 commit comments

Comments
 (0)