From e4c5bcf7e3fd0a1dc4af7eb6ab935c954a40e49b Mon Sep 17 00:00:00 2001 From: James Hush Date: Mon, 2 Dec 2024 14:23:15 +0800 Subject: [PATCH] fix: Add better error handling for Cartesia --- src/pipecat/services/cartesia.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/pipecat/services/cartesia.py b/src/pipecat/services/cartesia.py index ad4636a74..0489cf92d 100644 --- a/src/pipecat/services/cartesia.py +++ b/src/pipecat/services/cartesia.py @@ -255,6 +255,7 @@ class CartesiaTTSService(WordTTSService): else: logger.error(f"Cartesia error, unknown message type: {msg}") except asyncio.CancelledError: + # TODO(jamsea): Should we really pass here? pass except Exception as e: logger.error(f"{self} exception: {e}") @@ -377,17 +378,31 @@ class CartesiaHttpTTSService(TTSService): stream=False, _experimental_voice_controls=voice_controls, ) - - await self.stop_ttfb_metrics() - frame = TTSAudioRawFrame( audio=output["audio"], sample_rate=self._settings["output_format"]["sample_rate"], num_channels=1, ) yield frame + except asyncio.TimeoutError as e: + logger.error(f"{self} timeout error: {e}") + await self.push_frame(ErrorFrame("Request timed out")) + except asyncio.CancelledError: + logger.info(f"{self} operation cancelled") + await self.push_frame(ErrorFrame("Operation cancelled")) + raise # Re-raise CancelledError to properly handle task cancellation + except ConnectionError as e: + logger.error(f"{self} connection error: {e}") + await self.push_frame(ErrorFrame(f"Connection error: {e}")) + except KeyError as e: + logger.error(f"{self} invalid settings: Missing {e}") + await self.push_frame(ErrorFrame(f"Invalid settings: Missing {e}")) except Exception as e: - logger.error(f"{self} exception: {e}") + logger.error(f"{self} unexpected error: {e}") + await self.push_frame(ErrorFrame(f"Unexpected error: {e}")) + + finally: + await self.stop_ttfb_metrics() await self.start_tts_usage_metrics(text) yield TTSStoppedFrame()