openenv/coding_env
21
1# Copyright (c) Meta Platforms, Inc. and affiliates.2# All rights reserved.3#4# This source code is licensed under the BSD-style license found in the5# LICENSE file in the root directory of this source tree.6 7"""Custom exceptions for environment server operations."""8 9from typing import Optional10 11 12class OpenEnvError(Exception):13 """Base exception for all OpenEnv errors."""14 15 pass16 17 18class ConcurrencyConfigurationError(OpenEnvError):19 """20 Raised when an environment is misconfigured for concurrent sessions.21 22 This error is raised during server startup when max_concurrent_envs > 123 is specified for an environment that is not marked as SUPPORTS_CONCURRENT_SESSIONS.24 """25 26 def __init__(27 self,28 environment_name: str,29 max_concurrent_envs: int,30 message: Optional[str] = None,31 ):32 self.environment_name = environment_name33 self.max_concurrent_envs = max_concurrent_envs34 35 if message is None:36 message = (37 f"Environment '{environment_name}' is not marked as SUPPORTS_CONCURRENT_SESSIONS. "38 f"Cannot run with max_concurrent_envs={max_concurrent_envs}. "39 f"Either set max_concurrent_envs=1 or ensure the environment "40 f"properly isolates session state and set SUPPORTS_CONCURRENT_SESSIONS=True."41 )42 43 super().__init__(message)44 45 46class SessionCapacityError(OpenEnvError):47 """48 Raised when the server cannot accept new sessions due to capacity limits.49 50 This error is raised when a new WebSocket connection is attempted but51 the server has already reached max_concurrent_envs active sessions.52 """53 54 def __init__(55 self,56 active_sessions: int,57 max_sessions: int,58 message: Optional[str] = None,59 ):60 self.active_sessions = active_sessions61 self.max_sessions = max_sessions62 63 if message is None:64 message = (65 f"Server at capacity: {active_sessions}/{max_sessions} sessions active. "66 f"Cannot accept new connections."67 )68 69 super().__init__(message)70 71 72class SessionNotFoundError(OpenEnvError):73 """Raised when attempting to access a session that does not exist."""74 75 def __init__(self, session_id: str, message: Optional[str] = None):76 self.session_id = session_id77 78 if message is None:79 message = f"Session '{session_id}' not found."80 81 super().__init__(message)82 83 84class SessionCreationError(OpenEnvError):85 """Raised when a session cannot be created."""86 87 def __init__(self, reason: str, message: Optional[str] = None):88 self.reason = reason89 90 if message is None:91 message = f"Failed to create session: {reason}"92 93 super().__init__(message)94 95 96class EnvironmentFactoryError(OpenEnvError):97 """Raised when the environment factory fails to create an instance."""98 99 def __init__(self, factory_name: str, message: Optional[str] = None):100 self.factory_name = factory_name101 102 if message is None:103 message = f"Environment factory '{factory_name}' failed to create instance."104 105 super().__init__(message)106 