From b8fbb95fca33cdaec53dc46bbed332987383b810 Mon Sep 17 00:00:00 2001 From: Alexey Pelykh Date: Mon, 23 Mar 2020 08:14:37 +0100 Subject: [PATCH] [FIX] account_bank_statement_import_online: catch any exception --- .../models/online_bank_statement_provider.py | 10 ++++--- .../online_bank_statement_provider_dummy.py | 10 +++++-- ...st_account_bank_statement_import_online.py | 28 +++++++++++++++++-- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/account_bank_statement_import_online/models/online_bank_statement_provider.py b/account_bank_statement_import_online/models/online_bank_statement_provider.py index 6efb834..41001f3 100644 --- a/account_bank_statement_import_online/models/online_bank_statement_provider.py +++ b/account_bank_statement_import_online/models/online_bank_statement_provider.py @@ -1,10 +1,11 @@ -# Copyright 2019 Brainbean Apps (https://brainbeanapps.com) -# Copyright 2019 Dataplug (https://dataplug.io) +# Copyright 2019-2020 Brainbean Apps (https://brainbeanapps.com) +# Copyright 2019-2020 Dataplug (https://dataplug.io) # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). from dateutil.relativedelta import relativedelta, MO from decimal import Decimal import logging +from sys import exc_info from odoo import models, fields, api, _ from odoo.addons.base.models.res_bank import sanitize_account_number @@ -167,7 +168,8 @@ class OnlineBankStatementProvider(models.Model): statement_date_since, statement_date_until ) - except Exception as e: + except: + e = exc_info()[1] if is_scheduled: _logger.warning( 'Online Bank Statement Provider "%s" failed to' @@ -186,7 +188,7 @@ class OnlineBankStatementProvider(models.Model): provider.name, statement_date_since, statement_date_until, - str(e), + str(e) if e else _('N/A'), ), subject=_( 'Online Bank Statement Provider failure' diff --git a/account_bank_statement_import_online/tests/online_bank_statement_provider_dummy.py b/account_bank_statement_import_online/tests/online_bank_statement_provider_dummy.py index 04b96ca..6c3f087 100644 --- a/account_bank_statement_import_online/tests/online_bank_statement_provider_dummy.py +++ b/account_bank_statement_import_online/tests/online_bank_statement_provider_dummy.py @@ -1,5 +1,5 @@ -# Copyright 2019 Brainbean Apps (https://brainbeanapps.com) -# Copyright 2019 Dataplug (https://dataplug.io) +# Copyright 2019-2020 Brainbean Apps (https://brainbeanapps.com) +# Copyright 2019-2020 Dataplug (https://dataplug.io) # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). from datetime import datetime, timedelta @@ -22,7 +22,11 @@ class OnlineBankStatementProviderDummy(models.Model): ) # pragma: no cover if self.env.context.get('crash', False): - raise Exception('Expected') + exception = self.env.context.get( + 'exception', + Exception('Expected') + ) + raise exception line_step_options = self.env.context.get('step', { 'minutes': 5, diff --git a/account_bank_statement_import_online/tests/test_account_bank_statement_import_online.py b/account_bank_statement_import_online/tests/test_account_bank_statement_import_online.py index 2eb25d1..00be08f 100644 --- a/account_bank_statement_import_online/tests/test_account_bank_statement_import_online.py +++ b/account_bank_statement_import_online/tests/test_account_bank_statement_import_online.py @@ -1,10 +1,10 @@ -# Copyright 2019 Brainbean Apps (https://brainbeanapps.com) -# Copyright 2019 Dataplug (https://dataplug.io) +# Copyright 2019-2020 Brainbean Apps (https://brainbeanapps.com) +# Copyright 2019-2020 Dataplug (https://dataplug.io) # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from dateutil.relativedelta import relativedelta - from psycopg2 import IntegrityError +from urllib.error import HTTPError from odoo.tests import common from odoo.tools import mute_logger @@ -341,3 +341,25 @@ class TestAccountBankAccountStatementImportOnline(common.TransactionCase): self.now - relativedelta(hours=1), self.now, ) + + def test_pull_httperror(self): + journal = self.AccountJournal.create({ + 'name': 'Bank', + 'type': 'bank', + 'code': 'BANK', + 'bank_statements_source': 'online', + 'online_bank_statement_provider': 'dummy', + }) + + provider = journal.online_bank_statement_provider_id + provider.active = True + provider.statement_creation_mode = 'weekly' + + with self.assertRaises(HTTPError): + provider.with_context( + crash=True, + exception=HTTPError(None, 500, 'Error', None, None), + )._pull( + self.now - relativedelta(hours=1), + self.now, + )