diff --git a/edi_storage_oca/models/edi_oca_storage_handler.py b/edi_storage_oca/models/edi_oca_storage_handler.py index ce0f7682f..3e2a3d938 100644 --- a/edi_storage_oca/models/edi_oca_storage_handler.py +++ b/edi_storage_oca/models/edi_oca_storage_handler.py @@ -9,6 +9,7 @@ from pathlib import PurePath from odoo import models +from odoo.exceptions import UserError from .. import utils @@ -34,7 +35,9 @@ def send(self, exchange_record): utils.add_file(exchange_record.backend_id.storage_id, path.as_posix(), filedata) def receive(self, exchange_record): - return self._get_remote_file(exchange_record, "pending", binary=True) + return self._get_remote_file( + exchange_record, "pending", binary=True, raise_if_missing=True + ) def _dir_by_state(self, backend, direction, state): """Return remote directory path by direction and state. @@ -61,11 +64,21 @@ def _get_remote_file_path(self, exchange_record, state, filename=None): ) return path - def _get_remote_file(self, exchange_record, state, filename=None, binary=False): + def _get_remote_file( + self, + exchange_record, + state, + filename=None, + binary=False, + raise_if_missing=False, + ): """Get file for current exchange_record in the given destination state. :param state: string ("pending", "done", "error") :param filename: custom file name, exchange_record filename used by default + :param raise_if_missing: when True, storage errors are re-raised as + swallable exceptions so the caller transitions the record to an + error state instead of treating "no content" as a successful read. :return: remote file content as string """ path = self._get_remote_file_path(exchange_record, state, filename=filename) @@ -84,14 +97,25 @@ def _get_remote_file(self, exchange_record, state, filename=None, binary=False): path, state, ) + if raise_if_missing: + raise return None - except OSError: - _logger.info( - "Ignored OSError when trying to get file %s into path %s for state %s", + except OSError as err: + _logger.warning( + "OSError when trying to get file %s into path %s for state %s: %s", filename, path, state, + err, ) + if raise_if_missing: + raise UserError( + self.env._( + "Storage error while reading %(path)s: %(error)s", + path=path.as_posix(), + error=str(err), + ) + ) from err return None def check(self, exchange_record):