A community based topic aggregation platform built on atproto
at main 84 lines 2.8 kB view raw
1package aggregators 2 3import ( 4 "errors" 5 "fmt" 6) 7 8// Domain errors 9var ( 10 ErrAggregatorNotFound = errors.New("aggregator not found") 11 ErrAuthorizationNotFound = errors.New("authorization not found") 12 ErrNotAuthorized = errors.New("aggregator not authorized for this community") 13 ErrAlreadyAuthorized = errors.New("aggregator already authorized for this community") 14 ErrRateLimitExceeded = errors.New("aggregator rate limit exceeded") 15 ErrInvalidConfig = errors.New("invalid aggregator configuration") 16 ErrConfigSchemaValidation = errors.New("configuration does not match aggregator's schema") 17 ErrNotModerator = errors.New("user is not a moderator of this community") 18 ErrNotImplemented = errors.New("feature not yet implemented") // For Phase 2 write-forward operations 19 20 // API Key authentication errors 21 ErrAPIKeyRevoked = errors.New("API key has been revoked") 22 ErrAPIKeyInvalid = errors.New("invalid API key") 23 ErrAPIKeyNotFound = errors.New("API key not found for this aggregator") 24 ErrOAuthTokenExpired = errors.New("OAuth token has expired and needs refresh") 25 ErrOAuthRefreshFailed = errors.New("failed to refresh OAuth token") 26 ErrOAuthSessionMismatch = errors.New("OAuth session DID does not match aggregator DID") 27) 28 29// ValidationError represents a validation error with field details 30type ValidationError struct { 31 Field string 32 Message string 33} 34 35func (e *ValidationError) Error() string { 36 return fmt.Sprintf("validation error: %s - %s", e.Field, e.Message) 37} 38 39// NewValidationError creates a new validation error 40func NewValidationError(field, message string) error { 41 return &ValidationError{ 42 Field: field, 43 Message: message, 44 } 45} 46 47// Error classification helpers for handlers to map to HTTP status codes 48func IsNotFound(err error) bool { 49 return errors.Is(err, ErrAggregatorNotFound) || 50 errors.Is(err, ErrAuthorizationNotFound) || 51 errors.Is(err, ErrAPIKeyNotFound) 52} 53 54func IsValidationError(err error) bool { 55 var validationErr *ValidationError 56 return errors.As(err, &validationErr) || errors.Is(err, ErrInvalidConfig) || errors.Is(err, ErrConfigSchemaValidation) 57} 58 59func IsUnauthorized(err error) bool { 60 return errors.Is(err, ErrNotAuthorized) || errors.Is(err, ErrNotModerator) 61} 62 63func IsConflict(err error) bool { 64 return errors.Is(err, ErrAlreadyAuthorized) 65} 66 67func IsRateLimited(err error) bool { 68 return errors.Is(err, ErrRateLimitExceeded) 69} 70 71func IsNotImplemented(err error) bool { 72 return errors.Is(err, ErrNotImplemented) 73} 74 75func IsAPIKeyError(err error) bool { 76 return errors.Is(err, ErrAPIKeyRevoked) || 77 errors.Is(err, ErrAPIKeyInvalid) || 78 errors.Is(err, ErrAPIKeyNotFound) 79} 80 81func IsOAuthError(err error) bool { 82 return errors.Is(err, ErrOAuthTokenExpired) || 83 errors.Is(err, ErrOAuthRefreshFailed) 84}