Skip to content

Commit

Permalink
refactor!: rework errors raised by Client
Browse files Browse the repository at this point in the history
  • Loading branch information
rick-jennings committed Sep 10, 2024
1 parent 3a73310 commit 92bdd26
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 86 deletions.
8 changes: 7 additions & 1 deletion docs/client.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# `Client` class

::: phable.client.Client
::: phable.client.Client

::: phable.client.UnknownRecError

::: phable.client.ErrorGridError

::: phable.client.IncompleteDataError
123 changes: 47 additions & 76 deletions phable/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,41 @@ class HaystackHisWriteOpParametersError(Exception):

@dataclass
class UnknownRecError(Exception):
"""Error raised by `Client` when server's `Grid` response does not include data for
one more more recs being requested.
Parameters:
help_msg: A display to help with troubleshooting.
"""

help_msg: str


@dataclass
class ErrorGridResponseError(Exception):
class ErrorGridError(Exception):
"""Error raised by `Client` when server's `Grid` response meta has an `err` marker
tag.
Parameters:
response:
`Grid` that has `err` marker tag in meta described
[here](https://project-haystack.org/doc/docHaystack/HttpApi#errorGrid).
"""

response: Grid


@dataclass
class IncompleteDataResponseError(Exception):
class IncompleteDataError(Exception):
"""Error raised by `Client` when server's `Grid` response meta has an `incomplete`
tag.
Parameters:
response:
`Grid` that has `incomplete` tag in meta described
[here](https://project-haystack.org/doc/docHaystack/HttpApi#incompleteData).
"""

response: Grid


