Skip to content

fix: make toToml swallow errors consistent with toYaml and toJson#31915

Open
realgagangupta wants to merge 2 commits intohelm:mainfrom
realgagangupta:fix-toToml-error-return
Open

fix: make toToml swallow errors consistent with toYaml and toJson#31915
realgagangupta wants to merge 2 commits intohelm:mainfrom
realgagangupta:fix-toToml-error-return

Conversation

@realgagangupta
Copy link

@realgagangupta realgagangupta commented Mar 8, 2026

Problem

`toToml` returns `err.Error()` on failure, which renders the raw error message string directly into the template output. This is inconsistent with `toYAML` and `toJSON`, both of which swallow errors silently by returning `""`.

Before:
```go
if err != nil {
return err.Error() // renders error text into template
}
```

After:
```go
if err != nil {
// Swallow errors inside of a template.
return "" // consistent with toYAML and toJSON
}
```

Solution

Changed `toTOML` to return `""` on error, matching the behavior of `toYAML` and `toJSON`.

Fixes #31430

Copilot AI review requested due to automatic review settings March 8, 2026 07:45
@pull-request-size pull-request-size bot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Mar 8, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Aligns Helm template helper behavior by making toToml swallow TOML marshal/encode errors (returning "") to match toYaml/toJson, avoiding raw error strings being rendered into templates.

Changes:

  • Update toTOML to return an empty string on encode errors instead of err.Error().
  • Clarify toTOML behavior in its doc comment (errors are swallowed, consistent with YAML/JSON helpers).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 151 to 156
// toTOML takes an interface, marshals it to toml, and returns a string. It will
// always return a string, even on marshal error (empty string).
// Errors are swallowed, consistent with toYAML and toJSON behavior.
//
// This is designed to be called from a template.
func toTOML(v any) string {
Copy link

Copilot AI Mar 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR description says users can use mustToToml for error visibility, but this repo currently exposes only toToml (mapped to toTOML) and there is no mustToToml equivalent in funcMap() or elsewhere. Either add a mustToTOML/mustToToml function and wire it into the func map (mirroring mustToYaml/mustToJson), or adjust the PR description/docs to avoid referencing a non-existent function.

Copilot uses AI. Check for mistakes.
Comment on lines 156 to 163
func toTOML(v any) string {
b := bytes.NewBuffer(nil)
e := toml.NewEncoder(b)
err := e.Encode(v)
if err != nil {
return err.Error()
// Swallow errors inside of a template.
return ""
}
Copy link

Copilot AI Mar 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes toTOML to swallow encoding errors, but there is no corresponding test asserting the new behavior (unlike toYaml/toJson, which have explicit “swallow error” cases in funcs_test.go). Add a test case that forces TOML encoding to fail (e.g., a cyclic structure) and asserts toToml renders an empty string.

Copilot uses AI. Check for mistakes.
@pull-request-size pull-request-size bot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Mar 8, 2026
Previously toToml returned err.Error() on failure, rendering the error
message string into the template output. toYAML and toJSON both return
an empty string on error to swallow it silently inside templates.

This aligns toToml behavior with the other serialization functions.

Fixes: helm#31430
Signed-off-by: Gagan Gupta <realgagangupta@users.noreply.github.com>
Add a test case that verifies toToml returns "" on encoding errors
(e.g. map[int]string where keys are not strings), consistent with
how toYaml and toJson handle errors.

Signed-off-by: Gagan Gupta <realgagangupta@users.noreply.github.com>
@realgagangupta realgagangupta force-pushed the fix-toToml-error-return branch from e3d9db0 to 68097e3 Compare March 8, 2026 09:49
@TerryHowe
Copy link
Contributor

Description is still talking about the non existent mustToToml

@realgagangupta
Copy link
Author

Good catch! Updated the description — mustToToml doesn't exist so that line was incorrect. Removed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unalignment between toToml and toYaml/toJson

3 participants