Skip to content

Commit

Permalink
fix: retry hcaptcha on timeouts (#16377)
Browse files Browse the repository at this point in the history
  • Loading branch information
miketheman authored Aug 2, 2024
1 parent 54c1583 commit a1f916b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tests/unit/captcha/test_hcaptcha.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# limitations under the License.

import pretend
import pyramid_retry
import pytest
import requests
import responses
Expand Down Expand Up @@ -119,6 +120,18 @@ def test_remote_ip_added(self):
hostname=None,
)

def test_retries_on_timeout(self, monkeypatch):
service = hcaptcha.Service.create_service(
context=None,
request=_REQUEST,
)
monkeypatch.setattr(
service.request.http, "post", pretend.raiser(requests.Timeout)
)

with pytest.raises(pyramid_retry.RetryableException):
service.verify_response("meaningless")

def test_unexpected_error(self, monkeypatch):
service = hcaptcha.Service.create_service(
context=None,
Expand Down
4 changes: 4 additions & 0 deletions warehouse/captcha/hcaptcha.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

from urllib.parse import urlencode

from pyramid_retry import RetryableException
from requests.exceptions import Timeout
from zope.interface import implementer

from .interfaces import ChallengeResponse, ICaptchaService
Expand Down Expand Up @@ -134,6 +136,8 @@ def verify_response(self, response, remote_ip=None) -> ChallengeResponse | None:
},
timeout=10,
)
except Timeout as err:
raise RetryableException from err
except Exception as err:
raise UnexpectedError(str(err)) from err

Expand Down

0 comments on commit a1f916b

Please sign in to comment.