Rthur2003 commited on
Commit
e8b1db1
·
1 Parent(s): 91fa7cf

fix: enhance error responses in audio processing endpoint with structured details

Browse files
Files changed (1) hide show
  1. app/routes/data_processing.py +6 -6
app/routes/data_processing.py CHANGED
@@ -26,17 +26,17 @@ async def process_audio_endpoint(
26
  logger.info(f"Received audio processing request for file: {file.filename}")
27
 
28
  if not file.content_type or not file.content_type.startswith("audio/"):
29
- raise HTTPException(status_code=400, detail="Invalid file type. Must be audio.")
30
 
31
  try:
32
  # Read file content with size guard
33
  content = await file.read()
34
  if len(content) > MAX_PAYLOAD_BYTES:
35
- raise HTTPException(status_code=413, detail=f"File too large. Maximum size is {MAX_PAYLOAD_BYTES // (1024*1024)} MB.")
36
-
37
  # Process audio
38
  processed_audio = process_audio(content, options)
39
-
40
  # Return as downloadable file
41
  filename = f"processed_{file.filename}.wav"
42
  return StreamingResponse(
@@ -46,7 +46,7 @@ async def process_audio_endpoint(
46
  )
47
 
48
  except ValueError as e:
49
- raise HTTPException(status_code=400, detail=str(e))
50
  except Exception as e:
51
  logger.error(f"Unexpected error in audio processing: {e}", exc_info=True)
52
- raise HTTPException(status_code=500, detail="Internal server error during audio processing")
 
26
  logger.info(f"Received audio processing request for file: {file.filename}")
27
 
28
  if not file.content_type or not file.content_type.startswith("audio/"):
29
+ raise HTTPException(status_code=400, detail={"code": "invalid_file_type", "message": "Invalid file type. Must be audio."})
30
 
31
  try:
32
  # Read file content with size guard
33
  content = await file.read()
34
  if len(content) > MAX_PAYLOAD_BYTES:
35
+ raise HTTPException(status_code=413, detail={"code": "file_too_large", "message": f"File too large. Maximum size is {MAX_PAYLOAD_BYTES // (1024*1024)} MB."})
36
+
37
  # Process audio
38
  processed_audio = process_audio(content, options)
39
+
40
  # Return as downloadable file
41
  filename = f"processed_{file.filename}.wav"
42
  return StreamingResponse(
 
46
  )
47
 
48
  except ValueError as e:
49
+ raise HTTPException(status_code=400, detail={"code": "validation_error", "message": str(e)})
50
  except Exception as e:
51
  logger.error(f"Unexpected error in audio processing: {e}", exc_info=True)
52
+ raise HTTPException(status_code=500, detail={"code": "internal_error", "message": "Internal server error during audio processing"})