1
我正在使用自定义错误控制器。我想在访问500页的URL时有200个响应代码(否则我的中间件拦截500个响应代码给我发送一封例外邮件,只要我想炫耀我的500)通过路由自定义错误的路径 - 读取原始请求的URL
看起来使用self.routes
作为exceptions_app将改变request.path
始终等于错误号
目标
- 访问www.example.com/crashy_url#=>应显示为500的响应代码自定义500错误模板
- 访问www.example.com/500#=>应该显示出与200响应代码
问题
- 访问www.example.com/crashy_url#=>请求的自定义500错误模板.path等于`/ 500',所以我的控制器发送一个200响应代码
我该如何提取真正访问过的URL?
# config/application.rb
config.exceptions_app = self.routes
# routes
match '/500', to: 'errors#server_error', via: :all
# app/controllers/errors_controller.rb
def server_error
@status = 500
can_be_visited_with_ok_response_code
show
end
def can_be_visited_with_ok_response_code
# We want to hide hide the faulty status if the user only wanted to see how our beautiful /xxx page looks like
# BUT `action_dispatch/middleware/debug_exceptions` will change request.path to be equal to the error !!
# @status = 200 if request.path == "/#{@status}" # BAD request.path will always return response code
end
def show
respond_to do |format|
format.html { render 'show', status: @status }
format.all { head @status }
end
end