Улучшена обработка ошибок и логирование в tmdb-proxy
This commit is contained in:
parent
26a54095bc
commit
69c7a9b3d1
1 changed files with 54 additions and 1 deletions
|
|
@ -5,10 +5,15 @@ TMDB API Proxy Service
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import logging
|
||||||
import httpx
|
import httpx
|
||||||
from fastapi import FastAPI, HTTPException, Query
|
from fastapi import FastAPI, HTTPException, Query
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
|
||||||
|
# Настройка логирования
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
app = FastAPI(title="TMDB API Proxy", version="1.0.0")
|
app = FastAPI(title="TMDB API Proxy", version="1.0.0")
|
||||||
|
|
||||||
# Настройка CORS
|
# Настройка CORS
|
||||||
|
|
@ -35,6 +40,7 @@ async def search_movies(
|
||||||
"""Прокси для поиска фильмов через TMDB API"""
|
"""Прокси для поиска фильмов через TMDB API"""
|
||||||
async with httpx.AsyncClient(timeout=30.0) as client:
|
async with httpx.AsyncClient(timeout=30.0) as client:
|
||||||
try:
|
try:
|
||||||
|
logger.info(f"Searching TMDB for query: {query}")
|
||||||
response = await client.get(
|
response = await client.get(
|
||||||
f"{TMDB_BASE_URL}/search/movie",
|
f"{TMDB_BASE_URL}/search/movie",
|
||||||
params={
|
params={
|
||||||
|
|
@ -46,9 +52,33 @@ async def search_movies(
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
return response.json()
|
data = response.json()
|
||||||
|
logger.info(f"TMDB search successful, found {data.get('total_results', 0)} results")
|
||||||
|
return data
|
||||||
|
except httpx.ConnectError as e:
|
||||||
|
logger.error(f"Connection error to TMDB API: {e}")
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=503,
|
||||||
|
detail=f"Cannot connect to TMDB API. Check network connectivity and DNS. Error: {str(e)}"
|
||||||
|
)
|
||||||
|
except httpx.TimeoutException as e:
|
||||||
|
logger.error(f"Timeout connecting to TMDB API: {e}")
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=504,
|
||||||
|
detail=f"Timeout connecting to TMDB API: {str(e)}"
|
||||||
|
)
|
||||||
|
except httpx.HTTPStatusError as e:
|
||||||
|
logger.error(f"TMDB API returned error status {e.response.status_code}: {e.response.text}")
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=e.response.status_code,
|
||||||
|
detail=f"TMDB API error (status {e.response.status_code}): {e.response.text[:200]}"
|
||||||
|
)
|
||||||
except httpx.HTTPError as e:
|
except httpx.HTTPError as e:
|
||||||
|
logger.error(f"HTTP error: {e}")
|
||||||
raise HTTPException(status_code=500, detail=f"TMDB API error: {str(e)}")
|
raise HTTPException(status_code=500, detail=f"TMDB API error: {str(e)}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Unexpected error: {e}", exc_info=True)
|
||||||
|
raise HTTPException(status_code=500, detail=f"Unexpected error: {str(e)}")
|
||||||
|
|
||||||
|
|
||||||
@app.get("/movie/{movie_id}")
|
@app.get("/movie/{movie_id}")
|
||||||
|
|
@ -60,6 +90,7 @@ async def get_movie_details(
|
||||||
"""Прокси для получения детальной информации о фильме из TMDB"""
|
"""Прокси для получения детальной информации о фильме из TMDB"""
|
||||||
async with httpx.AsyncClient(timeout=30.0) as client:
|
async with httpx.AsyncClient(timeout=30.0) as client:
|
||||||
try:
|
try:
|
||||||
|
logger.info(f"Fetching TMDB movie details for ID: {movie_id}")
|
||||||
params = {
|
params = {
|
||||||
"api_key": TMDB_API_KEY,
|
"api_key": TMDB_API_KEY,
|
||||||
"language": language
|
"language": language
|
||||||
|
|
@ -73,8 +104,30 @@ async def get_movie_details(
|
||||||
)
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
return response.json()
|
return response.json()
|
||||||
|
except httpx.ConnectError as e:
|
||||||
|
logger.error(f"Connection error to TMDB API: {e}")
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=503,
|
||||||
|
detail=f"Cannot connect to TMDB API. Check network connectivity and DNS. Error: {str(e)}"
|
||||||
|
)
|
||||||
|
except httpx.TimeoutException as e:
|
||||||
|
logger.error(f"Timeout connecting to TMDB API: {e}")
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=504,
|
||||||
|
detail=f"Timeout connecting to TMDB API: {str(e)}"
|
||||||
|
)
|
||||||
|
except httpx.HTTPStatusError as e:
|
||||||
|
logger.error(f"TMDB API returned error status {e.response.status_code}: {e.response.text}")
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=e.response.status_code,
|
||||||
|
detail=f"TMDB API error (status {e.response.status_code}): {e.response.text[:200]}"
|
||||||
|
)
|
||||||
except httpx.HTTPError as e:
|
except httpx.HTTPError as e:
|
||||||
|
logger.error(f"HTTP error: {e}")
|
||||||
raise HTTPException(status_code=500, detail=f"TMDB API error: {str(e)}")
|
raise HTTPException(status_code=500, detail=f"TMDB API error: {str(e)}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Unexpected error: {e}", exc_info=True)
|
||||||
|
raise HTTPException(status_code=500, detail=f"Unexpected error: {str(e)}")
|
||||||
|
|
||||||
|
|
||||||
@app.get("/health")
|
@app.get("/health")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue