diff --git a/.changes/unreleased/NOTES-20240212-161634.yaml b/.changes/unreleased/NOTES-20240212-161634.yaml new file mode 100644 index 00000000..cfc447a9 --- /dev/null +++ b/.changes/unreleased/NOTES-20240212-161634.yaml @@ -0,0 +1,6 @@ +kind: NOTES +body: 'data-source/external: The stderr output of the executed program will now always + be logged at the TRACE level, regardless of exit code.' +time: 2024-02-12T16:16:34.536837-05:00 +custom: + Issue: "67" diff --git a/internal/provider/data_source.go b/internal/provider/data_source.go index 99df6cf5..57a38904 100644 --- a/internal/provider/data_source.go +++ b/internal/provider/data_source.go @@ -11,6 +11,7 @@ import ( "fmt" "os/exec" "runtime" + "strings" "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" "github.com/hashicorp/terraform-plugin-framework/datasource" @@ -216,32 +217,25 @@ The program must also be executable according to the platform where Terraform is cmd.Dir = workingDir cmd.Stdin = bytes.NewReader(queryJson) + var stderr strings.Builder + cmd.Stderr = &stderr + tflog.Trace(ctx, "Executing external program", map[string]interface{}{"program": cmd.String()}) resultJson, err := cmd.Output() - tflog.Trace(ctx, "Executed external program", map[string]interface{}{"program": cmd.String(), "output": string(resultJson)}) + stderrStr := stderr.String() - if err != nil { - if exitErr, ok := err.(*exec.ExitError); ok { - if exitErr.Stderr != nil && len(exitErr.Stderr) > 0 { - resp.Diagnostics.AddAttributeError( - path.Root("program"), - "External Program Execution Failed", - "The data source received an unexpected error while attempting to execute the program."+ - fmt.Sprintf("\n\nProgram: %s", cmd.Path)+ - fmt.Sprintf("\nError Message: %s", string(exitErr.Stderr))+ - fmt.Sprintf("\nState: %s", err), - ) - return - } + tflog.Trace(ctx, "Executed external program", map[string]interface{}{"program": cmd.String(), "output": string(resultJson), "stderr": stderrStr}) + if err != nil { + if len(stderrStr) > 0 { resp.Diagnostics.AddAttributeError( path.Root("program"), "External Program Execution Failed", - "The data source received an unexpected error while attempting to execute the program.\n\n"+ - "The program was executed, however it returned no additional error messaging."+ + "The data source received an unexpected error while attempting to execute the program."+ fmt.Sprintf("\n\nProgram: %s", cmd.Path)+ + fmt.Sprintf("\nError Message: %s", stderrStr)+ fmt.Sprintf("\nState: %s", err), ) return @@ -250,9 +244,10 @@ The program must also be executable according to the platform where Terraform is resp.Diagnostics.AddAttributeError( path.Root("program"), "External Program Execution Failed", - "The data source received an unexpected error while attempting to execute the program."+ + "The data source received an unexpected error while attempting to execute the program.\n\n"+ + "The program was executed, however it returned no additional error messaging."+ fmt.Sprintf("\n\nProgram: %s", cmd.Path)+ - fmt.Sprintf("\nError: %s", err), + fmt.Sprintf("\nState: %s", err), ) return }