跳到內容 跳到搜尋

動作傳遞 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 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
  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