Skip to content

Commit 9a27858

Browse files
committed
[FIX] google_calendar: user-friendly error message on synchronisation fail
Give a chance to the user to solve the issue by himself when the Google synchronization is failing, by displaying the actual error returned by Google regarding the error. The case of the OPW behind this revision is the synchronisation from odoo to google calendar of an event with an attendee having an invalid email address. In such a case, Google returns the meaningful error: "Invalid attendee email". We better pass this information to the user, along with the event causing the trouble, so he gets a chance to solve the issue by himself. I take into account the revision 67c281c Multiple attempts have been done by Julien to solve this issue genericly, by catching the error and raising an `UserError` in the helper `_do_request` directly, but this caused various issues for methods expecting `_do_request` to return `HTTPError`, which suddenly returned `UserError` instead. I therefore chose to catch the error directly in `create_an_event`, which is only called at one place, and this place doesn't expect to catch `HTTPError` exceptions. (no `try...except...`) opw-2334442 closes odoo#58696 Signed-off-by: Denis Ledoux (dle) <[email protected]>
1 parent 36b1a7a commit 9a27858

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

addons/google_calendar/i18n/google_calendar.pot

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ msgstr ""
227227
msgid "The account you are trying to synchronize (%s) is not the same as the last one used (%s)!"
228228
msgstr ""
229229

230+
#. module: google_calendar
231+
#: code:addons/google_calendar/models/google_calendar.py:277
232+
#, python-format
233+
msgid "The event \"%s\", %s (ID: %s) cannot be synchronized because of the following error: %s"
234+
msgstr ""
235+
230236
#. module: google_calendar
231237
#: model:ir.model.fields,field_description:google_calendar.field_res_users_google_calendar_token_validity
232238
msgid "Token Validity"

addons/google_calendar/models/google_calendar.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from werkzeug import urls
1313

1414
from odoo import api, fields, models, tools, _
15+
from odoo.exceptions import UserError
1516
from odoo.osv import expression
1617
from odoo.tools import exception_to_unicode
1718

@@ -263,7 +264,20 @@ def create_an_event(self, event):
263264
url = "/calendar/v3/calendars/%s/events?fields=%s&access_token=%s" % ('primary', urls.url_quote('id,updated'), self.get_token())
264265
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
265266
data_json = json.dumps(data)
266-
return self.env['google.service']._do_request(url, data_json, headers, type='POST')
267+
try:
268+
return self.env['google.service']._do_request(url, data_json, headers, type='POST')
269+
except requests.HTTPError as e:
270+
try:
271+
response = e.response.json()
272+
error = response.get('error', {}).get('message')
273+
except Exception:
274+
error = None
275+
if not error:
276+
raise e
277+
message = _('The event "%s", %s (ID: %s) cannot be synchronized because of the following error: %s') % (
278+
event.name, event.start, event.id, error
279+
)
280+
raise UserError(message)
267281

268282
def delete_an_event(self, event_id):
269283
""" Delete the given event in primary calendar of google cal.

0 commit comments

Comments
 (0)