feat: improve error handling in HTTP requests and database fixture loading

This commit is contained in:
gabdevele
2026-06-18 01:06:59 +02:00 Unverified
parent 07547ce2c3
commit 929f829d37
3 changed files with 54 additions and 13 deletions
+28 -9
View File
@@ -16,16 +16,35 @@ if [ ! -f "$DB_PATH" ] && [ -f "$LEGACY_DB_PATH" ]; then
cp "$LEGACY_DB_PATH" "$DB_PATH"
fi
if [ ! -f "$DB_PATH" ] && [ "${LOAD_FIXTURE_ON_EMPTY_DB:-true}" = "true" ]; then
python manage.py migrate
if [ -f "$DUMP_SRC" ]; then
TMP_DUMP="$(mktemp)"
python /app/scripts/rewrite_dumps.py "$DUMP_SRC" "$TMP_DUMP"
python manage.py loaddata "$TMP_DUMP"
rm -f "$TMP_DUMP"
python manage.py migrate
should_load_fixture=0
if [ "${LOAD_FIXTURE_ON_EMPTY_DB:-true}" = "true" ] && [ -f "$DUMP_SRC" ]; then
if [ ! -s "$DB_PATH" ]; then
should_load_fixture=1
else
entity_conf_count="$(python manage.py shell -c '
from django.db import connection
table = "spid_cie_oidc_entity_federationentityconfiguration"
with connection.cursor() as cursor:
tables = connection.introspection.table_names(cursor)
if table not in tables:
print(0)
else:
cursor.execute(f"SELECT COUNT(*) FROM {table}")
print(cursor.fetchone()[0])
')"
if [ "${entity_conf_count:-0}" = "0" ]; then
should_load_fixture=1
fi
fi
else
python manage.py migrate
fi
if [ "$should_load_fixture" = "1" ]; then
TMP_DUMP="$(mktemp)"
python /app/scripts/rewrite_dumps.py "$DUMP_SRC" "$TMP_DUMP"
python manage.py loaddata "$TMP_DUMP"
rm -f "$TMP_DUMP"
fi
python /app/scripts/normalize_db_urls.py "$DB_PATH"
@@ -2,12 +2,26 @@ import aiohttp
import asyncio
def _body_preview(body: str, limit: int = 256) -> str:
if not body:
return ""
preview = body[:limit].strip()
if len(body) > limit:
return f"{preview}..."
return preview
async def fetch(session, url, httpc_params: dict = {}):
async with session.get(url, **httpc_params.get("connection", {})) as response:
body = await response.text()
if response.status != 200: # pragma: no cover
# response.raise_for_status()
return ""
return await response.text()
raise RuntimeError(
"HTTP GET failed for "
f"{url}: status={response.status} "
f"content_type={response.headers.get('Content-Type', '')} "
f"body_preview={_body_preview(body)}"
)
return body
async def fetch_all(session, urls, httpc_params):
@@ -39,7 +39,15 @@ def get_http_url(urls: list, httpc_params: dict = {}) -> list:
responses = []
for i in urls:
res = requests.get(i, **httpc_params) # nosec - B113
responses.append(res.content.decode())
body = res.content.decode()
if res.status_code != 200:
raise RuntimeError(
"HTTP GET failed for "
f"{i}: status={res.status_code} "
f"content_type={res.headers.get('Content-Type', '')} "
f"body_preview={body[:256].strip()}"
)
responses.append(body)
else:
responses = asyncio.run(http_get(urls, httpc_params)) # pragma: no cover
return responses