"""Fail closed without labelling unavailable authority as verified revocation.""" from sqlalchemy.exc import SQLAlchemyError from app.modules.knowledge.service import KnowledgeError def authority_failure_code(exc): if isinstance(exc, KnowledgeError) and exc.code in {'ACTOR_UNAVAILABLE', 'CONTEXT_REVOKED'}: return 'CONTEXT_REVOKED' if isinstance(exc, TimeoutError): return 'SEARCH_TIMEOUT' if isinstance(exc, SQLAlchemyError): return 'DEPENDENCY_UNAVAILABLE' if isinstance(exc, ValueError): if str(exc) == 'SERVICE_BUSY': return 'SEARCH_SATURATED' if str(exc) in {'AUTH_REQUIRED', 'USER_DISABLED', 'ACCOUNT_DISABLED', 'SESSION_REVOKED', 'SESSION_EXPIRED', 'CONTEXT_REVOKED', 'CONVERSATION_STALE', 'CONVERSATION_CLOSED', 'CONVERSATION_NOT_FOUND', 'SESSION_NOT_FOUND', 'LIVE_SESSION_EXPIRED', 'LIVE_SESSION_UNUSABLE'}: return 'CONTEXT_REVOKED' return 'DEPENDENCY_UNAVAILABLE'