Skip to content

Commit

Permalink
[ADD] EDIWebserviceSendHTTPException to catch http error form edi_web…
Browse files Browse the repository at this point in the history
…service_oca
  • Loading branch information
Matthias BARKAT committed Jun 10, 2024
1 parent 5c39f74 commit 6269113
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
10 changes: 9 additions & 1 deletion edi_webservice_oca/components/send.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
# @author: Simone Orsi <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).


from requests.exceptions import HTTPError

from odoo import _, exceptions

from odoo.addons.component.core import Component

from ..exceptions import EDIWebserviceSendHTTPException


class EDIWebserviceSend(Component):
"""Generic component for webservice requests.
Expand All @@ -27,7 +32,10 @@ def __init__(self, work_context):

def send(self):
method, pargs, kwargs = self._get_call_params()
return self.webservice_backend.call(method, *pargs, **kwargs)
try:
return self.webservice_backend.call(method, *pargs, **kwargs)
except HTTPError as err:
raise EDIWebserviceSendHTTPException("EDI HTTP Error: %s" % err) from err

def _get_call_params(self):
try:
Expand Down
5 changes: 5 additions & 0 deletions edi_webservice_oca/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from requests.exceptions import HTTPError


class EDIWebserviceSendHTTPException(HTTPError):
"""Exception raised when an HTTP error occurs during a webservice call."""
13 changes: 13 additions & 0 deletions edi_webservice_oca/tests/test_send.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from odoo import exceptions

from ..exceptions import EDIWebserviceSendHTTPException
from .common import TestEDIWebserviceBase


Expand Down Expand Up @@ -112,3 +113,15 @@ def test_component_send(self):
responses.calls[0].request.headers["Content-Type"], "application/xml"
)
self.assertEqual(responses.calls[0].request.body, "This is a simple file")

@responses.activate
def test_component_send_raise_http_error(self):
self.record.type_id.set_settings(self.settings2)
record = self.record.with_user(self.a_user)
backend = self.backend.with_user(self.a_user)

url = "https://foo.test/push/here"
responses.add(responses.POST, url, status=404)
component = backend._get_component(record, "send")
with self.assertRaises(EDIWebserviceSendHTTPException):
component.send()

0 comments on commit 6269113

Please sign in to comment.