mirror of
https://github.com/larksuite/cli.git
synced 2026-07-06 00:06:28 +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.
80 lines
2.9 KiB
Go
80 lines
2.9 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package errcompat
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/larksuite/cli/errs"
|
|
internalauth "github.com/larksuite/cli/internal/auth"
|
|
)
|
|
|
|
func TestPromoteAuthError_PromotesNeedAuthorizationError(t *testing.T) {
|
|
needAuth := &internalauth.NeedAuthorizationError{UserOpenId: "u_xxx"}
|
|
got := PromoteAuthError(needAuth)
|
|
|
|
var authErr *errs.AuthenticationError
|
|
if !errors.As(got, &authErr) {
|
|
t.Fatalf("expected *errs.AuthenticationError, got %T", got)
|
|
}
|
|
if authErr.Subtype != errs.SubtypeTokenMissing {
|
|
t.Errorf("subtype = %v, want %v", authErr.Subtype, errs.SubtypeTokenMissing)
|
|
}
|
|
|
|
// Cause chain must preserve original *NeedAuthorizationError so legacy
|
|
// consumers (auth.IsNeedUserAuthorizationError + errors.As pattern in
|
|
// internal/auth/errors.go:42) still match.
|
|
var preserved *internalauth.NeedAuthorizationError
|
|
if !errors.As(got, &preserved) {
|
|
t.Error("Unwrap chain lost *NeedAuthorizationError — breaks auth.IsNeedUserAuthorizationError consumer")
|
|
}
|
|
}
|
|
|
|
func TestPromoteAuthError_PreservesNeedUserAuthorizationMarker(t *testing.T) {
|
|
needAuth := &internalauth.NeedAuthorizationError{UserOpenId: "u_xxx"}
|
|
got := PromoteAuthError(needAuth)
|
|
if !strings.Contains(got.Error(), "need_user_authorization") {
|
|
t.Errorf("Message must contain need_user_authorization marker, got: %q", got.Error())
|
|
}
|
|
}
|
|
|
|
func TestPromoteAuthError_PreservesUserOpenID(t *testing.T) {
|
|
needAuth := &internalauth.NeedAuthorizationError{UserOpenId: "u_test_open_id"}
|
|
got := PromoteAuthError(needAuth)
|
|
|
|
var authErr *errs.AuthenticationError
|
|
if !errors.As(got, &authErr) {
|
|
t.Fatalf("expected *errs.AuthenticationError, got %T", got)
|
|
}
|
|
if authErr.UserOpenID != "u_test_open_id" {
|
|
t.Errorf("UserOpenID = %q, want preserved", authErr.UserOpenID)
|
|
}
|
|
}
|
|
|
|
// TestPromoteAuthError_CarriesAuthLoginHint pins that the recovery action
|
|
// prompt is attached at promotion time — without this Hint, downstream
|
|
// consumers see authentication/token_missing but no "run: lark-cli auth login"
|
|
// guidance, mirroring the pre-typed UX failure when NeedAuthorizationError
|
|
// surfaced as a bare network error. cmd's applyNeedAuthorizationHint relies
|
|
// on this Hint being non-empty so scope enrichment appends instead of
|
|
// overwrites the recovery prompt.
|
|
func TestPromoteAuthError_CarriesAuthLoginHint(t *testing.T) {
|
|
got := PromoteAuthError(&internalauth.NeedAuthorizationError{UserOpenId: "u_xxx"})
|
|
var authErr *errs.AuthenticationError
|
|
if !errors.As(got, &authErr) {
|
|
t.Fatalf("expected *errs.AuthenticationError, got %T", got)
|
|
}
|
|
if !strings.Contains(authErr.Hint, "lark-cli auth login") {
|
|
t.Errorf("Hint must guide user to re-authorize, got: %q", authErr.Hint)
|
|
}
|
|
}
|
|
|
|
func TestPromoteAuthError_Nil_ReturnsNil(t *testing.T) {
|
|
if got := PromoteAuthError(nil); got != nil {
|
|
t.Errorf("nil input should return nil, got %v", got)
|
|
}
|
|
}
|