Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle error when object storage is inactive or down. #49

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/backend/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import uuid
from typing import Literal

import httpx
from dotenv import load_dotenv
from fastapi import HTTPException, UploadFile
from storage3 import AsyncStorageClient, create_client
Expand Down Expand Up @@ -36,8 +37,9 @@ def _validate_file(file: UploadFile) -> Literal[True]:

async def _upload_to_storage(files: list[UploadFile], destination: str) -> list[str]:
for file in files:
assert file.filename is not None
_validate_file(file)
if file.filename is None:
raise HTTPException(status_code=400, detail="No filename provided.")
_validate_file(file)

urls = []
filenames = set()
Expand All @@ -60,6 +62,11 @@ async def _upload_to_storage(files: list[UploadFile], destination: str) -> list[
raise HTTPException(
status_code=error_details["statusCode"], detail=error_details["message"]
)
except httpx.ConnectError:
raise HTTPException(
status_code=503,
detail="Post could not be uploaded as object storage is not reachable.",
)
else:
_, path = res.json()["Key"].split("/", 1)
urls.append(await storage_client.from_(BUCKET_NAME).get_public_url(path))
Expand Down