From c18f845a7ab7e3fb604e4271925e6cbd2142426f Mon Sep 17 00:00:00 2001 From: Patrick Decat Date: Mon, 3 Apr 2023 15:15:23 +0200 Subject: [PATCH] feat: Also log command stderr at TRACE level --- internal/provider/data_source.go | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/internal/provider/data_source.go b/internal/provider/data_source.go index d96533c0..2d767b5a 100644 --- a/internal/provider/data_source.go +++ b/internal/provider/data_source.go @@ -8,6 +8,7 @@ import ( "fmt" "os/exec" "runtime" + "strings" "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" "github.com/hashicorp/terraform-plugin-framework/datasource" @@ -202,32 +203,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", string(stderrStr))+ fmt.Sprintf("\nState: %s", err), ) return @@ -236,9 +230,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 }