mirror of
https://github.com/larksuite/cli.git
synced 2026-09-14 18:42:53 +08:00
c5b5aece33
* 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.
93 lines
3.1 KiB
Go
93 lines
3.1 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package errscontract
|
|
|
|
import (
|
|
"go/ast"
|
|
"go/parser"
|
|
"go/token"
|
|
"strings"
|
|
)
|
|
|
|
// CheckNoLegacyRuntimeAPICall flags calls to the runtime's auto-classifying
|
|
// API helpers (CallAPI / DoAPIJSON / DoAPIJSONWithLogID). Those helpers
|
|
// classify a response without the running command's identity context, so a
|
|
// Lark authorization failure cannot carry MissingScopes / ConsoleURL /
|
|
// Identity. Code must call the domain's typed wrapper or runtime.DoAPIJSONTyped
|
|
// / runtime.DoAPI + errclass.BuildAPIError so failures classify into
|
|
// fully-populated typed errs.* errors. forbidigo cannot see these because they
|
|
// are method calls, not output.Err* identifiers — this AST rule covers that gap
|
|
// repo-wide.
|
|
//
|
|
// Applies to every .go path; skips _test.go fixtures. A typed wrapper like
|
|
// driveCallAPI is an unqualified call (*ast.Ident), not a selector, so it is not
|
|
// matched. runtime.DoAPI / runtime.RawAPI are intentionally not listed: they
|
|
// return the raw response for the caller to classify and do not emit a legacy
|
|
// envelope themselves.
|
|
//
|
|
// Files that do not import shortcuts/common are skipped: the legacy helpers
|
|
// are methods on common.RuntimeContext, so a same-named method on another
|
|
// receiver (for example the event domain's APIClient interface, whose
|
|
// implementation classifies into typed errs.* errors) is not a legacy call.
|
|
func CheckNoLegacyRuntimeAPICall(path, src string) []Violation {
|
|
if strings.HasSuffix(path, "_test.go") {
|
|
return nil
|
|
}
|
|
fset := token.NewFileSet()
|
|
file, err := parser.ParseFile(fset, path, src, parser.ParseComments)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
if !importsPath(file, commonImportPath) {
|
|
return nil
|
|
}
|
|
var out []Violation
|
|
ast.Inspect(file, func(n ast.Node) bool {
|
|
call, ok := n.(*ast.CallExpr)
|
|
if !ok {
|
|
return true
|
|
}
|
|
sel, ok := call.Fun.(*ast.SelectorExpr)
|
|
if !ok || sel.Sel == nil {
|
|
return true
|
|
}
|
|
if name, ok := matchLegacyRuntimeAPIMethod(sel.Sel.Name); ok {
|
|
out = append(out, Violation{
|
|
Rule: "no_legacy_runtime_api_call",
|
|
Action: ActionReject,
|
|
File: path,
|
|
Line: fset.Position(call.Pos()).Line,
|
|
Message: "runtime." + name + " classifies the response without the command's identity context (no MissingScopes/ConsoleURL/Identity on authorization failures); it is forbidden",
|
|
Suggestion: "call the domain's typed API wrapper (for example driveCallAPI or callTaskAPITyped) or runtime.DoAPI + errclass.BuildAPIError " +
|
|
"so failures classify into typed errs.* errors",
|
|
})
|
|
}
|
|
return true
|
|
})
|
|
return out
|
|
}
|
|
|
|
// matchLegacyRuntimeAPIMethod returns the name when it is one of the runtime's
|
|
// legacy auto-classifying API helper methods.
|
|
func matchLegacyRuntimeAPIMethod(name string) (string, bool) {
|
|
switch name {
|
|
case "CallAPI", "DoAPIJSON", "DoAPIJSONWithLogID":
|
|
return name, true
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
// importsPath reports whether the file imports the given package path.
|
|
func importsPath(file *ast.File, importPath string) bool {
|
|
for _, imp := range file.Imports {
|
|
if imp.Path == nil {
|
|
continue
|
|
}
|
|
if strings.Trim(imp.Path.Value, "`\"") == importPath {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|