Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions edi_storage_oca/models/edi_oca_storage_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pathlib import PurePath

from odoo import models
from odoo.exceptions import UserError

from .. import utils

Expand All @@ -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.
Expand All @@ -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)
Expand All @@ -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):
Expand Down
Loading