Catch and handle recaptcha API errors
authorMagnus Hagander <magnus@hagander.net>
Mon, 14 Nov 2016 17:53:10 +0000 (18:53 +0100)
committerMagnus Hagander <magnus@hagander.net>
Mon, 14 Nov 2016 17:53:42 +0000 (18:53 +0100)
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

index e61c5a12dd672410868c81257eb10c0160c52d8e..0af7b1bfe9715ef9db6b90a217c9a20472a43946 100644 (file)
@@ -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: