You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
orquestrador/app/api/routes/dependencies.py

34 lines
1.2 KiB
Python

from sqlalchemy.exc import SQLAlchemyError
from app.db.database import SessionLocal
def get_db():
"""Fornece uma sessao de banco para a request e garante o fechamento."""
db = SessionLocal()
try:
yield db
finally:
db.close()
def db_error_detail(exc: SQLAlchemyError) -> str:
"""Converte erros de banco em mensagens amigaveis para a API."""
text = str(exc).lower()
# Heuristica para identificar falhas no MySQL de tools.
tools_markers = ("tools", "tool")
if any(marker in text for marker in tools_markers):
return "Servico temporariamente indisponivel: banco MySQL (tools) inacessivel."
# Heuristica para identificar falhas no MySQL (base ficticia).
mysql_mock_markers = ("mock", "vehicles", "customers", "orders", "review_schedules")
if any(marker in text for marker in mysql_mock_markers):
return "Servico temporariamente indisponivel: banco MySQL (dados ficticios) inacessivel."
mysql_generic_markers = ("mysql", "pymysql", "(2003", "3306")
if any(marker in text for marker in mysql_generic_markers):
return "Servico temporariamente indisponivel: banco MySQL inacessivel."
return "Servico temporariamente indisponivel: erro de acesso ao banco de dados."