mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 06:41:25 +08:00
FIX: return 429 when admin api key is limited on admin route
This also handles a general case where exceptions leak out prior to being handled by the application controller
This commit is contained in:
31
lib/middleware/discourse_public_exceptions.rb
Normal file
31
lib/middleware/discourse_public_exceptions.rb
Normal file
@ -0,0 +1,31 @@
|
||||
# since all the rescue from clauses are not caught by the application controller for matches
|
||||
# we need to handle certain exceptions here
|
||||
module Middleware
|
||||
class DiscoursePublicExceptions < ::ActionDispatch::PublicExceptions
|
||||
|
||||
def initialize(path)
|
||||
super
|
||||
end
|
||||
|
||||
def call(env)
|
||||
# this is so so gnarly
|
||||
# sometimes we leak out exceptions prior to creating a controller instance
|
||||
# this can happen if we have an exception in a route constraint in some cases
|
||||
# this code re-dispatches the exception to our application controller so we can
|
||||
# properly translate the exception to a page
|
||||
exception = env["action_dispatch.exception"]
|
||||
response = ActionDispatch::Response.new
|
||||
|
||||
if exception
|
||||
fake_controller = ApplicationController.new
|
||||
fake_controller.response = response
|
||||
|
||||
if ApplicationController.rescue_with_handler(exception, object: fake_controller)
|
||||
return [response.status, response.headers, response.body]
|
||||
end
|
||||
end
|
||||
super
|
||||
end
|
||||
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user