跳至内容 跳至搜索

Action Dispatch PublicExceptions

当被调用时,此中间件将渲染一个错误页面。默认情况下,如果预期是 HTML 响应,它将从 /public 目录渲染静态错误页面。例如,当此中间件接收到 500 响应时,它将渲染 /public/500.html 中找到的模板。如果设置了国际化区域设置,此中间件将尝试渲染 /public/500.<locale>.html 中的模板。如果找不到国际化模板,它将回退到 /public/500.html

当请求的内容类型不是 HTML 时,此中间件将尝试将错误信息转换为适当的响应类型。

方法
C
N

Attributes

[RW] public_path

类公共方法

new(public_path)

# File actionpack/lib/action_dispatch/middleware/public_exceptions.rb, line 21
def initialize(public_path)
  @public_path = public_path
end

实例公共方法

call(env)

# File actionpack/lib/action_dispatch/middleware/public_exceptions.rb, line 25
def call(env)
  request      = ActionDispatch::Request.new(env)
  status       = request.path_info[1..-1].to_i
  content_type = request.formats.first
  body = { status: status, error: Rack::Utils::HTTP_STATUS_CODES.fetch(status, Rack::Utils::HTTP_STATUS_CODES[500]) }

  if env["action_dispatch.original_request_method"] == "HEAD"
    render_format(status, content_type, "")
  else
    render(status, content_type, body)
  end
end