Skip to content

Commit

Permalink
Added logging for errors from Turnstile (#2533)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuastegmaier committed Sep 19, 2024
1 parent 6a6ca06 commit 021235c
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions concordia/turnstile/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import inspect
import json
from logging import getLogger
from urllib.error import HTTPError
from urllib.parse import urlencode
from urllib.request import ProxyHandler, Request, build_opener
Expand All @@ -13,6 +14,8 @@

from ..turnstile.widgets import TurnstileWidget

logger = getLogger(__name__)


class TurnstileField(forms.Field):
widget = TurnstileWidget
Expand Down Expand Up @@ -64,13 +67,19 @@ def validate(self, value):
try:
response = opener.open(request, timeout=settings.TURNSTILE_TIMEOUT)
except HTTPError as exc:
logger.exception("HTTPError received from Turnstile: ", exc, exc_info=exc)
raise forms.ValidationError(
self.error_messages["error_turnstile"], code="error_turnstile"
) from exc

response_data = json.loads(response.read().decode("utf-8"))

if not response_data.get("success"):
logger.exception(
"Failure received from Turnstile. Error codes: %s. Messages: %s",
response_data.get("error-codes"),
response_data.get("messages"),
)
raise forms.ValidationError(
self.error_messages["invalid_turnstile"], code="invalid_turnstile"
)

0 comments on commit 021235c

Please sign in to comment.