From c5d162dee737518c983d81ab95b05b08a496210c Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Mon, 14 Nov 2016 18:53:10 +0100 Subject: [PATCH] Catch and handle recaptcha API errors Just send the user an API error and they can try again. Better than internal server error + email to webmaster... Seems to be caused by intermittent network issues. --- pgweb/account/recaptcha.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/pgweb/account/recaptcha.py b/pgweb/account/recaptcha.py index e61c5a12..0af7b1bf 100644 --- a/pgweb/account/recaptcha.py +++ b/pgweb/account/recaptcha.py @@ -57,10 +57,16 @@ class ReCaptchaField(forms.CharField): # if self.remoteip: # param['remoteip'] = self.remoteip - c.request('POST', '/recaptcha/api/siteverify', urllib.urlencode(param), { - 'Content-type': 'application/x-www-form-urlencoded', - }) - c.sock.settimeout(10) + try: + c.request('POST', '/recaptcha/api/siteverify', urllib.urlencode(param), { + 'Content-type': 'application/x-www-form-urlencoded', + }) + c.sock.settimeout(10) + except Exception, e: + # Error to connect at TCP level + log.error('Failed to connect to google recaptcha API: %s' % e) + raise ValidationError('Failed in API call to google recaptcha') + try: r = c.getresponse() except: