Skip to content

Commit

Permalink
Message/error management refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
desiena committed Dec 26, 2024
1 parent 0d29e2d commit 24ccec0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
4 changes: 2 additions & 2 deletions fmdata/fmclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def wrapper(self: FMClient, *args, **kwargs):

result: BaseResult = f(self, *args, **kwargs)
invalid_token_error = next(
result.get_errors_iterator(include_codes=[FMErrorEnum.INVALID_FILEMAKER_DATA_API_TOKEN]), None)
result.get_messages_iterator(search_codes=[FMErrorEnum.INVALID_FILEMAKER_DATA_API_TOKEN]), None)

# If not invalid token error, return result immediately
if not invalid_token_error:
Expand Down Expand Up @@ -736,7 +736,7 @@ def page_generator(
has_messages = any(result.messages_iterator)
if has_messages:
message_is_record_is_missing = any(
result.get_errors_iterator(include_codes=[FMErrorEnum.RECORD_IS_MISSING]))
result.get_messages_iterator(search_codes=[FMErrorEnum.RECORD_IS_MISSING]))
if message_is_record_is_missing:
is_final_page = True
else:
Expand Down
42 changes: 20 additions & 22 deletions fmdata/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def _get_int(input: FMErrorEnum | int):
@dataclass(frozen=True)
class BaseResult(BaseProxy):

def _message_codes_that_are_not_considered_errors(self) -> List[int]:
return [FMErrorEnum.NO_ERROR.value]

@cached_property
def messages(self) -> List[Message]:
return list(self.messages_iterator)
Expand All @@ -46,38 +49,33 @@ def messages(self) -> List[Message]:
def messages_iterator(self) -> Iterator[Message]:
return (Message(msg) for msg in self.raw_content['messages'])

def get_errors(self,
include_codes: Optional[List[FMErrorEnum | int]] = None,
exclude_codes: Optional[List[FMErrorEnum | int]] = None
) -> List[Message]:

return list(self.get_errors_iterator(include_codes=include_codes, exclude_codes=exclude_codes))

def get_errors_iterator(self,
include_codes: Optional[List[FMErrorEnum | int]] = None,
exclude_codes: Optional[List[FMErrorEnum | int]] = None
) -> Iterator[Message]:

if exclude_codes is None:
exclude_codes = [FMErrorEnum.NO_ERROR]

int_include_codes = [_get_int(code) for code in include_codes] if include_codes is not None else None
def get_messages_iterator(self,
search_codes: Optional[List[FMErrorEnum | int]] = None,
exclude_codes: Optional[List[FMErrorEnum | int]] = None
) -> Iterator[Message]:
int_include_codes = [_get_int(code) for code in search_codes] if search_codes is not None else None
int_exclude_codes = [_get_int(code) for code in exclude_codes] if exclude_codes is not None else None

return (msg for msg in self.messages
if int(msg.code) not in int_exclude_codes and (
int_include_codes is None or (int(msg.code) in int_include_codes)))

def raise_exception_if_has_error(self,
include_codes: Optional[List[FMErrorEnum | int]] = None,
exclude_codes: Optional[List[FMErrorEnum | int]] = None
) -> None:

error = next(self.get_errors_iterator(include_codes=include_codes, exclude_codes=exclude_codes), None)
def raise_exception_if_has_message(self,
include_codes: Optional[List[FMErrorEnum | int]] = None,
exclude_codes: Optional[List[FMErrorEnum | int]] = None
) -> None:
error = next(self.get_messages_iterator(search_codes=include_codes, exclude_codes=exclude_codes), None)

if error is not None:
raise FileMakerErrorException(code=error.code, message=error.message)

@cached_property
def errors(self) -> List[Message]:
return list(self.get_messages_iterator(exclude_codes=self._message_codes_that_are_not_considered_errors()))

def raise_exception_if_has_error(self) -> None:
self.raise_exception_if_has_message(exclude_codes=self._message_codes_that_are_not_considered_errors())


@dataclass(frozen=True)
class LogoutResult(BaseResult, BaseProxy):
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_create_get_delete_record(self) -> None:
query=[{'FullName': 'TestRecord0'}]
)

result.raise_exception_if_has_error(exclude_codes=[FMErrorEnum.NO_ERROR, FMErrorEnum.NO_RECORDS_MATCH_REQUEST])
result.raise_exception_if_has_message(exclude_codes=[FMErrorEnum.NO_ERROR, FMErrorEnum.NO_RECORDS_MATCH_REQUEST])

found_set = result.found_set

Expand All @@ -51,7 +51,7 @@ def test_create_get_delete_record(self) -> None:
)

# Assert that the record has been deleted
result.raise_exception_if_has_error(exclude_codes=[FMErrorEnum.NO_RECORDS_MATCH_REQUEST])
result.raise_exception_if_has_message(exclude_codes=[FMErrorEnum.NO_RECORDS_MATCH_REQUEST])

found_set = result.found_set
self.assertTrue(found_set.empty)

0 comments on commit 24ccec0

Please sign in to comment.