From: Raphael Michel Date: Fri, 25 Jan 2019 15:45:11 +0000 (+0100) Subject: Throw a FinTSNoResponseError if no HISPA1 is returned X-Git-Tag: v2.0.1~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=21a209bba49a56d915450451c2adbe6a31c79b11;p=thirdparty%2Fpython-fints.git Throw a FinTSNoResponseError if no HISPA1 is returned --- diff --git a/fints/client.py b/fints/client.py index 8bae84e..8bc225c 100644 --- a/fints/client.py +++ b/fints/client.py @@ -408,7 +408,7 @@ class FinTS3Client: response = dialog.send(HKSPA1()) self.accounts = [] - for seg in response.find_segments(HISPA1): + for seg in response.find_segments(HISPA1, throw=True): self.accounts.extend(seg.accounts) return [a for a in [acc.as_sepa_account() for acc in self.accounts] if a] diff --git a/fints/exceptions.py b/fints/exceptions.py index 57ae058..4d6919e 100644 --- a/fints/exceptions.py +++ b/fints/exceptions.py @@ -32,3 +32,7 @@ class FinTSConnectionError(FinTSError): class FinTSUnsupportedOperation(FinTSError): pass + + +class FinTSNoResponseError(FinTSError): + pass diff --git a/fints/types.py b/fints/types.py index b44a12f..8adca2d 100644 --- a/fints/types.py +++ b/fints/types.py @@ -1,6 +1,7 @@ from collections import Iterable, OrderedDict from contextlib import suppress +from .exceptions import FinTSNoResponseError from .utils import SubclassesMixin @@ -244,7 +245,7 @@ class SegmentSequence: first_line_suffix=docstring) stream.write((prefix + level * indent) + "]){}\n".format(trailer)) - def find_segments(self, query=None, version=None, callback=None, recurse=True): + def find_segments(self, query=None, version=None, callback=None, recurse=True, throw=False): """Yields an iterable of all matching segments. :param query: Either a str or class specifying a segment type (such as 'HNHBK', or :class:`~fints.segments.message.HNHBK3`), or a list or tuple of strings or classes. @@ -253,9 +254,11 @@ class SegmentSequence: If a list/tuple is specified, segments returning any matching version will be returned. :param callback: A callable that will be given the segment as its sole argument and must return a boolean indicating whether to return this segment. :param recurse: If True (the default), recurse into SegmentSequenceField values, otherwise only look at segments in this SegmentSequence. + :param throw: If True, a FinTSNoResponseError is thrown if no result is found. Defaults to False. The match results of all given parameters will be AND-combined. """ + found_something = False if query is None: query = [] @@ -275,12 +278,20 @@ class SegmentSequence: ((not version) or any(s.header.version == v for v in version)) and \ callback(s): yield s + found_something = True if recurse: for name, field in s._fields.items(): val = getattr(s, name) if val and hasattr(val, 'find_segments'): - yield from val.find_segments(query=query, version=version, callback=callback, recurse=recurse) + for v in val.find_segments(query=query, version=version, callback=callback, recurse=recurse): + yield v + found_something = True + + if throw and not found_something: + raise FinTSNoResponseError( + 'The bank\'s response did not contain a response to your request, please inspect debug log.' + ) def find_segment_first(self, *args, **kwargs): """Finds the first matching segment.