4

I'm using rack attack. If somebody exceeds the limit I'm using the following code:

Rack::Attack.throttled_response = lambda do |env|
  [429, {}, [ActionView::Base.new.render(file: 'public/429.html')]]
end

When sby exceeds the limit on a POST request where the original response would be respond_to :html then the rendering of the 429.html works fine. When the limit is exceeded by a POST request that responds to respond_to :js then nothing happens on the screen, but if I check out the logs everything seems to be fine:

Rendered public/429.html (1.4ms)

How can I display the 429.html in case of js response? Is it possible to pass down error messages from this rack code to the rails app somehow? I may change to error messages from rendering if it's not that complex.

Sean Magyar
  • 2,360
  • 1
  • 25
  • 57

1 Answers1

7
Rack::Attack.throttled_response = lambda do |env|
  html = ActionView::Base.new.render(file: 'public/429.html')
  [503, {'Content-Type' => 'text/html'}, [html]]
end

You can set any response content type in the second params.

Yang
  • 389
  • 2
  • 15