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.
96 lines
3.6 KiB
Go
96 lines
3.6 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package output
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/larksuite/cli/errs"
|
|
)
|
|
|
|
// PartialFailureError is the exit signal for a batch / multi-status command that
|
|
// has already written an ok:false result envelope to stdout. The per-item
|
|
// outcomes are the primary, machine-readable output and live on stdout, so the
|
|
// dispatcher sets only the exit code and writes nothing to stderr.
|
|
//
|
|
// It is deliberately distinct from ErrBare (the stdout-carries-the-answer
|
|
// silent-exit signal) so that contract stays narrow, and from a typed *errs.XxxError
|
|
// (which owns the stderr error envelope): a partial failure is a result, not an
|
|
// error envelope.
|
|
type PartialFailureError struct {
|
|
Code int
|
|
}
|
|
|
|
func (e *PartialFailureError) Error() string {
|
|
return fmt.Sprintf("partial failure (exit %d)", e.Code)
|
|
}
|
|
|
|
// PartialFailure builds the partial-failure exit signal with the given code.
|
|
func PartialFailure(code int) *PartialFailureError {
|
|
return &PartialFailureError{Code: code}
|
|
}
|
|
|
|
// WriteTypedErrorEnvelope writes the JSON error envelope for a typed error.
|
|
// Each typed error owns its wire shape via its own struct tags: Problem fields
|
|
// are promoted to the top level through embedding, and extension fields
|
|
// (MissingScopes, ChallengeURL, etc.) sit alongside as siblings — not inside
|
|
// a `detail` sub-object.
|
|
//
|
|
// Two-stage write:
|
|
//
|
|
// 1. Serialize the envelope into an in-memory buffer. If serialization
|
|
// fails, return false so the dispatcher handles it via its signal /
|
|
// usage-error branches; nothing is written to w.
|
|
// 2. Best-effort write of the serialized bytes to w. A partial write is
|
|
// accepted (return value still true): the typed exit code has already
|
|
// been determined upstream by handleRootError calling ExitCodeOf(err)
|
|
// before this writer runs, so a torn envelope on stderr must not
|
|
// downgrade the caller's typed exit (3/4/6/10) to plain 1. Consumers
|
|
// parse-or-skip on malformed JSON.
|
|
//
|
|
// Returns true when err was a typed error and serialization succeeded.
|
|
// Returns false only when err carries no Problem (the dispatcher then handles
|
|
// it via its signal / usage-error branches) or when JSON encoding itself failed.
|
|
func WriteTypedErrorEnvelope(w io.Writer, err error, identity string) bool {
|
|
typed, ok := errs.UnwrapTypedError(err)
|
|
if !ok {
|
|
return false
|
|
}
|
|
env := typedEnvelope{
|
|
OK: false,
|
|
Identity: identity,
|
|
Error: typed,
|
|
Notice: GetNotice(),
|
|
}
|
|
var buf bytes.Buffer
|
|
enc := json.NewEncoder(&buf)
|
|
enc.SetEscapeHTML(false)
|
|
enc.SetIndent("", " ")
|
|
if encErr := enc.Encode(env); encErr != nil {
|
|
// Encoding failed — emit nothing here; the dispatcher's fall-through
|
|
// branches still surface the error, so stderr is never blank.
|
|
return false
|
|
}
|
|
// Best-effort write. Partial-write does not downgrade the success status:
|
|
// the dispatcher has already captured ExitCodeOf(err) before calling us,
|
|
// and a torn stderr is preferable to falling through to the plain
|
|
// "Error:" path with exit 1.
|
|
_, _ = w.Write(buf.Bytes())
|
|
return true
|
|
}
|
|
|
|
// typedEnvelope wraps a typed error for wire emission. Error is `error` so the
|
|
// underlying typed error's own json tags determine the inner shape via
|
|
// encoding/json reflection; Notice mirrors the success Envelope's notice (see
|
|
// GetNotice in envelope.go).
|
|
type typedEnvelope struct {
|
|
OK bool `json:"ok"`
|
|
Identity string `json:"identity,omitempty"`
|
|
Error error `json:"error"`
|
|
Notice map[string]interface{} `json:"_notice,omitempty"`
|
|
}
|