mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 11:23:25 +08:00
Move descriptions for rate limiting errors into the exception
This commit is contained in:
parent
a1877e8292
commit
3620c8c85e
|
@ -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|
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user