"""Domain errors converted into stable API responses.""" class GatewayError(Exception): """An expected error that is safe to return to an API consumer.""" def __init__( self, message: str, *, status_code: int = 400, code: str = "gateway_error" ) -> None: super().__init__(message) self.message = message self.status_code = status_code self.code = code class QueueCapacityError(GatewayError): """The bounded inference queue cannot accept another job.""" def __init__(self) -> None: super().__init__( "The inference queue is full. Retry later.", status_code=503, code="queue_full" ) class ModelLoadError(GatewayError): """A model could not be initialized.""" def __init__(self, model_name: str, reason: str) -> None: super().__init__( f"Could not load {model_name}: {reason}", status_code=503, code="model_load_failed", ) class InferenceError(GatewayError): """A model failed while generating an output.""" def __init__(self, model_name: str, reason: str) -> None: super().__init__( f"{model_name} inference failed: {reason}", status_code=500, code="inference_failed", ) class OutOfMemoryError(GatewayError): """The selected execution device ran out of memory.""" def __init__(self, model_name: str) -> None: super().__init__( f"{model_name} ran out of memory", status_code=507, code="out_of_memory", )