Skip to content

Commit 6e6abae

Browse files
committed
Added error handling for subprocess.run calls by logging errors and raising AudioGenerationError.
Ensured cleanup of temporary files even if an error occurs.
1 parent 5a4b5e1 commit 6e6abae

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

agentic_security/misc/banner.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
from pyfiglet import Figlet, FontNotFound
32
from termcolor import colored
43

agentic_security/probe_data/audio_generator.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import base64
2+
import logging
23
import os
34
import platform
45
import subprocess
@@ -7,6 +8,16 @@
78
import httpx
89
from cache_to_disk import cache_to_disk
910

11+
# Configure logging
12+
logging.basicConfig(level=logging.INFO)
13+
logger = logging.getLogger(__name__)
14+
15+
16+
class AudioGenerationError(Exception):
17+
"""Custom exception for errors during audio generation."""
18+
19+
pass
20+
1021

1122
def encode(content: bytes) -> str:
1223
encoded_content = base64.b64encode(content).decode("utf-8")
@@ -41,12 +52,25 @@ def generate_audio_mac_wav(prompt: str) -> bytes:
4152
# Read the WAV file into memory
4253
with open(temp_wav_path, "rb") as f:
4354
audio_bytes = f.read()
55+
56+
except subprocess.CalledProcessError as e:
57+
logger.error(f"Subprocess error: {e}")
58+
raise AudioGenerationError("Failed to generate or convert audio.") from e
59+
except FileNotFoundError as e:
60+
logger.error(f"File not found: {e}")
61+
raise AudioGenerationError("Required file not found.") from e
62+
except Exception as e:
63+
logger.exception("Unexpected error occurred.")
64+
raise AudioGenerationError(
65+
"An unexpected error occurred during audio generation."
66+
) from e
4467
finally:
45-
# Clean up the temporary files
46-
if os.path.exists(temp_aiff_path):
47-
os.remove(temp_aiff_path)
48-
if os.path.exists(temp_wav_path):
49-
os.remove(temp_wav_path)
68+
for path in (temp_aiff_path, temp_wav_path):
69+
try:
70+
if os.path.exists(path):
71+
os.remove(path)
72+
except Exception as e:
73+
logger.warning(f"Failed to delete temporary file {path}: {e}")
5074

5175
# Return the audio bytes
5276
return audio_bytes

0 commit comments

Comments
 (0)