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.
122 lines
4.2 KiB
Go
122 lines
4.2 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package config
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/larksuite/cli/errs"
|
|
"github.com/larksuite/cli/internal/core"
|
|
"github.com/larksuite/cli/internal/output"
|
|
)
|
|
|
|
// updateExistingProfileWithoutSecret guards four blank-input scenarios. Each
|
|
// must surface as *ValidationError(SubtypeInvalidArgument) per RFC 6749 §5.2:
|
|
// SubtypeInvalidClient is reserved for IAM rejection of malformed credentials,
|
|
// not for missing user input.
|
|
|
|
func TestUpdateExistingProfileWithoutSecret_NilConfig_EmitsValidationError(t *testing.T) {
|
|
err := updateExistingProfileWithoutSecret(nil, "", "cli_test", core.BrandFeishu, "en")
|
|
assertValidationParam(t, err, "--app-secret")
|
|
}
|
|
|
|
func TestUpdateExistingProfileWithoutSecret_UnknownProfile_EmitsValidationError(t *testing.T) {
|
|
existing := &core.MultiAppConfig{
|
|
Apps: []core.AppConfig{{
|
|
Name: "default",
|
|
AppId: "app-default",
|
|
AppSecret: core.PlainSecret("secret-default"),
|
|
Brand: core.BrandFeishu,
|
|
}},
|
|
}
|
|
err := updateExistingProfileWithoutSecret(existing, "missing-profile", "cli_test", core.BrandFeishu, "en")
|
|
assertValidationParam(t, err, "--app-secret")
|
|
}
|
|
|
|
func TestUpdateExistingProfileWithoutSecret_NoCurrentApp_EmitsValidationError(t *testing.T) {
|
|
existing := &core.MultiAppConfig{
|
|
CurrentApp: "missing",
|
|
Apps: []core.AppConfig{{
|
|
Name: "default",
|
|
AppId: "app-default",
|
|
AppSecret: core.PlainSecret("secret-default"),
|
|
Brand: core.BrandFeishu,
|
|
}},
|
|
}
|
|
err := updateExistingProfileWithoutSecret(existing, "", "cli_test", core.BrandFeishu, "en")
|
|
assertValidationParam(t, err, "--app-secret")
|
|
}
|
|
|
|
func TestUpdateExistingProfileWithoutSecret_AppIdMismatch_EmitsValidationError(t *testing.T) {
|
|
existing := &core.MultiAppConfig{
|
|
Apps: []core.AppConfig{{
|
|
Name: "default",
|
|
AppId: "app-default",
|
|
AppSecret: core.PlainSecret("secret-default"),
|
|
Brand: core.BrandFeishu,
|
|
}},
|
|
}
|
|
err := updateExistingProfileWithoutSecret(existing, "", "cli_different", core.BrandFeishu, "en")
|
|
assertValidationParam(t, err, "--app-secret")
|
|
}
|
|
|
|
// wrapUpdateExistingProfileErr is the caller-side classifier for the error
|
|
// returned by updateExistingProfileWithoutSecret. It must preserve typed-error
|
|
// exit semantics: a typed ValidationError must keep ExitValidation rather than
|
|
// being downgraded to InternalError.
|
|
|
|
func TestWrapUpdateExistingProfileErr_NilPassesThrough(t *testing.T) {
|
|
if got := wrapUpdateExistingProfileErr(nil); got != nil {
|
|
t.Fatalf("expected nil, got %v", got)
|
|
}
|
|
}
|
|
|
|
func TestWrapUpdateExistingProfileErr_TypedValidationErrorPreserved(t *testing.T) {
|
|
in := errs.NewValidationError(errs.SubtypeInvalidArgument, "App Secret cannot be empty for new profile").
|
|
WithParam("--app-secret")
|
|
got := wrapUpdateExistingProfileErr(in)
|
|
assertValidationParam(t, got, "--app-secret")
|
|
// Exit code must remain ExitValidation (2), not ExitInternal (5).
|
|
if code := output.ExitCodeOf(got); code != output.ExitValidation {
|
|
t.Errorf("ExitCodeOf = %d, want %d (ExitValidation)", code, output.ExitValidation)
|
|
}
|
|
// Must NOT be wrapped as *InternalError.
|
|
var intErr *errs.InternalError
|
|
if errors.As(got, &intErr) {
|
|
t.Errorf("typed ValidationError was downgraded to *InternalError: %v", got)
|
|
}
|
|
}
|
|
|
|
func TestWrapUpdateExistingProfileErr_UntypedErrorBecomesInternal(t *testing.T) {
|
|
in := fmt.Errorf("disk full")
|
|
got := wrapUpdateExistingProfileErr(in)
|
|
var intErr *errs.InternalError
|
|
if !errors.As(got, &intErr) {
|
|
t.Fatalf("expected *errs.InternalError, got %T: %v", got, got)
|
|
}
|
|
if intErr.Subtype != errs.SubtypeSDKError {
|
|
t.Errorf("Subtype = %q, want %q", intErr.Subtype, errs.SubtypeSDKError)
|
|
}
|
|
}
|
|
|
|
// assertValidationParam asserts err is *ValidationError with the given Param.
|
|
func assertValidationParam(t *testing.T, err error, wantParam string) {
|
|
t.Helper()
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
var valErr *errs.ValidationError
|
|
if !errors.As(err, &valErr) {
|
|
t.Fatalf("expected *errs.ValidationError, got %T: %v", err, err)
|
|
}
|
|
if valErr.Subtype != errs.SubtypeInvalidArgument {
|
|
t.Errorf("Subtype = %q, want %q", valErr.Subtype, errs.SubtypeInvalidArgument)
|
|
}
|
|
if valErr.Param != wantParam {
|
|
t.Errorf("Param = %q, want %q", valErr.Param, wantParam)
|
|
}
|
|
}
|