Skip to content

Commit

Permalink
Append outputs of std.trace() function to the error diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
alxrem committed Dec 17, 2024
1 parent d261a12 commit a1ebd33
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.4.1 (Dec 17, 2024)

IMPROVEMENTS:

* Append outputs of std.trace() function to the error diagnostics.

## 2.4.0 (Dec 16, 2024)

IMPROVEMENTS:
Expand Down
9 changes: 7 additions & 2 deletions private/provider/data_source_jsonnet_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,18 @@ func (d *JsonnetFileDataSource) Read(ctx context.Context, req datasource.ReadReq
}
}()

trace := traceOut.String()

if err != nil {
resp.Diagnostics.AddError("Failed to render test from jsonnet template", err.Error())
resp.Diagnostics.AddError(
"Failed to render test from jsonnet template",
strings.Join([]string{err.Error(), trace}, ""),
)
return
}

state.Rendered = types.StringValue(rendered)
state.Trace = types.StringValue(traceOut.String())
state.Trace = types.StringValue(trace)
state.Id = types.StringValue(hash(rendered))

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
Expand Down
21 changes: 21 additions & 0 deletions private/provider/data_source_jsonnet_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,24 @@ func TestDataSourceJsonnetFile_RenderContentWithTrace(t *testing.T) {
},
})
}

func TestDataSourceJsonnetFile_AddTraceToDiagnostics(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
data "jsonnet_file" "template" {
content = <<EOF
{
"a": std.trace("str", "rest"),
"b": 1/0,
}
EOF
}
`,
ExpectError: regexp.MustCompile(`(?s)RUNTIME ERROR: Division by zero\..*TRACE: data:2 str`),
},
},
})
}

0 comments on commit a1ebd33

Please sign in to comment.