[FIX] payment_mercado_pago: handle mercado pago no body responses

Issue:
When using a mercado pago invalid access token with extra tabs, we get 403 response from mercado pago without a body
which raises and exception while handling this exception we fail to parse the response as it has no body.

line causing the issue: https://github.com/odoo/odoo/blob/9764e6f7fe39a10f3b04e1764110d8c274d0431a/addons/payment_mercado_pago/models/payment_provider.py#L70

Steps to reproduce:
1- Enable mercado pago as a payment provider
2- Set a valid access token for mercado pago with extra tabs
3- Go to website
4- Fill the cart
5- Checkout with the cart using mercado pago
6- You see error message of unhandled json parsing error

Solution:
We should wrap parsing the response in a try statement to handle the responses without body

opw-3654133

closes odoo/odoo#152858

X-original-commit: 952a64423e7cabf7cd3ef700dff7be2bfba1b56a
Signed-off-by: Omar Abosamaha (abom) <abom@odoo.com>
This commit is contained in:
OmarAbosamaha
2024-02-06 20:16:30 +00:00
parent 4bfdbb2141
commit ac78749ec0
2 changed files with 25 additions and 10 deletions
@@ -4,10 +4,10 @@
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 17.0\n"
"Project-Id-Version: Odoo Server 17.0+e\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-10-26 21:56+0000\n"
"PO-Revision-Date: 2023-10-26 21:56+0000\n"
"POT-Creation-Date: 2024-02-06 13:57+0000\n"
"PO-Revision-Date: 2024-02-06 13:57+0000\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
@@ -147,6 +147,15 @@ msgid ""
"information: '%s' (code %s)"
msgstr ""
#. module: payment_mercado_pago
#. odoo-python
#: code:addons/payment_mercado_pago/models/payment_provider.py:0
#, python-format
msgid ""
"The communication with the API failed. The response is empty. Please verify "
"your access token."
msgstr ""
#. module: payment_mercado_pago
#: model:ir.model.fields,help:payment_mercado_pago.field_payment_provider__code
msgid "The technical code of this payment provider."
@@ -65,13 +65,19 @@ class PaymentProvider(models.Model):
_logger.exception(
"Invalid API request at %s with data:\n%s", url, pprint.pformat(payload),
)
response_content = response.json()
error_code = response_content.get('error')
error_message = response_content.get('message')
raise ValidationError("Mercado Pago: " + _(
"The communication with the API failed. Mercado Pago gave us the following "
"information: '%s' (code %s)", error_message, error_code
))
try:
response_content = response.json()
error_code = response_content.get('error')
error_message = response_content.get('message')
raise ValidationError("Mercado Pago: " + _(
"The communication with the API failed. Mercado Pago gave us the"
" following information: '%s' (code %s)", error_message, error_code
))
except ValueError: # The response can be empty when the access token is wrong.
raise ValidationError("Mercado Pago: " + _(
"The communication with the API failed. The response is empty. Please"
" verify your access token."
))
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout):
_logger.exception("Unable to reach endpoint at %s", url)
raise ValidationError(