Skip to content

Commit

Permalink
more error handling improvements (#370)
Browse files Browse the repository at this point in the history
This fixes some cases where the underlying errors can be swallowed and
not visible to the user.
  • Loading branch information
pgier authored Feb 26, 2024
1 parent d3d2cee commit 92893ba
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
10 changes: 3 additions & 7 deletions internal/provider/resource_streaming_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,16 +402,12 @@ func (r *StreamingTopicResource) Read(ctx context.Context, req resource.ReadRequ

//schema := &StreamingTopicSchema{}
params := pulsaradmin.SchemasResourceGetSchemaParams{}
schemaResp, err := r.clients.pulsarAdminClient.SchemasResourceGetSchemaWithResponse(ctx, tenant, namespace, topic,
&params, streamingRequestHeaders)
schemaResp, err := r.clients.pulsarAdminClient.SchemasResourceGetSchemaWithResponse(ctx, tenant, namespace, topic, &params, streamingRequestHeaders)
if err != nil {
resp.Diagnostics.AddError(
"Failed to get topic schema",
err.Error(),
)
resp.Diagnostics.AddError("Failed to get topic schema", err.Error())
return
} else if schemaResp.StatusCode() > 299 && schemaResp.StatusCode() != 404 {
resp.Diagnostics.Append(HTTPResponseDiagWarn(schemaResp.HTTPResponse, err, "Failed to get topic schema")...)
resp.Diagnostics.Append(HTTPResponseDiagWarnWithBody(schemaResp.StatusCode(), schemaResp.Body, err, "failed to get topic schema")...)
} else if schemaResp.JSON200 != nil {
state.Schema = &StreamingTopicSchema{
Type: (*string)(schemaResp.JSON200.Type),
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/types_streaming_namespace_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func getPulsarNamespacePolicies(ctx context.Context, pulsarAdminClient *pulsarad
policiesAttrTypes := plan.Policies.AttributeTypes(ctx)

resp, err := pulsarAdminClient.NamespacesGetPoliciesWithResponse(ctx, plan.Tenant.ValueString(), plan.Namespace.ValueString(), requestEditors...)
diags.Append(HTTPResponseDiagErr(resp.HTTPResponse, err, "failed to get namespace policies")...)
diags.Append(HTTPResponseDiagErrWithBody(resp.StatusCode(), resp.Body, err, "failed to get namespace policies")...)
if diags.HasError() {
return types.ObjectNull(policiesAttrTypes), diags
}
Expand Down
28 changes: 26 additions & 2 deletions internal/provider/util_terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func MergeTerraformObjects(old, new types.Object, attributeTypes map[string]attr
return basetypes.NewObjectValue(attributeTypes, attributes)
}

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

// HTTPResponseDiagWarn takes an HTTP response and error code and creates a Terraform Warn Diagnostic if there is an error
// HTTPResponseDiagErrWithBody takes an HTTP status code, body, and error and creates a Terraform Error Diagnostic if there is an error
func HTTPResponseDiagErrWithBody(statusCode int, body []byte, err error, errorSummary string) diag.Diagnostics {
diags := diag.Diagnostics{}
if err != nil {
diags.AddError(errorSummary, err.Error())
} else if statusCode >= 300 {
details := fmt.Sprintf("Received status code: '%v', with message: %s", statusCode, body)
diags.AddError(errorSummary, details)
}
return diags
}

// HTTPResponseDiagWarn takes an HTTP response and error and creates a Terraform Warn Diagnostic if there is an error
// or if the status code is not in the 2xx range
func HTTPResponseDiagWarn(resp *http.Response, err error, errorSummary string) diag.Diagnostics {
diags := diag.Diagnostics{}
Expand All @@ -131,6 +143,18 @@ func HTTPResponseDiagWarn(resp *http.Response, err error, errorSummary string) d
return diags
}

// HTTPResponseDiagWarnWithBody takes an HTTP status code, body, and error and creates a Terraform Error Diagnostic if there is an error
func HTTPResponseDiagWarnWithBody(statusCode int, body []byte, err error, errorSummary string) diag.Diagnostics {
diags := diag.Diagnostics{}
if err != nil {
diags.AddWarning(errorSummary, err.Error())
} else if statusCode >= 300 {
details := fmt.Sprintf("Received status code: '%v', with message: %s", statusCode, body)
diags.AddWarning(errorSummary, details)
}
return diags
}

// planModifierStringValueChanged is a terraform plan modifier function to use with 'RequiresReplaceIf' to check if a string value
// changed from one value to another, not including null values.
func planModifierStringValueChanged() stringplanmodifier.RequiresReplaceIfFunc {
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func main() {
providerserver.NewProtocol6(provider.New(version)()),
}

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

if err != nil {
Expand Down

0 comments on commit 92893ba

Please sign in to comment.