From 24ccec08b10ba1400f89852c5a2e272c41bfe2a8 Mon Sep 17 00:00:00 2001 From: desiena Date: Fri, 27 Dec 2024 00:45:44 +0100 Subject: [PATCH] Message/error management refactoring --- fmdata/fmclient.py | 4 +-- fmdata/results.py | 42 +++++++++++++++----------------- tests/integration/test_server.py | 4 +-- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/fmdata/fmclient.py b/fmdata/fmclient.py index 87468fc..d57f98d 100644 --- a/fmdata/fmclient.py +++ b/fmdata/fmclient.py @@ -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: @@ -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: diff --git a/fmdata/results.py b/fmdata/results.py index 310c55c..9eda39e 100644 --- a/fmdata/results.py +++ b/fmdata/results.py @@ -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) @@ -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): diff --git a/tests/integration/test_server.py b/tests/integration/test_server.py index 874378e..579e228 100644 --- a/tests/integration/test_server.py +++ b/tests/integration/test_server.py @@ -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 @@ -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)