Sha256: d40412095f2dd63179238da32912e1ff99c88c3684b74854f40d154ad131842e

Contents?: true

Size: 1.38 KB

Versions: 44

Compression:

Stored size: 1.38 KB

Contents

module ActionDispatch
  class PublicExceptions
    attr_accessor :public_path

    def initialize(public_path)
      @public_path = public_path
    end

    def call(env)
      status       = env["PATH_INFO"][1..-1]
      request      = ActionDispatch::Request.new(env)
      content_type = request.formats.first
      body         = { :status => status, :error => Rack::Utils::HTTP_STATUS_CODES.fetch(status.to_i, Rack::Utils::HTTP_STATUS_CODES[500]) }

      render(status, content_type, body)
    end

    private

    def render(status, content_type, body)
      format = "to_#{content_type.to_sym}" if content_type
      if format && body.respond_to?(format)
        render_format(status, content_type, body.public_send(format))
      else
        render_html(status)
      end
    end

    def render_format(status, content_type, body)
      [status, {'Content-Type' => "#{content_type}; charset=#{ActionDispatch::Response.default_charset}",
                'Content-Length' => body.bytesize.to_s}, [body]]
    end

    def render_html(status)
      found = false
      path = "#{public_path}/#{status}.#{I18n.locale}.html" if I18n.locale
      path = "#{public_path}/#{status}.html" unless path && (found = File.exist?(path))

      if found || File.exist?(path)
        render_format(status, 'text/html', File.read(path))
      else
        [404, { "X-Cascade" => "pass" }, []]
      end
    end
  end
end

Version data entries

44 entries across 44 versions & 3 rubygems

Version Path
actionpack-4.1.7.1 lib/action_dispatch/middleware/public_exceptions.rb
actionpack-4.0.11.1 lib/action_dispatch/middleware/public_exceptions.rb
actionpack-4.1.7 lib/action_dispatch/middleware/public_exceptions.rb
actionpack-4.0.11 lib/action_dispatch/middleware/public_exceptions.rb
nanumfont-rails-0.1 vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.6/lib/action_dispatch/middleware/public_exceptions.rb
actionpack-4.0.10 lib/action_dispatch/middleware/public_exceptions.rb
actionpack-4.1.6 lib/action_dispatch/middleware/public_exceptions.rb
actionpack-4.1.6.rc2 lib/action_dispatch/middleware/public_exceptions.rb
actionpack-4.0.10.rc2 lib/action_dispatch/middleware/public_exceptions.rb
actionpack-4.1.6.rc1 lib/action_dispatch/middleware/public_exceptions.rb
actionpack-4.0.10.rc1 lib/action_dispatch/middleware/public_exceptions.rb
actionpack-4.0.9 lib/action_dispatch/middleware/public_exceptions.rb
actionpack-4.1.5 lib/action_dispatch/middleware/public_exceptions.rb
actionpack-4.1.4 lib/action_dispatch/middleware/public_exceptions.rb
actionpack-4.0.8 lib/action_dispatch/middleware/public_exceptions.rb
actionpack-4.1.3 lib/action_dispatch/middleware/public_exceptions.rb
actionpack-4.0.7 lib/action_dispatch/middleware/public_exceptions.rb
actionpack-4.0.6 lib/action_dispatch/middleware/public_exceptions.rb
actionpack-4.1.2 lib/action_dispatch/middleware/public_exceptions.rb
actionpack-4.1.2.rc3 lib/action_dispatch/middleware/public_exceptions.rb