types

package
v1.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 12, 2025 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package types defines shared message types for WebSocket communication

Index

Constants

View Source
const (
	// Client errors (4xx equivalent)
	ErrorCodeValidation       = "VALIDATION_ERROR"
	ErrorCodeInvalidAction    = "INVALID_ACTION"
	ErrorCodeNotFound         = "NOT_FOUND"
	ErrorCodeUnauthorized     = "UNAUTHORIZED"
	ErrorCodeForbidden        = "FORBIDDEN"
	ErrorCodeRateLimited      = "RATE_LIMITED"
	ErrorCodeDuplicateRequest = "DUPLICATE_REQUEST"

	// Server errors (5xx equivalent)
	ErrorCodeInternal           = "INTERNAL_ERROR"
	ErrorCodeTimeout            = "TIMEOUT"
	ErrorCodeServiceUnavailable = "SERVICE_UNAVAILABLE"
	ErrorCodeStorageError       = "STORAGE_ERROR"
	ErrorCodeProcessingFailed   = "PROCESSING_FAILED"

	// Connection errors
	ErrorCodeConnectionClosed = "CONNECTION_CLOSED"
	ErrorCodeInvalidMessage   = "INVALID_MESSAGE"
	ErrorCodeProtocolError    = "PROTOCOL_ERROR"
)

Standard error codes

Variables

This section is empty.

Functions

func IsClientError

func IsClientError(code string) bool

IsClientError checks if the error code represents a client error

func IsRetryableError

func IsRetryableError(code string) bool

IsRetryableError checks if an error code indicates a retryable condition

func IsServerError

func IsServerError(code string) bool

IsServerError checks if the error code represents a server error

Types

type AcknowledgmentMessage

type AcknowledgmentMessage struct {
	Message
	RequestID string `json:"request_id"`
	Status    string `json:"status"` // "queued", "processing"
	Text      string `json:"message,omitempty"`
}

AcknowledgmentMessage is sent for async requests

func NewAcknowledgmentMessage

func NewAcknowledgmentMessage(requestID, status, message string) *AcknowledgmentMessage

NewAcknowledgmentMessage creates a new acknowledgment message

type ErrorInfo

type ErrorInfo struct {
	Code    string                 `json:"code"`
	Message string                 `json:"message"`
	Details map[string]interface{} `json:"details,omitempty"`
	Retry   *RetryInfo             `json:"retry,omitempty"`
}

ErrorInfo contains structured error information

func NewErrorInfo

func NewErrorInfo(code, message string) *ErrorInfo

NewErrorInfo creates a new error info structure

func (*ErrorInfo) WithDetail

func (e *ErrorInfo) WithDetail(key string, value interface{}) *ErrorInfo

WithDetail adds a detail to the error info

func (*ErrorInfo) WithRetry

func (e *ErrorInfo) WithRetry(retryable bool, after time.Time, maxTries int) *ErrorInfo

WithRetry adds retry information to the error

type ErrorMessage

type ErrorMessage struct {
	Message
	RequestID string     `json:"request_id,omitempty"`
	Error     *ErrorInfo `json:"error"`
}

ErrorMessage represents an error response

func NewErrorMessage

func NewErrorMessage(requestID string, errorInfo *ErrorInfo) *ErrorMessage

NewErrorMessage creates a new error message

type Message

type Message struct {
	Type      MessageType            `json:"type"`
	ID        string                 `json:"id,omitempty"`
	Timestamp int64                  `json:"timestamp"`
	Metadata  map[string]interface{} `json:"metadata,omitempty"`
}

Message is the base structure for all WebSocket messages

func NewMessage

func NewMessage(msgType MessageType) Message

NewMessage creates a base message with timestamp

type MessageType

type MessageType string

MessageType defines the type of WebSocket message

const (
	// Request message types
	MessageTypeRequest MessageType = "request"

	// Response message types
	MessageTypeResponse       MessageType = "response"
	MessageTypeAcknowledgment MessageType = "acknowledgment"
	MessageTypeProgress       MessageType = "progress"
	MessageTypeError          MessageType = "error"

	// Control message types
	MessageTypePing MessageType = "ping"
	MessageTypePong MessageType = "pong"
)

type ProgressMessage

type ProgressMessage struct {
	Message
	RequestID  string                 `json:"request_id"`
	Percentage float64                `json:"percentage"` // 0-100
	Text       string                 `json:"message,omitempty"`
	Details    map[string]interface{} `json:"details,omitempty"`
}

ProgressMessage represents async processing progress

func NewProgressMessage

func NewProgressMessage(requestID string, percentage float64, message string) *ProgressMessage

NewProgressMessage creates a new progress message

type RequestMessage

type RequestMessage struct {
	Message
	Action  string                 `json:"action"`
	Payload map[string]interface{} `json:"payload,omitempty"`
}

RequestMessage represents an incoming WebSocket request

func NewRequestMessage

func NewRequestMessage(action string, payload map[string]interface{}) *RequestMessage

NewRequestMessage creates a new request message

type ResponseMessage

type ResponseMessage struct {
	Message
	RequestID string      `json:"request_id"`
	Success   bool        `json:"success"`
	Data      interface{} `json:"data,omitempty"`
	Error     *ErrorInfo  `json:"error,omitempty"`
}

ResponseMessage represents a synchronous response

func NewResponseMessage

func NewResponseMessage(requestID string, success bool, data interface{}) *ResponseMessage

NewResponseMessage creates a new response message

type RetryInfo

type RetryInfo struct {
	Retryable bool      `json:"retryable"`
	After     time.Time `json:"after,omitempty"`
	MaxTries  int       `json:"max_tries,omitempty"`
}

RetryInfo provides retry guidance for errors

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL