mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 03:36:18 +08:00
FEATURE: set 'Retry-After' header for 429 responses (#5659)
This commit is contained in:
@ -107,7 +107,15 @@ class ApplicationController < ActionController::Base
|
||||
end
|
||||
|
||||
def render_rate_limit_error(e)
|
||||
render_json_error e.description, type: :rate_limit, status: 429, extras: { wait_seconds: e&.available_in }
|
||||
retry_time_in_seconds = e&.available_in
|
||||
|
||||
render_json_error(
|
||||
e.description,
|
||||
type: :rate_limit,
|
||||
status: 429,
|
||||
extras: { wait_seconds: retry_time_in_seconds },
|
||||
headers: { 'Retry-After': retry_time_in_seconds },
|
||||
)
|
||||
end
|
||||
|
||||
# If they hit the rate limiter
|
||||
@ -523,12 +531,15 @@ class ApplicationController < ActionController::Base
|
||||
|
||||
# Render action for a JSON error.
|
||||
#
|
||||
# obj - a translated string, an ActiveRecord model, or an array of translated strings
|
||||
# obj - a translated string, an ActiveRecord model, or an array of translated strings
|
||||
# opts:
|
||||
# type - a machine-readable description of the error
|
||||
# status - HTTP status code to return
|
||||
# type - a machine-readable description of the error
|
||||
# status - HTTP status code to return
|
||||
# headers - extra headers for the response
|
||||
def render_json_error(obj, opts = {})
|
||||
opts = { status: opts } if opts.is_a?(Integer)
|
||||
opts.fetch(:headers, {}).each { |name, value| headers[name.to_s] = value }
|
||||
|
||||
render json: MultiJson.dump(create_errors_json(obj, opts)), status: opts[:status] || 422
|
||||
end
|
||||
|
||||
|
Reference in New Issue
Block a user