Skip to content

Commit c41aca4

Browse files
committed
fix(init): drop redundant error wrap when --preset is missing on --update
readChartYamlPreset returns errors.WithHint('preset not found in Chart.yaml dependencies', 'add a preset chart…'); the call site in updateTalmLibraryChart wrapped that again with errors.Wrap(err, 'preset is required: use --preset flag or ensure Chart.yaml has a preset dependency'). The rendered error became 'preset is required: …: preset not found in Chart.yaml dependencies' — the same fact twice. Return the inner error directly; its hint already covers both recovery paths. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 9cdcaeb commit c41aca4

2 files changed

Lines changed: 74 additions & 1 deletion

File tree

‎pkg/commands/init.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,11 @@ func updateTalmLibraryChart() error {
11761176

11771177
presetName, err = readChartYamlPreset()
11781178
if err != nil {
1179-
return errors.Wrap(err, "preset is required: use --preset flag or ensure Chart.yaml has a preset dependency")
1179+
// readChartYamlPreset already returns errors.WithHint("preset
1180+
// not found in Chart.yaml dependencies", "add a preset chart…");
1181+
// wrapping that again would double-message the operator with
1182+
// the same fact.
1183+
return err
11801184
}
11811185
}
11821186

‎pkg/commands/init_test.go‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"strings"
2323
"testing"
2424

25+
"github.com/cockroachdb/errors"
2526
"gopkg.in/yaml.v3"
2627
)
2728

@@ -350,6 +351,74 @@ func TestUpdateTalmLibraryChartRejectsImageFlag(t *testing.T) {
350351
}
351352
}
352353

354+
// TestInitUpdate_PresetNotFoundError_SingleWrapped pins that the
355+
// "preset not declared anywhere" error path for `talm init --update`
356+
// surfaces once, not twice. readChartYamlPreset already returns a
357+
// cockroachdb/errors.WithHint("preset not found in Chart.yaml
358+
// dependencies", "add a preset chart…"); wrapping that again with
359+
// errors.Wrap(err, "preset is required: use --preset flag or ensure
360+
// Chart.yaml has a preset dependency") double-messages the operator:
361+
// the rendered error becomes "preset is required: …: preset not
362+
// found in Chart.yaml dependencies" — same fact twice. The fix is to
363+
// return the inner error directly; its hint already covers both
364+
// recovery paths (add a preset dependency OR pass --preset).
365+
func TestInitUpdate_PresetNotFoundError_SingleWrapped(t *testing.T) {
366+
imageOrig := initCmdFlags.image
367+
presetOrig := initCmdFlags.preset
368+
rootOrig := Config.RootDir
369+
t.Cleanup(func() {
370+
initCmdFlags.image = imageOrig
371+
initCmdFlags.preset = presetOrig
372+
Config.RootDir = rootOrig
373+
})
374+
375+
initCmdFlags.image = ""
376+
initCmdFlags.preset = ""
377+
378+
dir := t.TempDir()
379+
Config.RootDir = dir
380+
381+
// Chart.yaml carrying only the `talm` library dependency, no
382+
// preset chart. readChartYamlPreset will iterate the deps and
383+
// return its "preset not found" error.
384+
chartYaml := "apiVersion: v2\nname: test\nversion: 0.1.0\n" +
385+
"dependencies:\n" +
386+
" - name: talm\n" +
387+
" version: 0.1.0\n"
388+
if err := os.WriteFile(filepath.Join(dir, "Chart.yaml"), []byte(chartYaml), 0o644); err != nil {
389+
t.Fatalf("seed Chart.yaml: %v", err)
390+
}
391+
392+
err := updateTalmLibraryChart()
393+
if err == nil {
394+
t.Fatal("expected --update without preset to error; got nil")
395+
}
396+
397+
msg := err.Error()
398+
399+
// Double-wrap signature: the outer wrap text leaks into the
400+
// rendered string. After the fix this substring is gone.
401+
if strings.Contains(msg, "preset is required:") {
402+
t.Errorf("error must be single-wrapped — found outer wrap text %q in:\n%s", "preset is required:", msg)
403+
}
404+
405+
// The inner cause must survive untouched.
406+
if !strings.Contains(msg, "preset not found in Chart.yaml dependencies") {
407+
t.Errorf("error must surface the inner cause; got:\n%s", msg)
408+
}
409+
410+
// The hint chain must still surface the recovery path.
411+
hints := errors.GetAllHints(err)
412+
if len(hints) == 0 {
413+
t.Fatalf("expected at least one hint guiding the operator on how to declare a preset; got bare error:\n%s", msg)
414+
}
415+
416+
combined := strings.Join(hints, "\n")
417+
if !strings.Contains(combined, "add a preset chart") {
418+
t.Errorf("hint chain must mention adding a preset chart (or equivalent recovery); got:\n%s", combined)
419+
}
420+
}
421+
353422
// TestValidateImageRefShape_RejectsMalformed pins the shape check
354423
// that runs before any --image value reaches the preset rewrite.
355424
// Catches operator typos (::malformed, no-slash:tag, trailing

0 commit comments

Comments
 (0)