@@ -11,6 +11,7 @@ import (
11
11
"fmt"
12
12
"os/exec"
13
13
"runtime"
14
+ "strings"
14
15
15
16
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
16
17
"github.com/hashicorp/terraform-plugin-framework/datasource"
@@ -216,32 +217,25 @@ The program must also be executable according to the platform where Terraform is
216
217
cmd .Dir = workingDir
217
218
cmd .Stdin = bytes .NewReader (queryJson )
218
219
220
+ var stderr strings.Builder
221
+ cmd .Stderr = & stderr
222
+
219
223
tflog .Trace (ctx , "Executing external program" , map [string ]interface {}{"program" : cmd .String ()})
220
224
221
225
resultJson , err := cmd .Output ()
222
226
223
- tflog . Trace ( ctx , "Executed external program" , map [ string ] interface {}{ "program" : cmd .String (), "output" : string ( resultJson )} )
227
+ stderrStr := stderr .String ()
224
228
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 \n Program: %s" , cmd .Path )+
233
- fmt .Sprintf ("\n Error Message: %s" , string (exitErr .Stderr ))+
234
- fmt .Sprintf ("\n State: %s" , err ),
235
- )
236
- return
237
- }
229
+ tflog .Trace (ctx , "Executed external program" , map [string ]interface {}{"program" : cmd .String (), "output" : string (resultJson ), "stderr" : stderrStr })
238
230
231
+ if err != nil {
232
+ if len (stderrStr ) > 0 {
239
233
resp .Diagnostics .AddAttributeError (
240
234
path .Root ("program" ),
241
235
"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." +
244
237
fmt .Sprintf ("\n \n Program: %s" , cmd .Path )+
238
+ fmt .Sprintf ("\n Error Message: %s" , stderrStr )+
245
239
fmt .Sprintf ("\n State: %s" , err ),
246
240
)
247
241
return
@@ -250,9 +244,10 @@ The program must also be executable according to the platform where Terraform is
250
244
resp .Diagnostics .AddAttributeError (
251
245
path .Root ("program" ),
252
246
"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." +
254
249
fmt .Sprintf ("\n \n Program: %s" , cmd .Path )+
255
- fmt .Sprintf ("\n Error : %s" , err ),
250
+ fmt .Sprintf ("\n State : %s" , err ),
256
251
)
257
252
return
258
253
}
0 commit comments