Files
larksuite__cli/shortcuts/event/registry.go
evandance 2b4c6349a1 feat(event): emit typed error envelopes across the event domain (#1289)
Replace every command-facing error path in the event domain — the
consume/schema command layer, the +subscribe shortcut, EventKey
definitions, and the consume orchestration — with typed errs.*
envelopes, so consumers get stable type, subtype, param, hint, and
missing_scopes metadata for classification and recovery instead of
free-form message text.

- Input validation (--jq, --param, --output-dir, --filter, --route,
  unknown EventKey, EventKey params) reports validation /
  invalid_argument with the offending flag in param and an actionable
  hint.
- Scope preflight reports authorization / missing_scope with the
  machine-readable missing_scopes list; console-subscription and
  single-bus preconditions report failed_precondition with recovery
  hints.
- The consume API boundary passes already-typed errors through and
  classifies transport, non-JSON HTTP, and unparsable responses; the
  vc note-detail retry now matches the not-found code on typed errors
  (it silently never fired against the legacy envelope shape).
- Previously-bare failures exited 1 with a plain-text "Error:" line
  and now exit with their category code (validation 2, auth 3,
  network 4, internal 5) alongside the typed stderr envelope.
- forbidigo and errscontract guards now cover the event paths so
  regressions fail lint; AGENTS.md and the lark-event skill document
  the typed contract for agent consumers.

Validation: make unit-test (race) green; event unit and e2e suites
assert category/subtype/param/hint and cause preservation against the
real binary; errscontract and golangci lint clean.
2026-06-09 17:12:55 +08:00

60 lines
1.9 KiB
Go

// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package event
import "github.com/larksuite/cli/errs"
// ProcessorRegistry manages event_type → EventProcessor mappings.
type ProcessorRegistry struct {
processors map[string]EventProcessor
fallback EventProcessor
}
// NewProcessorRegistry creates a registry with a fallback for unregistered event types.
func NewProcessorRegistry(fallback EventProcessor) *ProcessorRegistry {
return &ProcessorRegistry{
processors: make(map[string]EventProcessor),
fallback: fallback,
}
}
// Register adds a processor. Returns an error on duplicate event type registration.
func (r *ProcessorRegistry) Register(p EventProcessor) error {
et := p.EventType()
if _, exists := r.processors[et]; exists {
return errs.NewInternalError(errs.SubtypeUnknown, "duplicate event processor for: %s", et)
}
r.processors[et] = p
return nil
}
// Lookup finds a processor by event type. Returns fallback if not registered. Never returns nil.
func (r *ProcessorRegistry) Lookup(eventType string) EventProcessor {
if p, ok := r.processors[eventType]; ok {
return p
}
return r.fallback
}
// DefaultRegistry builds the standard processor registry.
// To add a new processor, just add r.Register(...) here.
func DefaultRegistry() *ProcessorRegistry {
r := NewProcessorRegistry(&GenericProcessor{})
// im.message
_ = r.Register(&ImMessageProcessor{})
_ = r.Register(&ImMessageReadProcessor{})
_ = r.Register(NewImReactionCreatedProcessor())
_ = r.Register(NewImReactionDeletedProcessor())
// im.chat.member
_ = r.Register(NewImChatBotAddedProcessor())
_ = r.Register(NewImChatBotDeletedProcessor())
_ = r.Register(NewImChatMemberUserAddedProcessor())
_ = r.Register(NewImChatMemberUserWithdrawnProcessor())
_ = r.Register(NewImChatMemberUserDeletedProcessor())
// im.chat
_ = r.Register(&ImChatUpdatedProcessor{})
_ = r.Register(&ImChatDisbandedProcessor{})
return r
}