mirror of
https://github.com/larksuite/cli.git
synced 2026-07-03 22:24:31 +08:00
Every failure on the authentication, authorization, and configuration
path now surfaces as a typed structured error instead of an ad-hoc
envelope. Users and scripts that consume CLI output get:
- a fixed nine-category taxonomy on the wire, each mapped to a
stable shell exit code (authentication/authorization/config = 3,
network = 4, internal = 5, policy = 6, confirmation = 10)
- identity-aware detail fields (missing_scopes, requested_scopes,
granted_scopes, console_url, log_id, retryable, hint) carried
uniformly on the envelope
- a single canonical policy envelope at exit 6; the legacy
auth_error carve-out is retired
- per-subtype canonical message + hint that preserves Lark's
diagnostic phrasing and routes recovery to the right actor:
app developer (app_scope_not_applied), user (missing_scope,
token_scope_insufficient, user_unauthorized), or tenant admin
(app_unavailable, app_disabled)
- wrong app credentials classify as config/invalid_client whether
surfaced by the Open API endpoint (99991543) or the tenant
access-token mint endpoint (10003 / 10014), instead of
collapsing to a transport error or api/unknown
- local shortcut scope preflight emits the same
authorization/missing_scope envelope (identity + deterministic
missing-scope set) used by the post-call permission path, so AI
consumers read the same structured shape from precheck and from
server-returned permission denial
- streaming download/upload failures keep the same network subtype
split (timeout / TLS / DNS / transport) as the non-stream path
instead of collapsing every cause to a generic transport failure
- console_url is carried only on the bot-perspective
app_scope_not_applied envelope (where the recovery action is
"developer applies the scope at the developer console"); the
user-perspective missing_scope envelope drops the field, since
the only actionable user recovery is `lark-cli auth login --scope`
and pointing an end user at a console they cannot modify is
misleading
- bind workflows (Hermes / OpenClaw / lark-channel) flatten dynamic
Type tags to wire 'config' with the original module name kept
as a metric label
All 10 typed errors are cause-bearing, nil-safe on .Error() and
.Unwrap(), and defensively clone slice setter inputs. Four lint
rules (CheckNilSafeError / CheckBuilderImmutable / CheckUnwrapSymmetry
/ CheckBuildAPIErrorArms) lock these invariants on migrated paths.
137 lines
4.8 KiB
Go
137 lines
4.8 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package errclass
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/larksuite/cli/errs"
|
|
)
|
|
|
|
// TestBuildAPIError_CategoryConfirmationFillsRiskAction pins fail-closed
|
|
// behaviour: a code mapped to CategoryConfirmation MUST yield a
|
|
// ConfirmationRequiredError whose Risk + Action are non-empty even when the
|
|
// CodeMeta itself carries no Risk/Action hints. Risk falls back to
|
|
// RiskUnknown; Action falls back to ctx.LarkCmd.
|
|
func TestBuildAPIError_CategoryConfirmationFillsRiskAction(t *testing.T) {
|
|
const stubCode = 99999991
|
|
codeMeta[stubCode] = CodeMeta{
|
|
Category: errs.CategoryConfirmation,
|
|
Subtype: errs.SubtypeConfirmationRequired,
|
|
}
|
|
t.Cleanup(func() { delete(codeMeta, stubCode) })
|
|
|
|
resp := map[string]any{"code": stubCode, "msg": "confirmation required"}
|
|
ctx := ClassifyContext{
|
|
Brand: "feishu",
|
|
AppID: "cli_test",
|
|
Identity: "user",
|
|
LarkCmd: "drive +delete",
|
|
}
|
|
err := BuildAPIError(resp, ctx)
|
|
var confirmErr *errs.ConfirmationRequiredError
|
|
if !errors.As(err, &confirmErr) {
|
|
t.Fatalf("expected *ConfirmationRequiredError, got %T: %v", err, err)
|
|
}
|
|
if confirmErr.Risk == "" {
|
|
t.Error("Risk empty; arm must fail-closed with RiskUnknown")
|
|
}
|
|
if confirmErr.Risk != errs.RiskUnknown {
|
|
t.Errorf("Risk = %q, want %q (CodeMeta carried no Risk hint)",
|
|
confirmErr.Risk, errs.RiskUnknown)
|
|
}
|
|
if confirmErr.Action == "" {
|
|
t.Error("Action empty; arm must fail-closed with command name from ClassifyContext")
|
|
}
|
|
if confirmErr.Action != "drive +delete" {
|
|
t.Errorf("Action = %q, want %q (ctx.LarkCmd fallback)",
|
|
confirmErr.Action, "drive +delete")
|
|
}
|
|
}
|
|
|
|
// TestBuildAPIError_CategoryConfirmationPrefersCodeMetaHints pins that when
|
|
// CodeMeta carries explicit Risk + Action, the dispatcher uses them rather
|
|
// than falling back to RiskUnknown / ctx.LarkCmd.
|
|
func TestBuildAPIError_CategoryConfirmationPrefersCodeMetaHints(t *testing.T) {
|
|
const stubCode = 99999992
|
|
codeMeta[stubCode] = CodeMeta{
|
|
Category: errs.CategoryConfirmation,
|
|
Subtype: errs.SubtypeConfirmationRequired,
|
|
Risk: errs.RiskHighRiskWrite,
|
|
Action: "wiki:delete-space",
|
|
}
|
|
t.Cleanup(func() { delete(codeMeta, stubCode) })
|
|
|
|
resp := map[string]any{"code": stubCode, "msg": "confirmation required"}
|
|
ctx := ClassifyContext{LarkCmd: "drive +delete"}
|
|
err := BuildAPIError(resp, ctx)
|
|
var confirmErr *errs.ConfirmationRequiredError
|
|
if !errors.As(err, &confirmErr) {
|
|
t.Fatalf("expected *ConfirmationRequiredError, got %T: %v", err, err)
|
|
}
|
|
if confirmErr.Risk != errs.RiskHighRiskWrite {
|
|
t.Errorf("Risk = %q, want %q (CodeMeta hint should win)",
|
|
confirmErr.Risk, errs.RiskHighRiskWrite)
|
|
}
|
|
if confirmErr.Action != "wiki:delete-space" {
|
|
t.Errorf("Action = %q, want %q (CodeMeta hint should win)",
|
|
confirmErr.Action, "wiki:delete-space")
|
|
}
|
|
}
|
|
|
|
// TestBuildAPIError_UnknownCategoryRoutesToInternalError pins fail-closed
|
|
// behaviour: an unrecognized Category routes to InternalError instead of
|
|
// emitting an empty Problem on the wire.
|
|
func TestBuildAPIError_UnknownCategoryRoutesToInternalError(t *testing.T) {
|
|
const stubCode = 99999993
|
|
codeMeta[stubCode] = CodeMeta{
|
|
Category: errs.Category("totally_unknown_category"),
|
|
Subtype: errs.SubtypeUnknown,
|
|
}
|
|
t.Cleanup(func() { delete(codeMeta, stubCode) })
|
|
|
|
resp := map[string]any{"code": stubCode, "msg": "weird"}
|
|
err := BuildAPIError(resp, ClassifyContext{})
|
|
var ie *errs.InternalError
|
|
if !errors.As(err, &ie) {
|
|
t.Fatalf("expected *InternalError, got %T: %v", err, err)
|
|
}
|
|
if ie.Category != errs.CategoryInternal {
|
|
t.Errorf("Category = %q, want %q", ie.Category, errs.CategoryInternal)
|
|
}
|
|
if ie.Subtype != errs.SubtypeSDKError {
|
|
t.Errorf("Subtype = %q, want %q", ie.Subtype, errs.SubtypeSDKError)
|
|
}
|
|
if ie.Code != stubCode {
|
|
t.Errorf("Code = %d, want %d (raw Lark code should propagate)", ie.Code, stubCode)
|
|
}
|
|
}
|
|
|
|
// TestBuildAPIError_ConfigInvalidClient_HasHint pins that when a
|
|
// CategoryConfig response (Lark code 10014 — "app secret invalid") flows
|
|
// through BuildAPIError, the resulting *ConfigError MUST carry the canonical
|
|
// recovery hint pointing the user at `lark-cli config init`.
|
|
func TestBuildAPIError_ConfigInvalidClient_HasHint(t *testing.T) {
|
|
const code = 10014
|
|
resp := map[string]any{"code": code, "msg": "app secret invalid"}
|
|
ctx := ClassifyContext{Brand: "feishu", AppID: "cli_test", Identity: "bot"}
|
|
|
|
err := BuildAPIError(resp, ctx)
|
|
var cfgErr *errs.ConfigError
|
|
if !errors.As(err, &cfgErr) {
|
|
t.Fatalf("expected *ConfigError, got %T: %v", err, err)
|
|
}
|
|
if cfgErr.Subtype != errs.SubtypeInvalidClient {
|
|
t.Errorf("Subtype = %q, want %q", cfgErr.Subtype, errs.SubtypeInvalidClient)
|
|
}
|
|
if cfgErr.Hint == "" {
|
|
t.Errorf("Hint is empty; canonical hint required for invalid_client")
|
|
}
|
|
if !strings.Contains(cfgErr.Hint, "lark-cli config init") {
|
|
t.Errorf("Hint should reference `lark-cli config init`; got %q", cfgErr.Hint)
|
|
}
|
|
}
|