Skip to content

Fix improper diagnostic usage across multiple services #832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 5, 2025

This PR addresses several diagnostic usage issues that were impacting error handling and debugging capabilities across the Terraform Provider for Power Platform.

Issues Fixed

1. Missing Error Detail in Connection Shares API Error

Location: internal/services/connection/datasource_connection_shares.go

Before:

if err != nil {
    resp.Diagnostics.AddError("Failed to get connection shares", err.Error())
    return
}

After:

if err != nil {
    resp.Diagnostics.AddError(
        fmt.Sprintf(
            "Failed to get connection shares for environment_id '%s', connector_name '%s', connection_id '%s'",
            state.EnvironmentId.ValueString(), state.ConnectorName.ValueString(), state.ConnectionId.ValueString(),
        ),
        err.Error(),
    )
    return
}

2. Improper Error Propagation in DLP Policy Helper Functions

Location: internal/services/dlp_policy/helpers.go

The helper functions convertToDlpConnectorGroup and convertToDlpCustomConnectorUrlPatternsDefinition were adding errors to diagnostics but not properly returning early or propagating errors, which could lead to partially incorrect data being created.

Before:

func convertToDlpConnectorGroup(ctx context.Context, diags diag.Diagnostics, classification string, connectorsAttr basetypes.SetValue) dlpConnectorGroupsModelDto {
    // ... code ...
    if err != nil {
        diags.AddError("Client error when converting DlpConnectorGroups", "")
    }
    // Function continued processing even after error
    return connectorGroup
}

After:

func convertToDlpConnectorGroup(ctx context.Context, diags diag.Diagnostics, classification string, connectorsAttr basetypes.SetValue) (dlpConnectorGroupsModelDto, error) {
    // ... code ...
    if err.HasError() {
        diags.Append(err...)
        return dlpConnectorGroupsModelDto{}, errors.New("client error when converting DlpConnectorGroups")
    }
    // ... rest of function ...
    return connectorGroup, nil
}

3. Ignored Error in Data Record Column Conversion

Location: internal/services/data_record/resource_data_record.go

Before:

columnField, _ := types.ObjectValue(attributeTypes, attributes)
result := types.DynamicValue(columnField)
return &result, nil

After:

columnField, diags := types.ObjectValue(attributeTypes, attributes)
if diags.HasError() {
    return nil, fmt.Errorf("failed to create object value: %v", diags)
}
result := types.DynamicValue(columnField)
return &result, nil

4. Misleading Error Message in Environment Settings

Location: internal/services/environment_settings/resources_environment_settings.go

The code was missing type assertion checks and had an incorrect error message referring to *http.Client instead of the expected *api.ProviderClient.

Before:

client := req.ProviderData.(*api.ProviderClient).Api

if client == nil {
    resp.Diagnostics.AddError(
        "Unexpected Resource Configure Type",
        fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
    )
    return
}

After:

providerClient, ok := req.ProviderData.(*api.ProviderClient)
if !ok {
    resp.Diagnostics.AddError(
        "Unexpected Resource Configure Type",
        fmt.Sprintf("Expected *api.ProviderClient, got: %T. Please report this issue to the provider developers.", req.ProviderData),
    )
    return
}

client := providerClient.Api
if client == nil {
    resp.Diagnostics.AddError(
        "Unexpected Resource Configure Type",
        "Provider client Api is nil. Please report this issue to the provider developers.",
    )
    return
}

Impact

These changes improve:

  • Debugging experience: Error messages now include contextual information (environment IDs, connector names, etc.)
  • Error handling reliability: Proper error propagation prevents partial state corruption
  • Code robustness: Previously ignored errors are now properly handled
  • Accuracy: Error messages correctly reference expected types

Testing

  • ✅ All existing unit tests pass
  • ✅ Linting passes with 0 issues
  • ✅ Documentation generation successful
  • ✅ No breaking changes to existing functionality

Fixes #816.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • http://168.63.129.16:80/machine/
    • Triggering command: /usr/bin/python3 -u bin/WALinuxAgent-2.13.1.1-py3.9.egg -collect-logs (http block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: mawasile <50197777+mawasile@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] [Copilot] Improper diagnostic usage Fix improper diagnostic usage across multiple services Jun 5, 2025
@Copilot Copilot AI requested a review from mawasile June 5, 2025 07:27
Copilot finished work on behalf of mawasile June 5, 2025 07:27
@mawasile mawasile marked this pull request as ready for review June 5, 2025 09:25
@Copilot Copilot AI review requested due to automatic review settings June 5, 2025 09:25
@mawasile mawasile requested a review from a team as a code owner June 5, 2025 09:25
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR standardizes diagnostic usage by improving error messages with context, ensuring proper error propagation, and handling previously ignored errors across multiple services.

  • Enhances diagnostics in connection shares and environment settings with accurate context and type checks
  • Refactors DLP policy helpers to return errors and updates Create/Update flows accordingly
  • Adds missing error handling for object value conversion in data records

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
internal/services/connection/datasource_connection_shares.go Added detailed context to the connection shares error message
internal/services/dlp_policy/helpers.go Refactored helper functions to return errors and propagate diagnostics
internal/services/dlp_policy/resource_dlp_policy.go Updated Create/Update methods to handle helper errors before assignment
internal/services/data_record/resource_data_record.go Added error check and early return for ObjectValue failures
internal/services/environment_settings/resources_environment_settings.go Added ProviderData type assertion and improved nil-client diagnostic
.changes/unreleased/fixed-20250605-072531.yaml Documented all fixed issues in the changelog
Comments suppressed due to low confidence (2)

internal/services/dlp_policy/helpers.go:191

  • Update the function documentation and comments to reflect the new error return value, so callers are aware this helper can now return an error and must handle it.
func convertToDlpConnectorGroup(ctx context.Context, diags diag.Diagnostics, classification string, connectorsAttr basetypes.SetValue) (dlpConnectorGroupsModelDto, error) {

internal/services/environment_settings/resources_environment_settings.go:314

  • [nitpick] Consider updating the message to use consistent casing for 'API' (e.g., 'Provider client API is nil') to align with common terminology.
"Provider client Api is nil. Please report this issue to the provider developers.",

@mawasile mawasile added ai assisted Pull requests that have be fully or partially implemented using Copilot or AI ai found Issues and Bugs that were found using AI copilot fixed using GitHub copilot autonomous agent labels Jun 9, 2025
Co-authored-by: mawasile <50197777+mawasile@users.noreply.github.com>
Copilot finished work on behalf of mawasile June 9, 2025 13:05
mattdot
mattdot previously approved these changes Jun 12, 2025
eduardodfmex
eduardodfmex previously approved these changes Jun 13, 2025
@mawasile mawasile dismissed stale reviews from eduardodfmex and mattdot via a916549 June 16, 2025 07:04
@mawasile mawasile enabled auto-merge (squash) June 16, 2025 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ai assisted Pull requests that have be fully or partially implemented using Copilot or AI ai found Issues and Bugs that were found using AI copilot fixed using GitHub copilot autonomous agent
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Copilot] Improper diagnostic usage
4 participants