mirror of
https://github.com/larksuite/cli.git
synced 2026-07-05 07:31:22 +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.
160 lines
5.3 KiB
Go
160 lines
5.3 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package errclass
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/larksuite/cli/errs"
|
|
)
|
|
|
|
func TestLookupCodeMeta_CredentialCodes(t *testing.T) {
|
|
cases := []struct {
|
|
code int
|
|
wantCat errs.Category
|
|
wantSubtype errs.Subtype
|
|
wantRetry bool
|
|
}{
|
|
{99991661, errs.CategoryAuthentication, errs.SubtypeTokenMissing, false},
|
|
{99991671, errs.CategoryAuthentication, errs.SubtypeTokenInvalid, false},
|
|
{99991668, errs.CategoryAuthentication, errs.SubtypeTokenInvalid, false},
|
|
{99991663, errs.CategoryAuthentication, errs.SubtypeTokenInvalid, false},
|
|
{99991677, errs.CategoryAuthentication, errs.SubtypeTokenExpired, false},
|
|
{20026, errs.CategoryAuthentication, errs.SubtypeRefreshTokenInvalid, false},
|
|
{20037, errs.CategoryAuthentication, errs.SubtypeRefreshTokenExpired, false},
|
|
{20064, errs.CategoryAuthentication, errs.SubtypeRefreshTokenRevoked, false},
|
|
{20073, errs.CategoryAuthentication, errs.SubtypeRefreshTokenReused, false},
|
|
{20050, errs.CategoryAuthentication, errs.SubtypeRefreshServerError, true},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(fmt.Sprintf("%d", tc.code), func(t *testing.T) {
|
|
meta, ok := LookupCodeMeta(tc.code)
|
|
if !ok {
|
|
t.Fatalf("code %d not registered in codeMeta", tc.code)
|
|
}
|
|
if meta.Category != tc.wantCat || meta.Subtype != tc.wantSubtype || meta.Retryable != tc.wantRetry {
|
|
t.Errorf("code %d: got %+v, want Category=%v Subtype=%v Retryable=%v",
|
|
tc.code, meta, tc.wantCat, tc.wantSubtype, tc.wantRetry)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLookupCodeMeta_MissingScope(t *testing.T) {
|
|
got, ok := LookupCodeMeta(99991679)
|
|
if !ok {
|
|
t.Fatalf("LookupCodeMeta(99991679) ok=false, want true")
|
|
}
|
|
want := CodeMeta{Category: errs.CategoryAuthorization, Subtype: errs.SubtypeMissingScope, Retryable: false}
|
|
if got != want {
|
|
t.Fatalf("LookupCodeMeta(99991679) = %+v, want %+v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestLookupCodeMeta_TaskPermissionDenied_MergedViaInit(t *testing.T) {
|
|
got, ok := LookupCodeMeta(1470403)
|
|
if !ok {
|
|
t.Fatalf("LookupCodeMeta(1470403) ok=false, want true (task sub-table init merge)")
|
|
}
|
|
if got.Category != errs.CategoryAuthorization {
|
|
t.Errorf("Category = %q, want %q", got.Category, errs.CategoryAuthorization)
|
|
}
|
|
if got.Subtype != errs.SubtypePermissionDenied {
|
|
t.Errorf("Subtype = %q, want %q", got.Subtype, errs.SubtypePermissionDenied)
|
|
}
|
|
if got.Retryable {
|
|
t.Errorf("Retryable = true, want false")
|
|
}
|
|
}
|
|
|
|
func TestLookupCodeMeta_RetryableAuthCode(t *testing.T) {
|
|
got, ok := LookupCodeMeta(20050)
|
|
if !ok {
|
|
t.Fatalf("LookupCodeMeta(20050) ok=false, want true")
|
|
}
|
|
if !got.Retryable {
|
|
t.Errorf("LookupCodeMeta(20050).Retryable = false, want true (sole retryable refresh code)")
|
|
}
|
|
if got.Category != errs.CategoryAuthentication {
|
|
t.Errorf("Category = %q, want %q", got.Category, errs.CategoryAuthentication)
|
|
}
|
|
}
|
|
|
|
func TestLookupCodeMeta_RetryableRateLimit(t *testing.T) {
|
|
got, ok := LookupCodeMeta(99991400)
|
|
if !ok {
|
|
t.Fatalf("LookupCodeMeta(99991400) ok=false, want true")
|
|
}
|
|
if !got.Retryable {
|
|
t.Errorf("LookupCodeMeta(99991400).Retryable = false, want true (rate_limit retryable)")
|
|
}
|
|
if got.Subtype != errs.SubtypeRateLimit {
|
|
t.Errorf("Subtype = %q, want %q", got.Subtype, errs.SubtypeRateLimit)
|
|
}
|
|
}
|
|
|
|
func TestLookupCodeMeta_Unknown(t *testing.T) {
|
|
_, ok := LookupCodeMeta(999999)
|
|
if ok {
|
|
t.Fatalf("LookupCodeMeta(999999) ok=true, want false for unknown code")
|
|
}
|
|
}
|
|
|
|
// TestLookupCodeMeta_ConfigCode_99991543 pins the Lark "app_id or app_secret
|
|
// is incorrect" code to CategoryConfig / SubtypeInvalidClient. The CLI cannot
|
|
// retry around a wrong app credential — the operator has to edit the local
|
|
// config — so this MUST stay non-retryable and live in the config category
|
|
// (not the API category it was originally classed under).
|
|
func TestLookupCodeMeta_ConfigCode_99991543(t *testing.T) {
|
|
meta, ok := LookupCodeMeta(99991543)
|
|
if !ok {
|
|
t.Fatal("99991543 not registered in codeMeta")
|
|
}
|
|
if meta.Category != errs.CategoryConfig {
|
|
t.Errorf("category = %v, want %v", meta.Category, errs.CategoryConfig)
|
|
}
|
|
if meta.Subtype != errs.SubtypeInvalidClient {
|
|
t.Errorf("subtype = %v, want %v", meta.Subtype, errs.SubtypeInvalidClient)
|
|
}
|
|
if meta.Retryable {
|
|
t.Errorf("Retryable = true, want false (wrong app credential is operator-fix)")
|
|
}
|
|
}
|
|
|
|
func TestLookupCodeMeta_PolicyChallengeRequired(t *testing.T) {
|
|
got, ok := LookupCodeMeta(21000)
|
|
if !ok {
|
|
t.Fatalf("LookupCodeMeta(21000) ok=false, want true")
|
|
}
|
|
if got.Category != errs.CategoryPolicy {
|
|
t.Errorf("Category = %q, want %q", got.Category, errs.CategoryPolicy)
|
|
}
|
|
if got.Subtype != errs.Subtype("challenge_required") {
|
|
t.Errorf("Subtype = %q, want %q", got.Subtype, "challenge_required")
|
|
}
|
|
}
|
|
|
|
func TestMergeCodeMeta_PanicsOnDuplicate(t *testing.T) {
|
|
defer func() {
|
|
r := recover()
|
|
if r == nil {
|
|
t.Fatalf("mergeCodeMeta with duplicate code did not panic")
|
|
}
|
|
msg, ok := r.(string)
|
|
if !ok {
|
|
t.Fatalf("panic value is not a string: %T (%v)", r, r)
|
|
}
|
|
for _, needle := range []string{"1470403", "permission_denied", "intruder", "test"} {
|
|
if !strings.Contains(msg, needle) {
|
|
t.Errorf("panic message %q missing substring %q", msg, needle)
|
|
}
|
|
}
|
|
}()
|
|
mergeCodeMeta(map[int]CodeMeta{
|
|
1470403: {Category: errs.CategoryAPI, Subtype: errs.Subtype("intruder")},
|
|
}, "test")
|
|
}
|