Skip to content

Commit 0998e0f

Browse files
authored
feat: Also log command stderr at TRACE level (#206)
* feat: Also log command stderr at TRACE level
1 parent 4365dbd commit 0998e0f

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: NOTES
2+
body: 'data-source/external: The stderr output of the executed program will now always
3+
be logged at the TRACE level, regardless of exit code.'
4+
time: 2024-02-12T16:16:34.536837-05:00
5+
custom:
6+
Issue: "67"

internal/provider/data_source.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"os/exec"
1313
"runtime"
14+
"strings"
1415

1516
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
1617
"github.com/hashicorp/terraform-plugin-framework/datasource"
@@ -216,32 +217,25 @@ The program must also be executable according to the platform where Terraform is
216217
cmd.Dir = workingDir
217218
cmd.Stdin = bytes.NewReader(queryJson)
218219

220+
var stderr strings.Builder
221+
cmd.Stderr = &stderr
222+
219223
tflog.Trace(ctx, "Executing external program", map[string]interface{}{"program": cmd.String()})
220224

221225
resultJson, err := cmd.Output()
222226

223-
tflog.Trace(ctx, "Executed external program", map[string]interface{}{"program": cmd.String(), "output": string(resultJson)})
227+
stderrStr := stderr.String()
224228

225-
if err != nil {
226-
if exitErr, ok := err.(*exec.ExitError); ok {
227-
if exitErr.Stderr != nil && len(exitErr.Stderr) > 0 {
228-
resp.Diagnostics.AddAttributeError(
229-
path.Root("program"),
230-
"External Program Execution Failed",
231-
"The data source received an unexpected error while attempting to execute the program."+
232-
fmt.Sprintf("\n\nProgram: %s", cmd.Path)+
233-
fmt.Sprintf("\nError Message: %s", string(exitErr.Stderr))+
234-
fmt.Sprintf("\nState: %s", err),
235-
)
236-
return
237-
}
229+
tflog.Trace(ctx, "Executed external program", map[string]interface{}{"program": cmd.String(), "output": string(resultJson), "stderr": stderrStr})
238230

231+
if err != nil {
232+
if len(stderrStr) > 0 {
239233
resp.Diagnostics.AddAttributeError(
240234
path.Root("program"),
241235
"External Program Execution Failed",
242-
"The data source received an unexpected error while attempting to execute the program.\n\n"+
243-
"The program was executed, however it returned no additional error messaging."+
236+
"The data source received an unexpected error while attempting to execute the program."+
244237
fmt.Sprintf("\n\nProgram: %s", cmd.Path)+
238+
fmt.Sprintf("\nError Message: %s", stderrStr)+
245239
fmt.Sprintf("\nState: %s", err),
246240
)
247241
return
@@ -250,9 +244,10 @@ The program must also be executable according to the platform where Terraform is
250244
resp.Diagnostics.AddAttributeError(
251245
path.Root("program"),
252246
"External Program Execution Failed",
253-
"The data source received an unexpected error while attempting to execute the program."+
247+
"The data source received an unexpected error while attempting to execute the program.\n\n"+
248+
"The program was executed, however it returned no additional error messaging."+
254249
fmt.Sprintf("\n\nProgram: %s", cmd.Path)+
255-
fmt.Sprintf("\nError: %s", err),
250+
fmt.Sprintf("\nState: %s", err),
256251
)
257252
return
258253
}

0 commit comments

Comments
 (0)