All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jruby.rack.error_app.show_status.rb Maven / Gradle / Ivy

require 'erb'

class JRuby::Rack::ErrorApp

  # catches empty responses and replaces them with a site explaining the error.
  #
  # @note kindly adapted from on Rack::ShowStatus
  # @private internal API - likely won't exist in 1.2
  class ShowStatus

    def initialize(app)
      require 'rack/request'; require 'rack/utils'
      @app = app; @template = ERB.new(TEMPLATE, nil, '-')
    end

    def call(env)
      status, headers, body = @app.call(env)
      headers = Utils::HeaderHash.new(headers)
      empty = headers['Content-Length'].to_i <= 0

      detail = env['rack.showstatus.detail']
      # client or server error, or explicit message
      if (status.to_i >= 400 && empty) || detail
        # required erb template variables (captured with binding) :
        request = req = ::Rack::Request.new(env); request && req # avoid un-used warning
        message = Utils::HTTP_STATUS_CODES[status.to_i] || status.to_s
        detail = detail.nil? ? message : detail # false for no detail

        body = @template.result(binding)
        size = Utils.bytesize(body)
        [status, headers.merge('Content-Type' => "text/html", 'Content-Length' => size.to_s), [body]]
      else
        [status, headers, body]
      end
    end

    # @private
    def h(obj)
      obj.is_a?(String) ? Utils.escape_html(obj) : Utils.escape_html(obj.inspect)
    end

    # :stopdoc:

# adapted from Django 
# Copyright (c) 2005, the Lawrence Journal-World
# Used under the modified BSD license:
# http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5
TEMPLATE = <<'HTML'



  
  <%=h message %> at <%=h request.script_name + request.path_info %>
  
  


  

<%=h message %> (<%= status.to_i %>)

Request Method: <%=h request.request_method %>
Request URL: <%=h request.url %>
<% if detail -%>

<%=h detail %>

<% end %>

You're seeing this error because you use JRuby::Rack::ErrorApp::ShowStatus.

HTML end end




© 2015 - 2025 Weber Informatics LLC | Privacy Policy