From ac78749ec0cc4522472ec9ce9c4907c45f51b24c Mon Sep 17 00:00:00 2001 From: OmarAbosamaha Date: Wed, 31 Jan 2024 15:19:22 +0000 Subject: [PATCH] [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) --- .../i18n/payment_mercado_pago.pot | 15 +++++++++++--- .../models/payment_provider.py | 20 ++++++++++++------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/addons/payment_mercado_pago/i18n/payment_mercado_pago.pot b/addons/payment_mercado_pago/i18n/payment_mercado_pago.pot index c7df332741a..f0afaddb5ae 100644 --- a/addons/payment_mercado_pago/i18n/payment_mercado_pago.pot +++ b/addons/payment_mercado_pago/i18n/payment_mercado_pago.pot @@ -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." diff --git a/addons/payment_mercado_pago/models/payment_provider.py b/addons/payment_mercado_pago/models/payment_provider.py index d3697e3fb57..c72ed5e68fe 100644 --- a/addons/payment_mercado_pago/models/payment_provider.py +++ b/addons/payment_mercado_pago/models/payment_provider.py @@ -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(