🐛 fix(service.py): handle case when response is None to prevent UnboundLocalError

🐛 fix(service.py): handle case when response is None to prevent UnboundLocalError and raise ValueError with error message from response if available
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-10-23 23:46:07 -03:00
commit b33878088e

View file

@ -122,6 +122,7 @@ class StoreService(Service):
headers = {"Authorization": f"Bearer {api_key}"}
component_dict = component_data.dict(exclude_unset=True)
# Parent is a UUID, but the store expects a string
response = None
if component_dict.get("parent"):
component_dict["parent"] = str(component_dict["parent"])
try:
@ -132,4 +133,11 @@ class StoreService(Service):
component = response.json()["data"]
return ComponentResponse(**component)
except HTTPError as exc:
if response:
try:
errors = response.json()
message = errors["errors"][0]["message"]
raise ValueError(message)
except UnboundLocalError:
pass
raise ValueError(f"Upload failed: {exc}")