Skip to content

Fix missing return statements after error handling in datasources #827

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

Merged
merged 5 commits into from
Jun 16, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 4, 2025

This PR fixes critical error handling issues in three datasource files where missing return statements after error diagnostics could lead to undefined behavior, panics, or data corruption.

Issues Fixed

1. Connectors Datasource (internal/services/connectors/datasource_connectors.go)

Problem: When GetConnectors() fails, an error is added to diagnostics but execution continues, potentially using invalid connectors data.

// Before - missing return after error
connectors, err := d.ConnectorsClient.GetConnectors(ctx)
if err != nil {
    resp.Diagnostics.AddError(fmt.Sprintf("Client error when reading %s", d.FullTypeName()), fmt.Errorf("error occurred: %w", err).Error())
}
// Code continues executing with potentially nil/invalid connectors

// After - proper early return
connectors, err := d.ConnectorsClient.GetConnectors(ctx)
if err != nil {
    resp.Diagnostics.AddError(fmt.Sprintf("Client error when reading %s", d.FullTypeName()), fmt.Errorf("error occurred: %w", err).Error())
    return
}

2. Solutions Datasource (internal/services/solution/datasource_solutions.go)

Problem: When DataverseExists() fails, an error is added to diagnostics but execution continues with undefined dvExits value.

// Before - missing return after error
dvExits, err := d.SolutionClient.DataverseExists(ctx, state.EnvironmentId.ValueString())
if err != nil {
    resp.Diagnostics.AddError(fmt.Sprintf("Client error when checking if Dataverse exists in environment '%s'", state.EnvironmentId.ValueString()), err.Error())
}
// Code continues with dvExits having Go default value (false)

// After - proper early return
dvExits, err := d.SolutionClient.DataverseExists(ctx, state.EnvironmentId.ValueString())
if err != nil {
    resp.Diagnostics.AddError(fmt.Sprintf("Client error when checking if Dataverse exists in environment '%s'", state.EnvironmentId.ValueString()), err.Error())
    return
}

3. Tenant Application Packages Datasource (internal/services/application/datasource_tenant_application_packages.go)

Problem: resp.State.Get() doesn't check for errors in returned diagnostics, which could lead to panics with malformed state.

// Before - no error checking
var state TenantApplicationPackagesListDataSourceModel
resp.State.Get(ctx, &state)

// After - proper error handling
var state TenantApplicationPackagesListDataSourceModel
diags := resp.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
    return
}

Impact

These fixes prevent potential runtime panics, data corruption, and undefined behavior by ensuring proper error handling flow in critical datasource operations.

Testing

  • ✅ All unit tests pass with good coverage
  • ✅ Linting passes with 0 issues
  • ✅ Build completes successfully
  • ✅ Documentation generation works correctly

Fixes #814.


💡 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.

Copilot AI and others added 2 commits June 4, 2025 17:50
Co-authored-by: mawasile <50197777+mawasile@users.noreply.github.com>
Co-authored-by: mawasile <50197777+mawasile@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] [Copilot] Missing Return After Error Handling Issues Fix missing return statements after error handling in datasources Jun 4, 2025
@Copilot Copilot AI requested a review from mawasile June 4, 2025 17:52
Copilot finished work on behalf of mawasile June 4, 2025 17:52
@mawasile mawasile marked this pull request as ready for review June 5, 2025 09:17
@Copilot Copilot AI review requested due to automatic review settings June 5, 2025 09:17
@mawasile mawasile requested a review from a team as a code owner June 5, 2025 09:17
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 ensures proper early returns after error handling in three datasource implementations to prevent panics and undefined behavior.

  • Adds return statements after client error diagnostics in Connectors and Solutions datasources.
  • Introduces diagnostic error checking and early return for resp.State.Get in Tenant Application Packages datasource.
  • Updates changelog with the fix for issue [Copilot] Missing Return After Error Handling Issues #814.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
internal/services/solution/datasource_solutions.go Added return after DataverseExists error diagnostics
internal/services/connectors/datasource_connectors.go Added return after GetConnectors error diagnostics
internal/services/application/datasource_tenant_application_packages.go Appended diagnostics from resp.State.Get and early return on error
.changes/unreleased/fixed-20250604-175056.yaml Added changelog entry for this fix
Comments suppressed due to low confidence (3)

internal/services/solution/datasource_solutions.go:137

  • Add a unit test to simulate DataverseExists returning an error and verify that resp.Diagnostics contains the expected error and the method returns immediately without further processing.
return

internal/services/connectors/datasource_connectors.go:126

  • Add a test case where GetConnectors returns an error to confirm that diagnostics are recorded and execution stops as intended.
return

internal/services/application/datasource_tenant_application_packages.go:174

  • Introduce a test for a malformed state scenario where resp.State.Get returns diagnostics with errors, ensuring the new early return path is exercised.
diags := resp.State.Get(ctx, &state)

@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
@mawasile mawasile merged commit 1e880cb into main Jun 16, 2025
11 checks passed
@mawasile mawasile deleted the copilot/fix-814 branch June 16, 2025 07:19
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

Error loading sessions

Retrying...

Successfully merging this pull request may close these issues.

[Copilot] Missing Return After Error Handling Issues
4 participants