跳至內容 跳至搜尋

Action Dispatch PublicExceptions

呼叫時,此中間件會呈現錯誤頁面。預設情況下,如果預期為 HTML 回應,它會從 /public 目錄呈現靜態錯誤頁面。例如,當此中間件收到 500 回應時,它會呈現位於 /public/500.html 中的範本。如果設定了國際化語言環境,此中間件會嘗試呈現 /public/500.<locale>.html 中的範本。如果找不到國際化範本,它會退回至 /public/500.html

當建立內容類型為非 HTML 的要求時,此中間件會嘗試將錯誤資訊轉換為適當的回應類型。

方法
C
N

屬性

[RW] public_path

類別公共方法

new(public_path)

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

實例公共方法

call(env)

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

  render(status, content_type, body)
end