Processing rack_throttle exceptions in rails application

◇◆丶佛笑我妖孽 提交于 2019-12-04 15:41:08

Finally i found a solution

The problem was in rack-throttle gem. Response body should be an array of strings, so to fix the issue i've just overridden http_error method and that did the trick for me. You should make http_status(code) + (message.nil? ? "\n" : " (#{message})\n") an array so just take it in square brackets.

class Rack::Throttle::Limiter
  def http_error(code, message = nil, headers = {})
    [code, {'Content-Type' => 'text/plain; charset=utf-8'}.merge(headers),
    [http_status(code) + (message.nil? ? "\n" : " (#{message})\n")]]
  end
end

Put this inside config/initializers/rack_throttle.rb. The name of the file may be changed to whatever you like.

You need create a Rack::Middleware to check if there are an exceed limit from rack-throttle and do what you want.

Or you can override the method Rack::Throttle::Limiter#rate_limit_exceeded to do what you really want : https://github.com/datagraph/rack-throttle/blob/master/lib/rack/throttle/limiter.rb#L177

You can pass some option In your Rack::Trottle middelware too :

https://github.com/datagraph/rack-throttle/blob/master/lib/rack/throttle/limiter.rb#L19

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!