Fix error handling in StoreService

This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-11-21 22:44:45 -03:00
commit dda7096984
2 changed files with 15 additions and 9 deletions

View file

@ -23,17 +23,17 @@ class TagsIdResponse(BaseModel):
class ListComponentResponse(BaseModel):
id: UUID
name: Optional[str]
description: Optional[str]
liked_by_count: Optional[int]
id: Optional[UUID] = None
name: Optional[str] = None
description: Optional[str] = None
liked_by_count: Optional[int] = None
liked_by_user: Optional[bool] = None
is_component: Optional[bool]
metadata: Optional[dict]
user_created: Optional[dict]
is_component: Optional[bool] = None
metadata: Optional[dict] = {}
user_created: Optional[dict] = {}
tags: Optional[List[TagResponse]] = None
downloads_count: Optional[int]
last_tested_version: Optional[str]
downloads_count: Optional[int] = None
last_tested_version: Optional[str] = None
private: Optional[bool] = None
# tags comes as a TagsIdResponse but we want to return a list of TagResponse

View file

@ -343,6 +343,10 @@ class StoreService(Service):
try:
errors = response.json()
message = errors["errors"][0]["message"]
if message == "An unexpected error occurred.":
# This is a bug in Directus that returns this error
# when an error was thrown in the flow
message = "You already have a component with this name. Please choose a different name."
raise FilterError(message)
except UnboundLocalError:
pass
@ -458,6 +462,8 @@ class StoreService(Service):
raise ForbiddenError("You are not authorized to access this public resource")
elif exc.response.status_code == 401:
raise APIKeyError("You are not authorized to access this resource. Please check your API key.")
except Exception as exc:
raise ValueError(f"Unexpected error: {exc}")
try:
if result and not metadata:
if len(result) >= limit: