added warnings when asset files cannot be loaded

This commit is contained in:
Jakob Pinterits
2024-10-09 18:42:34 +02:00
parent 53192b5d0c
commit 5ee8bdb987
2 changed files with 16 additions and 1 deletions

View File

@@ -9,6 +9,7 @@ import json
import logging
import secrets
import typing as t
import warnings
import weakref
from datetime import timedelta
from pathlib import Path
@@ -622,6 +623,17 @@ Sitemap: {base_url / "/rio/sitemap"}
image.save(output_buffer, format="png")
except Exception as err:
if isinstance(self.app._icon, assets.PathAsset):
warnings.warn(
f"Could not fetch the app's icon from {self.app._icon.path.resolve()}"
)
elif isinstance(self.app._icon, assets.UrlAsset):
warnings.warn(
f"Could not fetch the app's icon from {self.app._icon.url}"
)
else:
warnings.warn(f"Could not fetch the app's icon from")
raise fastapi.HTTPException(
status_code=fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Could not fetch the app's icon.",

View File

@@ -7,6 +7,7 @@ https://github.com/tiangolo/fastapi/issues/1240#issuecomment-1055396884
import mimetypes
import typing as t
import warnings
from pathlib import Path
import fastapi
@@ -71,13 +72,15 @@ def range_requests_response(
Returns a fastapi response which serves the given file, supporting Range
Requests as per RFC7233 ("HTTP byte serving").
Returns a 404 if the file does not exist.
Returns a 404 if the file does not exist. In this case a warning is also
shown in the console.
"""
# Get the file size. This also verifies the file exists.
try:
file_size_in_bytes = file_path.stat().st_size
except FileNotFoundError:
warnings.warn(f"Cannot find file at {file_path.resolve()}")
return fastapi.responses.Response(status_code=404)
# Prepare response headers