Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
404 -> PickError
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-mellor committed Oct 24, 2024
1 parent 3c1f03e commit 77ba5b1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
15 changes: 13 additions & 2 deletions src/faebryk/libs/picker/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ class ApiError(Exception): ...
class ApiNotConfiguredError(ApiError): ...


class ApiHTTPError(ApiError):
def __init__(self, error: requests.exceptions.HTTPError):
super().__init__()
self.response = error.response

def __str__(self) -> str:
status_code = self.response.status_code
detail = self.response.json()["detail"]
return f"{super().__str__()}: {status_code} {detail}"


def check_compatible_parameters(
module: Module, component: Component, mapping: list[MappingParameterDB]
) -> bool:
Expand Down Expand Up @@ -177,7 +188,7 @@ def _get(self, url: str, timeout: float = 10) -> requests.Response:
response = self._client.get(f"{self.config.api_url}{url}", timeout=timeout)
response.raise_for_status()
except requests.exceptions.HTTPError as e:
raise ApiError(f"Failed to fetch {url}: {e}") from e
raise ApiHTTPError(e) from e

return response

Expand All @@ -190,7 +201,7 @@ def _post(
)
response.raise_for_status()
except requests.exceptions.HTTPError as e:
raise ApiError(f"Failed to fetch {url}: {e}") from e
raise ApiHTTPError(e) from e

return response

Expand Down
12 changes: 6 additions & 6 deletions src/faebryk/libs/picker/api/pickers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import faebryk.library._F as F
import faebryk.libs.picker.api.picker_lib as picker_lib
from faebryk.core.module import Module
from faebryk.libs.picker.api.api import ApiNotConfiguredError
from faebryk.libs.picker.api.api import ApiHTTPError
from faebryk.libs.picker.common import StaticPartPicker
from faebryk.libs.picker.jlcpcb.jlcpcb import Component
from faebryk.libs.picker.picker import PickError
Expand All @@ -14,11 +14,11 @@ class ApiPicker(F.has_multi_picker.FunctionPicker):
def pick(self, module: Module):
try:
super().pick(module)
except ApiNotConfiguredError:
raise
# TODO do we actually ever want to catch this?
# except ApiError as e:
# raise PickError(e.args[0], module) from e
except ApiHTTPError as e:
if e.response.status_code == 404:
raise PickError(str(e), module) from e
else:
raise


class StaticApiPartPicker(StaticPartPicker):
Expand Down

0 comments on commit 77ba5b1

Please sign in to comment.