Move descriptions for rate limiting errors into the exception

This commit is contained in:
Robin Ward 2015-09-24 12:25:09 -04:00
parent a1877e8292
commit 3620c8c85e
2 changed files with 14 additions and 12 deletions

View File

@ -76,17 +76,7 @@ class ApplicationController < ActionController::Base
# If they hit the rate limiter
rescue_from RateLimiter::LimitExceeded do |e|
time_left = ""
if e.available_in < 1.minute.to_i
time_left = I18n.t("rate_limiter.seconds", count: e.available_in)
elsif e.available_in < 1.hour.to_i
time_left = I18n.t("rate_limiter.minutes", count: (e.available_in / 1.minute.to_i))
else
time_left = I18n.t("rate_limiter.hours", count: (e.available_in / 1.hour.to_i))
end
render_json_error I18n.t("rate_limiter.too_many_requests", time_left: time_left), type: :rate_limit, status: 429
render_json_error e.description, type: :rate_limit, status: 429
end
rescue_from PG::ReadOnlySqlTransaction do |e|

View File

@ -2,10 +2,22 @@ class RateLimiter
# A rate limit has been exceeded.
class LimitExceeded < StandardError
attr_accessor :available_in
def initialize(available_in)
@available_in = available_in
end
def description
time_left = ""
if @available_in < 1.minute.to_i
time_left = I18n.t("rate_limiter.seconds", count: @available_in)
elsif @available_in < 1.hour.to_i
time_left = I18n.t("rate_limiter.minutes", count: (@available_in / 1.minute.to_i))
else
time_left = I18n.t("rate_limiter.hours", count: (@available_in / 1.hour.to_i))
end
I18n.t("rate_limiter.too_many_requests", time_left: time_left)
end
end
end