mirror of
https://github.com/larksuite/cli.git
synced 2026-07-03 14:02:43 +08:00
* refactor: retire legacy error envelopes and enforce typed contract
Consolidate all command error reporting onto the typed errs.* contract, remove
the legacy error surface that predated it, and tighten the lint guards so the
contract holds across the whole repository going forward.
Every failure now reaches stderr as one envelope shape: a category, an
optional subtype, a human- and agent-readable message, and a recovery hint,
with invalid parameters listed under `params`. The legacy ExitError envelope,
its constructors, and the boundary bridge that promoted untyped config and
authorization errors are deleted, leaving a single path from error to wire.
Predicate commands keep their silent-exit behavior through a dedicated signal
that carries only an exit code.
Infrastructure paths that still emitted ad-hoc envelopes — flag parsing,
unknown commands and subcommands, plugin and policy guards, confirmation
prompts, and auth/config failures — now classify into the same taxonomy.
Business, API, auth, and config exit codes are preserved; the one behavioral
change is that Cobra usage failures (missing required flag, unknown command,
bad arguments) now emit the typed validation envelope and exit 2, matching the
explicit flag and subcommand guards, instead of Cobra's plain-text exit 1.
Enforcement is repo-wide rather than per-path:
- The errscontract guards run by default everywhere instead of through a
migration allowlist, so legacy envelopes cannot be reintroduced anywhere.
- errorlint runs across the whole repository: every error wrap must use %w and
every comparison must use errors.Is/errors.As, so interior wraps stay legal
but can no longer break the chain the typed boundary relies on.
- The errs-no-bare-wrap guard is keyed by structural prefix instead of an
explicit per-domain allowlist, so new shortcut domains are covered without
editing a list. It runs where forbidigo is enabled (the shortcut domains and
the auth/config/service command groups); repo-wide chain integrity for the
remaining command paths is carried by errorlint above.
* test: align cli_e2e success assertions to the ok envelope
The api and service success path now emits the {"ok":true} envelope, so the
cli_e2e workflow assertions that still expected the old {"code":0} shape via
AssertStdoutStatus(t, 0) fail once they run with live credentials. Switch those
workflow assertions to AssertStdoutStatus(t, true); the fake-payload helper test
in core_test.go keeps its code-shape assertion.
105 lines
3.5 KiB
Go
105 lines
3.5 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/larksuite/cli/errs"
|
|
"github.com/larksuite/cli/internal/output"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func TestUnknownFlagName(t *testing.T) {
|
|
cases := []struct {
|
|
in string
|
|
name string
|
|
ok bool
|
|
}{
|
|
{"unknown flag: --query", "query", true},
|
|
{"unknown flag: --with-styles", "with-styles", true},
|
|
{"unknown shorthand flag: 'z' in -z", "", false},
|
|
{"flag needs an argument: --find", "", false},
|
|
{`invalid argument "x" for "--count"`, "", false},
|
|
}
|
|
for _, c := range cases {
|
|
name, ok := unknownFlagName(errors.New(c.in))
|
|
if name != c.name || ok != c.ok {
|
|
t.Errorf("unknownFlagName(%q) = (%q,%v), want (%q,%v)", c.in, name, ok, c.name, c.ok)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFlagDidYouMean_UnknownFlagSuggestsAndListsValid(t *testing.T) {
|
|
c := &cobra.Command{Use: "demo"}
|
|
c.Flags().String("range", "", "")
|
|
c.Flags().String("find", "", "")
|
|
c.Flags().Bool("dry-run", false, "")
|
|
|
|
err := flagDidYouMean(c, errors.New("unknown flag: --rang")) // typo of --range
|
|
var verr *errs.ValidationError
|
|
if !errors.As(err, &verr) {
|
|
t.Fatalf("expected *errs.ValidationError, got %T", err)
|
|
}
|
|
if verr.Subtype != errs.SubtypeInvalidArgument {
|
|
t.Errorf("subtype = %q, want invalid_argument", verr.Subtype)
|
|
}
|
|
if code := output.ExitCodeOf(err); code != output.ExitValidation {
|
|
t.Errorf("exit code = %d, want %d (ExitValidation)", code, output.ExitValidation)
|
|
}
|
|
// The offending flag is carried structurally on Params (replaces the
|
|
// legacy detail map) and named in the message.
|
|
if len(verr.Params) != 1 || verr.Params[0].Name != "--rang" {
|
|
t.Errorf("Params = %v, want one entry named --rang", verr.Params)
|
|
}
|
|
if len(verr.Params) == 1 && verr.Params[0].Reason == "" {
|
|
t.Error("Params[0].Reason must explain the rejection")
|
|
}
|
|
if !strings.Contains(verr.Message, "--rang") {
|
|
t.Errorf("message should name the offending flag, got %q", verr.Message)
|
|
}
|
|
// The ranked candidate rides on the param as a machine-readable suggestion
|
|
// so an agent can retry without parsing prose.
|
|
if len(verr.Params) == 1 {
|
|
found := false
|
|
for _, s := range verr.Params[0].Suggestions {
|
|
if s == "--range" {
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
t.Errorf("Params[0].Suggestions should include --range, got %v", verr.Params[0].Suggestions)
|
|
}
|
|
}
|
|
// The same candidate is also carried in the human-facing hint.
|
|
if !strings.Contains(verr.Hint, "--range") {
|
|
t.Errorf("hint should suggest --range, got %q", verr.Hint)
|
|
}
|
|
}
|
|
|
|
func TestFlagDidYouMean_OtherErrorStaysGeneric(t *testing.T) {
|
|
c := &cobra.Command{Use: "demo"}
|
|
err := flagDidYouMean(c, errors.New("flag needs an argument: --find"))
|
|
var verr *errs.ValidationError
|
|
if !errors.As(err, &verr) {
|
|
t.Fatalf("expected *errs.ValidationError, got %T", err)
|
|
}
|
|
// Non-unknown-flag errors stay generic: invalid_argument subtype, no
|
|
// structured param, generic --help hint (no "did you mean" suggestion).
|
|
if verr.Subtype != errs.SubtypeInvalidArgument {
|
|
t.Errorf("subtype = %q, want invalid_argument (non-unknown-flag errors stay generic)", verr.Subtype)
|
|
}
|
|
if code := output.ExitCodeOf(err); code != output.ExitValidation {
|
|
t.Errorf("exit code = %d, want %d (ExitValidation)", code, output.ExitValidation)
|
|
}
|
|
if verr.Param != "" || len(verr.Params) != 0 {
|
|
t.Errorf("Param=%q Params=%v, want both empty for generic flag error", verr.Param, verr.Params)
|
|
}
|
|
if strings.Contains(verr.Hint, "did you mean") {
|
|
t.Errorf("generic flag error must not produce a did-you-mean hint, got %q", verr.Hint)
|
|
}
|
|
}
|