ihtesham0345 commited on
Commit
0ca65c8
·
1 Parent(s): efb4fe4

fix: Raise HTTPException for unexpected errors instead of returning invalid dict

Browse files

- safe_analyze now raises HTTPException(500) on unexpected exceptions
- This prevents Pydantic ResponseValidationError from missing fields
- Expected errors (JSON parse fail) still return 200 with defaults + error

Files changed (1) hide show
  1. main.py +6 -1
main.py CHANGED
@@ -1,4 +1,5 @@
1
  from fastapi import FastAPI, HTTPException
 
2
  from models.schemas import (
3
  SEORequest, SEOResponse,
4
  YouTubeResponse, InstagramResponse, LinkedInResponse,
@@ -26,10 +27,14 @@ ERROR_MASK = "An internal error occurred. Please try again later."
26
  def safe_analyze(analyze_fn, content: str):
27
  try:
28
  result = analyze_fn(content)
 
 
29
  return result
 
 
30
  except Exception as e:
31
  print(f"CRITICAL SERVER ERROR: {e}\n{traceback.format_exc()}")
32
- return {"error": ERROR_MASK}
33
 
34
  # ---------------------------------------------------------------------------
35
  # Root
 
1
  from fastapi import FastAPI, HTTPException
2
+ from fastapi.responses import JSONResponse
3
  from models.schemas import (
4
  SEORequest, SEOResponse,
5
  YouTubeResponse, InstagramResponse, LinkedInResponse,
 
27
  def safe_analyze(analyze_fn, content: str):
28
  try:
29
  result = analyze_fn(content)
30
+ if isinstance(result, dict) and result.get("error"):
31
+ print(f"[Warning] Analysis completed with error: {result['error']}")
32
  return result
33
+ except HTTPException:
34
+ raise
35
  except Exception as e:
36
  print(f"CRITICAL SERVER ERROR: {e}\n{traceback.format_exc()}")
37
+ raise HTTPException(status_code=500, detail=ERROR_MASK)
38
 
39
  # ---------------------------------------------------------------------------
40
  # Root