Skip to content

Let verify_turnstile_request hand a failed check to a handler - #56

Merged
henrikbjorn merged 1 commit into
mainfrom
failure-handler
Nov 12, 2026
Merged

henrikbjorn merged 1 commit into
mainfrom
failure-handler

Conversation

@henrikbjorn

@henrikbjorn henrikbjorn commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

A failed check answered a bare 400, so someone who failed it got a blank page instead of the form. A Cloudflare timeout surfaced as a 500. Tavle and Studio each carry the same lib/gem_ext/turnstiled/controller_methods.rb to fix both.

verify_turnstile_request with: :turnstile_failed, only: %i[create]
verify_turnstile_request(only: %i[create]) { redirect_to root_path }

The handler, modelled on Rails

  • with: takes a method name or a lambda. Its default, the 400, sits in the signature, as rate_limit does it. A block can be passed instead, as with rescue_from.
  • The handler runs the way Rails 8.1's rate_limit runs its with:: with.is_a?(Symbol) ? send(with) : instance_exec(&with), registered as a lambda before_action.
  • With no handler, a failed check still answers 400.

Network errors

  • Verifier#verify rescues Faraday::Error, reports it through Rails.error as a handled warning, and answers false. Controllers never see Faraday.

Also

  • require "faraday": the gem used Faraday without requiring it, so it only worked because the apps load Faraday themselves.
  • The Rails floor goes from 6.0 to 7.0, because Rails.error needs Rails 7.

Checked

  • Gem tests cover a passed check, a failed check with a method name, with a block and with no handler, and an unreachable Cloudflare.
  • Tavle's sign-in and sign-up tests pass with this branch and Tavle's patch deleted, except one. a challenge transport failure is handled like a refusal replaces Turnstiled.verify with a method that raises, which skips the verifier's rescue. Delete it when Tavle upgrades, since the gem's verifier test covers that case.

🤖 Generated with Claude Code

A failed check answered a bare 400, so a person who failed it saw a blank
page instead of the form, and a Cloudflare timeout surfaced as a 500.
Tavle and Studio each carried the same patch to fix both.

with: takes a method name or a lambda, defaulting to the 400 in the
signature as rate_limit does, and a block can be passed instead. The
handler is resolved as rate_limit resolves its with:. The verifier reports
a Faraday error through Rails.error and answers false, so controllers
never see Faraday.

The gem used Faraday without requiring it, and only worked because the
apps load it. Rails.error needs Rails 7.
@henrikbjorn
henrikbjorn merged commit eabae06 into main Nov 12, 2026
2 of 3 checks passed
@henrikbjorn henrikbjorn mentioned this pull request Nov 12, 2026
henrikbjorn added a commit that referenced this pull request Nov 12, 2026
Turbo visits to sign-in pages no longer reload (#55), and
verify_turnstile_request takes a with: handler or block (#56). Minor
rather than patch because #56 raises the Rails floor to 7.0.
@henrikbjorn
henrikbjorn deleted the failure-handler branch November 12, 2026 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant