// Package errors provides domain-specific error types for the KnowFoolery application. package errors import ( "errors" "fmt" ) // DomainError represents a domain-level error with an error code. type DomainError struct { Code ErrorCode Message string Err error } // Error implements the error interface. func (e *DomainError) Error() string { if e.Err != nil { return fmt.Sprintf("[%s] %s: %v", e.Code, e.Message, e.Err) } return fmt.Sprintf("[%s] %s", e.Code, e.Message) } // Unwrap returns the underlying error. func (e *DomainError) Unwrap() error { return e.Err } // Is checks if the target error matches this DomainError's code. func (e *DomainError) Is(target error) bool { var domainErr *DomainError if errors.As(target, &domainErr) { return e.Code == domainErr.Code } return false } // New creates a new DomainError with the given code and message. func New(code ErrorCode, message string) *DomainError { return &DomainError{ Code: code, Message: message, } } // Wrap wraps an existing error with a DomainError. func Wrap(code ErrorCode, message string, err error) *DomainError { return &DomainError{ Code: code, Message: message, Err: err, } } // Common domain errors var ( // ErrNotFound indicates a requested resource was not found. ErrNotFound = New(CodeNotFound, "resource not found") // ErrInvalidInput indicates invalid input was provided. ErrInvalidInput = New(CodeInvalidInput, "invalid input") // ErrUnauthorized indicates the user is not authenticated. ErrUnauthorized = New(CodeUnauthorized, "unauthorized") // ErrForbidden indicates the user lacks permission. ErrForbidden = New(CodeForbidden, "forbidden") // ErrConflict indicates a resource conflict. ErrConflict = New(CodeConflict, "resource conflict") // ErrInternal indicates an internal server error. ErrInternal = New(CodeInternal, "internal error") // ErrSessionExpired indicates a game session has expired. ErrSessionExpired = New(CodeSessionExpired, "session expired") // ErrGameInProgress indicates a game is already in progress. ErrGameInProgress = New(CodeGameInProgress, "game already in progress") // ErrMaxAttemptsReached indicates the maximum attempts have been reached. ErrMaxAttemptsReached = New(CodeMaxAttemptsReached, "maximum attempts reached") )