Documentation
¶
Overview ¶
Package errors provides a structured error taxonomy for swarm coordination in agentkit. It defines a comprehensive set of error types, codes, and categories that enable consistent error handling across distributed agent systems.
Error Categories ¶
Errors are classified into four categories:
- Transient: Temporary failures where retry may succeed (network issues, etc.)
- Permanent: Failures where retry will not help (invalid input, not found, etc.)
- Resource: Resource exhaustion issues (rate limits, quotas, etc.)
- Internal: Unexpected errors indicating bugs or system failures
Error Codes ¶
Each error has a specific code that identifies the type of failure:
- TIMEOUT: Operation timed out
- RATE_LIMITED: Rate limit exceeded
- NOT_FOUND: Resource not found
- CONFLICT: Conflicting operation
- UNAUTHORIZED: Authentication/authorization failure
- And more...
Usage ¶
Create a new error:
err := errors.New(errors.ErrCodeTimeout, "operation timed out")
Wrap an existing error with context:
wrapped := errors.Wrap(err, "fetching agent state")
Check if an error is retryable:
if agentErr, ok := errors.AsAgentError(err); ok && agentErr.Retryable() {
// retry logic
}
JSON Serialization ¶
All errors support JSON serialization for cross-agent communication:
data, err := json.Marshal(agentErr)
Errors can be deserialized back:
var agentErr errors.Error json.Unmarshal(data, &agentErr)
Index ¶
- func AllRetryable(errs []error) bool
- func AnyRetryable(errs []error) bool
- func Cause(err error) error
- func Collect(errs ...error) []error
- func FirstRetryable(errs []error) error
- func GetMetadata(err error) map[string]string
- func Is(err error, code ErrorCode) bool
- func IsCategory(err error, category ErrorCategory) bool
- func IsInternal(err error) bool
- func IsPermanent(err error) bool
- func IsResource(err error) bool
- func IsRetryable(err error) bool
- func IsTransient(err error) bool
- func Join(errs ...error) error
- type AgentError
- type Error
- func AgentOffline(agentID string, opts ...Option) *Error
- func Conflict(message string, opts ...Option) *Error
- func CoordinationFailure(message string, opts ...Option) *Error
- func Forbidden(message string, opts ...Option) *Error
- func FromCode(code ErrorCode, opts ...Option) *Error
- func Internal(message string, opts ...Option) *Error
- func InvalidInput(message string, opts ...Option) *Error
- func New(code ErrorCode, message string, opts ...Option) *Error
- func Newf(code ErrorCode, format string, args ...interface{}) *Error
- func NotFound(message string, opts ...Option) *Error
- func RateLimited(message string, opts ...Option) *Error
- func RecoverPanic(recovered interface{}) *Error
- func TaskFailed(taskID, reason string, opts ...Option) *Error
- func Timeout(message string, opts ...Option) *Error
- func Unauthorized(message string, opts ...Option) *Error
- func Wrap(err error, message string, opts ...Option) *Error
- func WrapWithCode(err error, code ErrorCode, message string, opts ...Option) *Error
- func Wrapf(err error, format string, args ...interface{}) *Error
- func (e *Error) AgentID() string
- func (e *Error) Category() ErrorCategory
- func (e *Error) Code() ErrorCode
- func (e *Error) Error() string
- func (e *Error) MarshalJSON() ([]byte, error)
- func (e *Error) Metadata() map[string]string
- func (e *Error) Retryable() bool
- func (e *Error) TaskID() string
- func (e *Error) Timestamp() time.Time
- func (e *Error) UnmarshalJSON(data []byte) error
- func (e *Error) Unwrap() error
- type ErrorCategory
- type ErrorCode
- type Option
- func WithAgentID(id string) Option
- func WithCategory(cat ErrorCategory) Option
- func WithCause(cause error) Option
- func WithMetadata(key, value string) Option
- func WithMetadataMap(m map[string]string) Option
- func WithRetryable(retryable bool) Option
- func WithTaskID(id string) Option
- func WithTimestamp(t time.Time) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllRetryable ¶
AllRetryable checks if all errors in the slice are retryable. Returns true for empty slice.
func AnyRetryable ¶
AnyRetryable checks if any error in the slice is retryable. Returns false for empty slice.
func FirstRetryable ¶
FirstRetryable returns the first retryable error from a slice. Returns nil if no retryable error is found.
func GetMetadata ¶
GetMetadata extracts metadata from an error. Returns nil if err is not an AgentError.
func IsCategory ¶
func IsCategory(err error, category ErrorCategory) bool
IsCategory checks if any error in the chain has the given category.
func IsInternal ¶
IsInternal checks if the error is an internal error.
func IsResource ¶
IsResource checks if the error is resource-related.
Types ¶
type AgentError ¶
type AgentError interface {
error
// Code returns the specific error code identifying the failure type.
Code() ErrorCode
// Category returns the error category for retry/handling decisions.
Category() ErrorCategory
// Retryable returns true if the operation may succeed on retry.
Retryable() bool
// Metadata returns additional context as key-value pairs.
Metadata() map[string]string
// Unwrap returns the underlying error, if any.
Unwrap() error
}
AgentError is the interface for all structured errors in agentkit. It extends the standard error interface with additional context for swarm coordination and retry logic.
func AsAgentError ¶
func AsAgentError(err error) AgentError
As attempts to extract an AgentError from an error chain. Returns nil if no AgentError is found.
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error is the concrete implementation of AgentError.
func AgentOffline ¶
AgentOffline creates an agent offline error.
func CoordinationFailure ¶
CoordinationFailure creates a coordination failure error.
func InvalidInput ¶
InvalidInput creates an invalid input error.
func RateLimited ¶
RateLimited creates a rate limit error.
func RecoverPanic ¶
func RecoverPanic(recovered interface{}) *Error
RecoverPanic converts a recovered panic value into an Error.
func TaskFailed ¶
TaskFailed creates a task failed error.
func Unauthorized ¶
Unauthorized creates an unauthorized error.
func Wrap ¶
Wrap wraps an error with additional context while preserving the error chain. If err is nil, Wrap returns nil. If err is already an AgentError, it wraps it with the new message. Otherwise, it creates a new Internal error wrapping the original.
func WrapWithCode ¶
WrapWithCode wraps an error with a specific error code.
func (*Error) Category ¶
func (e *Error) Category() ErrorCategory
Category returns the error category.
func (*Error) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (*Error) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
type ErrorCategory ¶
type ErrorCategory string
ErrorCategory classifies errors by their nature and retry semantics.
const ( // CategoryTransient indicates temporary failures where retry may succeed. // Examples: network timeouts, temporary service unavailability. CategoryTransient ErrorCategory = "transient" // CategoryPermanent indicates failures where retry will not help. // Examples: invalid input, resource not found, permission denied. CategoryPermanent ErrorCategory = "permanent" // CategoryResource indicates resource exhaustion or quota issues. // Examples: rate limiting, storage quota exceeded, connection pool exhausted. CategoryResource ErrorCategory = "resource" // CategoryInternal indicates unexpected errors, bugs, or system failures. // Examples: nil pointer, assertion failures, corrupted state. CategoryInternal ErrorCategory = "internal" )
Error categories define how errors should be handled.
func Category ¶
func Category(err error) ErrorCategory
Category extracts the error category from an error, if available. Returns empty string if err is not an AgentError.
func (ErrorCategory) IsRetryable ¶
func (c ErrorCategory) IsRetryable() bool
IsRetryable returns true if errors in this category may succeed on retry.
func (ErrorCategory) String ¶
func (c ErrorCategory) String() string
String returns the string representation of the category.
type ErrorCode ¶
type ErrorCode string
ErrorCode identifies specific error types within categories.
const ( // Transient errors ErrCodeTimeout ErrorCode = "TIMEOUT" // Operation timed out ErrCodeNetworkErr ErrorCode = "NETWORK_ERR" // Network connectivity issue ErrCodeRetryLater ErrorCode = "RETRY_LATER" // Server requested retry // Permanent errors ErrCodeNotFound ErrorCode = "NOT_FOUND" // Resource does not exist ErrCodeConflict ErrorCode = "CONFLICT" // Conflicting operation or state ErrCodeInvalidInput ErrorCode = "INVALID_INPUT" // Malformed or invalid input ErrCodeForbidden ErrorCode = "FORBIDDEN" // Authorization denied ErrCodeAlreadyExists ErrorCode = "ALREADY_EXISTS" // Resource already exists ErrCodePrecondition ErrorCode = "PRECONDITION" // Precondition not met ErrCodeUnsupported ErrorCode = "UNSUPPORTED" // Operation not supported ErrCodeCanceled ErrorCode = "CANCELED" // Operation was canceled // Resource errors ErrCodeRateLimit ErrorCode = "RATE_LIMITED" // Rate limit exceeded ErrCodeQuotaExceeded ErrorCode = "QUOTA_EXCEEDED" // Resource quota exhausted ErrCodeResourceBusy ErrorCode = "RESOURCE_BUSY" // Resource is busy/locked ErrCodeCapacity ErrorCode = "CAPACITY" // System at capacity // Internal errors ErrCodeInternal ErrorCode = "INTERNAL" // Unexpected internal error ErrCodeCorruption ErrorCode = "CORRUPTION" // Data corruption detected ErrCodeAssertion ErrorCode = "ASSERTION" // Assertion/invariant violation ErrCodePanic ErrorCode = "PANIC" // Recovered from panic // Agent-specific errors ErrCodeAgentOffline ErrorCode = "AGENT_OFFLINE" // Target agent is offline ErrCodeAgentBusy ErrorCode = "AGENT_BUSY" // Agent is processing another task ErrCodeTaskFailed ErrorCode = "TASK_FAILED" // Task execution failed ErrCodeCoordination ErrorCode = "COORDINATION" // Swarm coordination failure ErrCodeHandoffFailed ErrorCode = "HANDOFF_FAILED" // Agent handoff failed ErrCodeCapabilityMissing ErrorCode = "CAPABILITY_MISSING" // Required capability not available )
Error codes for common failure scenarios.
func Code ¶
Code extracts the error code from an error, if available. Returns empty string if err is not an AgentError.
func (ErrorCode) DefaultCategory ¶
func (c ErrorCode) DefaultCategory() ErrorCategory
DefaultCategory returns the default category for an error code.
func (ErrorCode) DefaultRetryable ¶
DefaultRetryable returns whether this error code is typically retryable.
func (ErrorCode) Description ¶
Description returns a human-readable description for the error code.
type Option ¶
type Option func(*Error)
Option is a functional option for configuring an Error.
func WithCategory ¶
func WithCategory(cat ErrorCategory) Option
WithCategory overrides the default category.
func WithMetadata ¶
WithMetadata adds metadata key-value pairs.
func WithMetadataMap ¶
WithMetadataMap adds multiple metadata key-value pairs.
func WithRetryable ¶
WithRetryable explicitly sets whether the error is retryable.