diff --git a/modules/ing/api/transfer_page.py b/modules/ing/api/transfer_page.py index 343a754314f22bb0eb5a2782647d7e35b70db099..a8505a28cd4c3ab0a275bed90e9cb49614b40a2e 100644 --- a/modules/ing/api/transfer_page.py +++ b/modules/ing/api/transfer_page.py @@ -30,9 +30,7 @@ from weboob.browser.elements import method, DictElement, ItemElement from weboob.browser.filters.json import Dict from weboob.browser.filters.standard import Env, Field, Date -from weboob.capabilities.bank import ( - Recipient, RecipientInvalidIban, RecipientInvalidOTP, -) +from weboob.capabilities.bank import Recipient class TransferINGVirtKeyboard(SimpleVirtualKeyboard): @@ -168,13 +166,6 @@ def check_recipient(self, recipient): rcpt = self.doc return rcpt['accountHolderName'] == recipient.label and rcpt['iban'] == recipient.iban - def handle_error(self): - if 'error' in self.doc: - if self.doc['error']['code'] == 'EXTERNAL_ACCOUNT.IBAN_NOT_FRENCH': - # not using the bank message because it is too generic - raise RecipientInvalidIban(message="L'IBAN doit correpondre à celui d'une banque domiciliée en France.") - assert False, 'Recipient error not handled' - class OtpChannelsPage(LoggedPage, JsonPage): def get_sms_info(self): @@ -186,10 +177,4 @@ def get_sms_info(self): class ConfirmOtpPage(LoggedPage, JsonPage): - def handle_error(self): - if 'error' in self.doc: - error_code = self.doc['error']['code'] - if error_code == 'SCA.WRONG_OTP_ATTEMPT': - raise RecipientInvalidOTP(message=self.doc['error']['message']) - - assert False, 'Recipient OTP error not handled' + pass diff --git a/modules/ing/api_browser.py b/modules/ing/api_browser.py index 1550467e03b93505bc87bf99852cdcef76f4b67d..865b64b8f458e100263d3b7acdd6673361c48784 100644 --- a/modules/ing/api_browser.py +++ b/modules/ing/api_browser.py @@ -30,7 +30,7 @@ from weboob.capabilities.bank import ( TransferBankError, TransferInvalidAmount, AddRecipientStep, RecipientInvalidOTP, - AddRecipientTimeout, AddRecipientBankError, + AddRecipientTimeout, AddRecipientBankError, RecipientInvalidIban, ) from weboob.tools.capabilities.bank.transactions import FrenchTransaction from weboob.tools.value import Value @@ -436,23 +436,38 @@ def send_sms_to_user(self, recipient, sms_info): raise AddRecipientStep(recipient, Value('code', label='Veuillez saisir le code temporaire envoyé par SMS')) def handle_recipient_error(self, r): + # The bank gives an error message when an error occures. + # But sometimes the message is not relevant. + # So I may replace it by nothing or by a custom message. + # The exception to raise can be coupled with: + # * Nothing: empty message + # * None: message of the bank + # * String: custom message + RECIPIENT_ERROR = { + 'SENSITIVE_OPERATION.SENSITIVE_OPERATION_NOT_FOUND': (AddRecipientTimeout,), + 'SENSITIVE_OPERATION.EXPIRED_TEMPORARY_CODE': (AddRecipientTimeout, None), + 'EXTERNAL_ACCOUNT.EXTERNAL_ACCOUNT_ALREADY_EXISTS': (AddRecipientBankError, None), + 'EXTERNAL_ACCOUNT.ACCOUNT_RESTRICTION': (AddRecipientBankError, None), + 'EXTERNAL_ACCOUNT.EXTERNAL_ACCOUNT_IS_INTERNAL_ACCOUT': (AddRecipientBankError, None), # nice spelling + 'EXTERNAL_ACCOUNT.IBAN_NOT_FRENCH': (RecipientInvalidIban, "L'IBAN doit correpondre à celui d'une banque domiciliée en France."), + 'SCA.WRONG_OTP_ATTEMPT': (RecipientInvalidOTP, None), + 'INPUT_INVALID': (AssertionError, None), # invalid request + } + error_page = r.response.json() if 'error' in error_page: error = error_page['error'] - # the error message may seem generic - # but after testing multiple cases - # it is the only time that it appears - if error['code'] == 'SENSITIVE_OPERATION.SENSITIVE_OPERATION_NOT_FOUND': - raise AddRecipientTimeout() - elif error['code'] in ( - 'EXTERNAL_ACCOUNT.EXTERNAL_ACCOUNT_ALREADY_EXISTS', - # not allowed to add a recipient - 'EXTERNAL_ACCOUNT.ACCOUNT_RESTRICTION', - ): - raise AddRecipientBankError(message=error['message']) + error_exception = RECIPIENT_ERROR.get(error['code']) + if error_exception: + if len(error_exception) == 1: + raise error_exception[0]() + elif error_exception[1] is None: + raise error_exception[0](message=error['message']) + else: + raise error_exception[0](message=error_exception[1]) - assert False, 'Recipient error not handled' + assert False, 'Recipient error "%s" not handled' % error['code'] @need_login def end_sms_recipient(self, recipient, code): @@ -477,8 +492,6 @@ def end_sms_recipient(self, recipient, code): self.handle_recipient_error(e) raise - self.page.handle_error() - @need_login @need_to_be_on_website('api') def new_recipient(self, recipient, **params): @@ -507,8 +520,6 @@ def new_recipient(self, recipient, **params): self.handle_recipient_error(e) raise - self.page.handle_error() - assert self.page.check_recipient(recipient), "The recipients don't match." self.add_recipient_info = self.page.doc