Permalink
Browse files

Use PublicExceptions code that is in rails master

I've committed this to rails master, we have to keep this file until we
support >= 4.0.0
  • Loading branch information...
1 parent b1d0780 commit 6e52c2a7296a51ef67557deb2be9cef043222bcc @spastorino spastorino committed Jun 12, 2012
Showing with 36 additions and 10 deletions.
  1. +1 −1 lib/rails-api/application.rb
  2. +34 −8 lib/rails-api/public_exceptions.rb
  3. +1 −1 test/api_controller/renderers_test.rb
@@ -27,7 +27,7 @@ def default_middleware_stack
middleware.use ::Rack::Runtime
middleware.use ::ActionDispatch::RequestId
middleware.use ::Rails::Rack::Logger, config.log_tags # must come after Rack::MethodOverride to properly log overridden methods
- middleware.use ::ActionDispatch::ShowExceptions, config.exceptions_app || Rails::API::PublicExceptions.new
+ middleware.use ::ActionDispatch::ShowExceptions, config.exceptions_app || Rails::API::PublicExceptions.new(Rails.public_path)
middleware.use ::ActionDispatch::DebugExceptions
middleware.use ::ActionDispatch::RemoteIp, config.action_dispatch.ip_spoofing_check, config.action_dispatch.trusted_proxies
@@ -1,24 +1,50 @@
module Rails
module API
class PublicExceptions
- def initialize
- @fallback = ActionDispatch::PublicExceptions.new(Rails.public_path)
+ attr_accessor :public_path
+
+ def initialize(public_path)
+ @public_path = public_path
end
def call(env)
exception = env["action_dispatch.exception"]
status = env["PATH_INFO"][1..-1]
request = ActionDispatch::Request.new(env)
content_type = request.formats.first
+ format = content_type && "to_#{content_type.to_sym}"
body = { :status => status, :error => exception.message }
- format = "to_#{Mime[content_type].to_sym}"
- if body.respond_to?(format)
- body = body.public_send(format)
- [status, {'Content-Type' => "#{content_type}; charset=#{ActionDispatch::Response.default_charset}",
- 'Content-Length' => body.bytesize.to_s}, [body]]
+ render(status, body, :format => format, :content_type => content_type)
+ end
+
+ private
+
+ def render(status, body, options)
+ format = options[:format]
+
+ if format && body.respond_to?(format)
+ render_format(status, body.public_send(format), options)
+ else
+ render_html(status)
+ end
+ end
+
+ def render_format(status, body, options)
+ [status, {'Content-Type' => "#{options[: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)
+ body = File.read(path)
+ [status, {'Content-Type' => "text/html; charset=#{Response.default_charset}", 'Content-Length' => body.bytesize.to_s}, [body]]
else
- @fallback.call(env)
+ [404, { "X-Cascade" => "pass" }, []]
end
end
end
@@ -12,7 +12,7 @@ def to_xml(options = {})
end
class RenderersApiController < ActionController::API
- use ActionDispatch::ShowExceptions, Rails::API::PublicExceptions.new
+ use ActionDispatch::ShowExceptions, Rails::API::PublicExceptions.new(Rails.public_path)
def one
render :json => Model.new

0 comments on commit 6e52c2a

Please sign in to comment.