mirror of
https://github.com/larksuite/cli.git
synced 2026-07-03 14:02:43 +08:00
Introduce a typed error contract framework for lark-cli so in-process
Go callers can branch via errors.As(&errs.XxxError{}) and shell scripts,
AI agents, and protocol adapters can branch on stable JSON type/subtype
fields instead of regex-parsing free-form messages.
Adds:
- Canonical taxonomy under errs/ (9 categories + typed Error structs
embedding a shared Problem, RFC 7807-aligned)
- Centralized Lark code metadata + identity-aware BuildAPIError dispatch
- Typed JSON envelope writer alongside the legacy envelope writer
- MCP / OAuth (RFC 6750 Bearer) projection adapters
- Five CI lint guards preventing ad-hoc taxonomy drift
Backward compatibility: legacy *output.ExitError producers (ErrAPI,
ErrWithHint, Errorf, ErrBare) and business shortcuts that use them
continue to render the legacy envelope unchanged. SecurityPolicyError
wire format and exit code are preserved via a carve-out; taxonomy
migration is deferred to PR 2. Domain-specific business migration is
staged across PR 3+.
Framework-direct paths now return typed *errs.*Error: ErrAuth /
ErrValidation / ErrNetwork emit category literals on the wire
(authentication / validation / network), *core.ConfigError is promoted
at the cmd/root boundary with exit code aligned from 2 to 3, and Lark
API permission denials classified by BuildAPIError exit 3.
At the SDK boundary, WrapDoAPIError preserves any already-classified
error (legacy *output.ExitError or typed *errs.*) so output.ErrAuth
from missing credentials surfaces with the auth category and exit 3
intact instead of being downgraded to a network error. Policy responses
classified by BuildAPIError (codes 21000 / 21001) extract challenge_url
and the canonical hint from the response body, matching what the
auth transport already surfaces at the HTTP layer; non-https
challenge URLs are dropped.
First PR in the feat/error-contract-* series.
114 lines
3.3 KiB
Go
114 lines
3.3 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package output
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestClassifyLarkError_DriveCreateShortcutConstraints verifies known Drive shortcut errors map to actionable hints.
|
|
func TestClassifyLarkError_DriveCreateShortcutConstraints(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
code int
|
|
wantExitCode int
|
|
wantType string
|
|
wantHint string
|
|
}{
|
|
{
|
|
name: "resource contention",
|
|
code: LarkErrDriveResourceContention,
|
|
wantExitCode: ExitAPI,
|
|
wantType: "conflict",
|
|
wantHint: "avoid concurrent duplicate requests",
|
|
},
|
|
{
|
|
name: "cross tenant unit",
|
|
code: LarkErrDriveCrossTenantUnit,
|
|
wantExitCode: ExitAPI,
|
|
wantType: "cross_tenant",
|
|
wantHint: "same tenant and region/unit",
|
|
},
|
|
{
|
|
name: "cross brand",
|
|
code: LarkErrDriveCrossBrand,
|
|
wantExitCode: ExitAPI,
|
|
wantType: "cross_brand",
|
|
wantHint: "same brand environment",
|
|
},
|
|
{
|
|
name: "sheets float image invalid dims",
|
|
code: LarkErrSheetsFloatImageInvalidDims,
|
|
wantExitCode: ExitAPI,
|
|
wantType: "invalid_parameters",
|
|
wantHint: "--width / --height / --offset-x / --offset-y",
|
|
},
|
|
{
|
|
name: "drive permission apply rate limit",
|
|
code: LarkErrDrivePermApplyRateLimit,
|
|
wantExitCode: ExitAPI,
|
|
wantType: "rate_limit",
|
|
wantHint: "5 times per day",
|
|
},
|
|
{
|
|
name: "drive permission apply not applicable",
|
|
code: LarkErrDrivePermApplyNotApplicable,
|
|
wantExitCode: ExitAPI,
|
|
wantType: "invalid_parameters",
|
|
wantHint: "does not accept a permission-apply request",
|
|
},
|
|
{
|
|
name: "ownership mismatch",
|
|
code: LarkErrOwnershipMismatch,
|
|
wantExitCode: ExitAPI,
|
|
wantType: "ownership_mismatch",
|
|
wantHint: "messages-resources-download",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
gotExitCode, gotType, gotHint := ClassifyLarkError(tt.code, "raw msg")
|
|
if gotExitCode != tt.wantExitCode {
|
|
t.Fatalf("exitCode=%d, want %d", gotExitCode, tt.wantExitCode)
|
|
}
|
|
if gotType != tt.wantType {
|
|
t.Fatalf("type=%q, want %q", gotType, tt.wantType)
|
|
}
|
|
if gotHint == "" {
|
|
t.Fatal("expected non-empty hint")
|
|
}
|
|
if !strings.Contains(gotHint, tt.wantHint) {
|
|
t.Fatalf("hint=%q, want substring %q", gotHint, tt.wantHint)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestClassifyLarkError_WikiLockContention verifies the wiki write-lock
|
|
// contention error (131009) maps to an actionable retry hint instead of
|
|
// a generic "api_error". Surfaces during concurrent wiki +node-create
|
|
// against the same parent (see larksuite/cli#1012).
|
|
func TestClassifyLarkError_WikiLockContention(t *testing.T) {
|
|
t.Parallel()
|
|
gotExitCode, gotType, gotHint := ClassifyLarkError(LarkErrWikiLockContention, "raw msg")
|
|
if gotExitCode != ExitAPI {
|
|
t.Fatalf("exitCode=%d, want %d", gotExitCode, ExitAPI)
|
|
}
|
|
if gotType != "conflict" {
|
|
t.Fatalf("type=%q, want %q", gotType, "conflict")
|
|
}
|
|
if !strings.Contains(gotHint, "wiki write lock") {
|
|
t.Fatalf("hint=%q, want substring %q", gotHint, "wiki write lock")
|
|
}
|
|
if !strings.Contains(gotHint, "backoff") {
|
|
t.Fatalf("hint=%q, want substring %q", gotHint, "backoff")
|
|
}
|
|
}
|