openenv/echo_env
6
1# SPDX-License-Identifier: BSD-3-Clause2 3"""Custom exceptions for environment server operations."""4 5from typing import Optional6 7 8class OpenEnvError(Exception):9 """Base exception for all OpenEnv errors."""10 11 pass12 13 14class ConcurrencyConfigurationError(OpenEnvError):15 """16 Raised when an environment is misconfigured for concurrent sessions.17 18 This error is raised during server startup when max_concurrent_envs > 119 is specified for an environment that is not marked as SUPPORTS_CONCURRENT_SESSIONS.20 """21 22 def __init__(23 self,24 environment_name: str,25 max_concurrent_envs: int,26 message: Optional[str] = None,27 ):28 self.environment_name = environment_name29 self.max_concurrent_envs = max_concurrent_envs30 31 if message is None:32 message = (33 f"Environment '{environment_name}' is not marked as SUPPORTS_CONCURRENT_SESSIONS. "34 f"Cannot run with max_concurrent_envs={max_concurrent_envs}. "35 f"Either set max_concurrent_envs=1 or ensure the environment "36 f"properly isolates session state and set SUPPORTS_CONCURRENT_SESSIONS=True."37 )38 39 super().__init__(message)40 41 42class SessionCapacityError(OpenEnvError):43 """44 Raised when the server cannot accept new sessions due to capacity limits.45 46 This error is raised when a new WebSocket connection is attempted but47 the server has already reached max_concurrent_envs active sessions.48 """49 50 def __init__(51 self,52 active_sessions: int,53 max_sessions: int,54 message: Optional[str] = None,55 ):56 self.active_sessions = active_sessions57 self.max_sessions = max_sessions58 59 if message is None:60 message = (61 f"Server at capacity: {active_sessions}/{max_sessions} sessions active. "62 f"Cannot accept new connections."63 )64 65 super().__init__(message)66 67 68class SessionNotFoundError(OpenEnvError):69 """Raised when attempting to access a session that does not exist."""70 71 def __init__(self, session_id: str, message: Optional[str] = None):72 self.session_id = session_id73 74 if message is None:75 message = f"Session '{session_id}' not found."76 77 super().__init__(message)78 79 80class SessionCreationError(OpenEnvError):81 """Raised when a session cannot be created."""82 83 def __init__(self, reason: str, message: Optional[str] = None):84 self.reason = reason85 86 if message is None:87 message = f"Failed to create session: {reason}"88 89 super().__init__(message)90 91 92class EnvironmentFactoryError(OpenEnvError):93 """Raised when the environment factory fails to create an instance."""94 95 def __init__(self, factory_name: str, message: Optional[str] = None):96 self.factory_name = factory_name97 98 if message is None:99 message = f"Environment factory '{factory_name}' failed to create instance."100 101 super().__init__(message)102 