Expand Down Expand Up @@ -150,14 +175,8 @@ def read(self, filter: str, checked: bool = True) -> Grid:
**Errors**
See `checked` parameter details.
Also, after the request `Grid` is successfully read by the server, the server
may respond with a `Grid` that triggers one of the following errors to be
raised:
1. `ErrorGridResponseError` if the operation fails
2. `IncompleteDataResponseError` if incomplete data is being returned
See `checked` parameter details. Also, this method might raise an
`ErrorGridError` or `IncompleteDataError`.
Parameters:
filter:
Expand Down Expand Up @@ -189,11 +208,7 @@ def read_all(self, filter: str, limit: int | None = None) -> Grid:
**Errors**
After the request `Grid` is successfully read by the server, the server may
respond with a `Grid` that triggers one of the following errors to be raised:
1. `ErrorGridResponseError` if the operation fails
2. `IncompleteDataResponseError` if incomplete data is being returned
This method might raise an `ErrorGridError` or `IncompleteDataError`.
Parameters:
filter:
Expand All @@ -220,14 +235,8 @@ def read_by_id(self, id: Ref, checked: bool = True) -> Grid:
**Errors**
See `checked` parameter details.
Also, after the request `Grid` is successfully read by the server, the server
may respond with a `Grid` that triggers one of the following errors to be
raised:
1. `ErrorGridResponseError` if the operation fails
2. `IncompleteDataResponseError` if incomplete data is being returned
See `checked` parameter details. Also, this method might raise an
`ErrorGridError` or `IncompleteDataError`.
Parameters:
id: Unique identifier for the record being read.
Expand Down Expand Up @@ -259,14 +268,8 @@ def read_by_ids(self, ids: list[Ref]) -> Grid:
**Errors**
Raises an `UnknownRecError` if any of the records cannot be found.
Also, after the request `Grid` is successfully read by the server, the server
may respond with a `Grid` that triggers one of the following errors to be
raised:
1. `ErrorGridResponseError` if the operation fails
2. `IncompleteDataResponseError` if incomplete data is being returned
Raises an `UnknownRecError` if any of the records cannot be found. Also, this
method might raise an `ErrorGridError` or `IncompleteDataError`.
Parameters:
ids: Unique identifiers for the records being read.
Expand Down Expand Up @@ -309,12 +312,7 @@ def his_read(
**Errors**
After the request `Grid` is successfully read by the server, the server
may respond with a `Grid` that triggers one of the following errors to be
raised:
1. `ErrorGridResponseError` if the operation fails
2. `IncompleteDataResponseError` if incomplete data is being returned
This method might raise an `ErrorGridError` or `IncompleteDataError`.
Parameters:
pt_data:
Expand Down Expand Up @@ -356,12 +354,7 @@ def his_read_by_id(
**Errors**
After the request `Grid` is successfully read by the server, the server
may respond with a `Grid` that triggers one of the following errors to be
raised:
1. `ErrorGridResponseError` if the operation fails
2. `IncompleteDataResponseError` if incomplete data is being returned
This method might raise an `ErrorGridError` or `IncompleteDataError`.
Parameters:
id:
Expand Down Expand Up @@ -399,12 +392,7 @@ def his_read_by_ids(
**Errors**
After the request `Grid` is successfully read by the server, the server
may respond with a `Grid` that triggers one of the following errors to be
raised:
1. `ErrorGridResponseError` if the operation fails
2. `IncompleteDataResponseError` if incomplete data is being returned
This method might raise an `ErrorGridError` or `IncompleteDataError`.
Parameters:
ids:
Expand Down Expand Up @@ -460,12 +448,7 @@ def his_write_by_id(
A `HaystackHisWriteOpParametersError` is raised if invalid column names are
used for the `his_rows` parameter.
Also, after the request `Grid` is successfully read by the server, the server
may respond with a `Grid` that triggers one of the following errors to be
raised:
1. `ErrorGridResponseError` if the operation fails
2. `IncompleteDataResponseError` if incomplete data is being returned
Also, this method might raise an `ErrorGridError` or `IncompleteDataError`.
**Additional requirements which are not validated by this method**
Expand Down Expand Up @@ -542,12 +525,7 @@ def his_write_by_ids(
A `HaystackHisWriteOpParametersError` is raised if invalid column names are
used for the `his_rows` parameter.
Also, after the request `Grid` is successfully read by the server, the server
may respond with a `Grid` that triggers one of the following errors to be
raised:
1. `ErrorGridResponseError` if the operation fails
2. `IncompleteDataResponseError` if incomplete data is being returned
Also, this method might raise an `ErrorGridError` or `IncompleteDataError`.
**Additional requirements which are not validated by this method**
Expand Down Expand Up @@ -603,12 +581,7 @@ def point_write(
**Errors**
After the request `Grid` is successfully read by the server, the server
may respond with a `Grid` that triggers one of the following errors to be
raised:
1. `ErrorGridResponseError` if the operation fails
2. `IncompleteDataResponseError` if incomplete data is being returned
This method might raise an `ErrorGridError` or `IncompleteDataError`.
Parameters:
id: Unique identifier of the writable point.
Expand Down Expand Up @@ -639,12 +612,7 @@ def point_write_array(self, id: Ref) -> Grid:
**Errors**
After the request `Grid` is successfully read by the server, the server
may respond with a `Grid` that triggers one of the following errors to be
raised:
1. `ErrorGridResponseError` if the operation fails
2. `IncompleteDataResponseError` if incomplete data is being returned
This method might raise an `ErrorGridError` or `IncompleteDataError`.
Parameters:
id: Unique identifier for the record.
Expand All @@ -661,7 +629,10 @@ def _call(
post_data: Grid = Grid(meta={"ver": "3.0"}, cols=[{"name": "empty"}], rows=[]),
) -> Grid:
"""Sends a POST request based on given parameters, receives a HTTP
response, and returns JSON data."""
response, and returns JSON data.
This method might raise an `ErrorGridError` or `IncompleteDataError`.
"""

headers = {
"Authorization": f"BEARER authToken={self._auth_token}",
Expand Down Expand Up @@ -708,10 +679,10 @@ def _validate_response_meta(response: Grid):

meta = response.meta
if "err" in meta.keys():
raise ErrorGridResponseError(response)
raise ErrorGridError(response)

if "incomplete" in meta.keys():
raise IncompleteDataResponseError(response)
raise IncompleteDataError(response)


def _create_his_read_req_data(
Expand Down
18 changes: 9 additions & 9 deletions phable/hx_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def commit_add(self, recs: dict[str, Any] | list[dict[str, Any]] | Grid) -> Grid
may respond with a `Grid` that triggers one of the following errors to be
raised:
1. `ErrorGridResponseError` if the operation fails
2. `IncompleteDataResponseError` if incomplete data is being returned
1. `ErrorGridError` if the operation fails
2. `IncompleteDataError` if incomplete data is being returned
**Additional info**
Expand All @@ -51,15 +51,15 @@ def commit_remove(self, recs: dict[str, Any] | list[dict[str, Any]] | Grid) -> G
**Errors**
An `ErrorGridResponseError` is raised if any of the recs do not exist on
An `ErrorGridError` is raised if any of the recs do not exist on
the server.
Also, after the request `Grid` is successfully read by the server, the server
may respond with a `Grid` that triggers one of the following errors to be
raised:
1. `ErrorGridResponseError` if the operation fails
2. `IncompleteDataResponseError` if incomplete data is being returned
1. `ErrorGridError` if the operation fails
2. `IncompleteDataError` if incomplete data is being returned
**Additional info**
Expand Down Expand Up @@ -90,8 +90,8 @@ def commit_update(self, recs: dict[str, Any] | list[dict[str, Any]] | Grid) -> G
may respond with a `Grid` that triggers one of the following errors to be
raised:
1. `ErrorGridResponseError` if the operation fails
2. `IncompleteDataResponseError` if incomplete data is being returned
1. `ErrorGridError` if the operation fails
2. `IncompleteDataError` if incomplete data is being returned
**Additional info**
Expand Down Expand Up @@ -122,8 +122,8 @@ def eval(self, expr: str) -> Grid:
may respond with a `Grid` that triggers one of the following errors to be
raised:
1. `ErrorGridResponseError` if the operation fails
2. `IncompleteDataResponseError` if incomplete data is being returned
1. `ErrorGridError` if the operation fails
2. `IncompleteDataError` if incomplete data is being returned
**Additional info**
Expand Down

0 comments on commit 92bdd26

Please sign in to comment.