diff options
author | Jorge Bejar <jorge@wyeworks.com> | 2015-07-06 21:33:19 -0300 |
---|---|---|
committer | Jorge Bejar <jorge@wyeworks.com> | 2015-12-09 10:53:41 -0300 |
commit | 83b4e9073f0852afc065ef398bd3ad9b5a6db29c (patch) | |
tree | dc2f2e814f8c6dcf4a1a0768c58c99e6d295d302 /actionpack | |
parent | b79bfaadaf5b5c6d3e458c24184a0e846bd8cf55 (diff) | |
download | rails-83b4e9073f0852afc065ef398bd3ad9b5a6db29c.tar.gz rails-83b4e9073f0852afc065ef398bd3ad9b5a6db29c.tar.bz2 rails-83b4e9073f0852afc065ef398bd3ad9b5a6db29c.zip |
Response when error should be formatted properly in Rails API if local request
Diffstat (limited to 'actionpack')
5 files changed, 43 insertions, 9 deletions
diff --git a/actionpack/lib/action_dispatch/http/parameters.rb b/actionpack/lib/action_dispatch/http/parameters.rb index 5c20bc53c2..4084d78f49 100644 --- a/actionpack/lib/action_dispatch/http/parameters.rb +++ b/actionpack/lib/action_dispatch/http/parameters.rb @@ -69,15 +69,15 @@ module ActionDispatch def default_path_parameters if format = format_from_path_extension - { 'format' => format } + { format: format } else {} end end def format_from_path_extension - path = @env['action_dispatch.original_path'] - if match = path.match(/\.(\w+)$/) + path = @env['action_dispatch.original_path'] || @env['PATH_INFO'] + if match = path && path.match(/\.(\w+)$/) match.captures.first end end diff --git a/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb b/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb index 66bb74b9c5..972410d806 100644 --- a/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb +++ b/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb @@ -38,9 +38,10 @@ module ActionDispatch end end - def initialize(app, routes_app = nil) + def initialize(app, routes_app = nil, api_only = false) @app = app @routes_app = routes_app + @api_only = api_only end def call(env) @@ -90,7 +91,19 @@ module ActionDispatch ) file = "rescues/#{wrapper.rescue_template}" - if request.xhr? + if @api_only + body = { + :status => wrapper.status_code, + :error => Rack::Utils::HTTP_STATUS_CODES.fetch(wrapper.status_code, Rack::Utils::HTTP_STATUS_CODES[500]), + :exception => wrapper.exception.inspect, + :traces => traces + } + if content_type = request.formats.first + to_format = "to_#{content_type.to_sym}" + body = body.public_send(to_format) + end + format = "application/json" + elsif request.xhr? body = template.render(template: file, layout: false, formats: [:text]) format = "text/plain" else diff --git a/actionpack/test/controller/mime/respond_to_test.rb b/actionpack/test/controller/mime/respond_to_test.rb index c025c7fa00..76e2d3ff43 100644 --- a/actionpack/test/controller/mime/respond_to_test.rb +++ b/actionpack/test/controller/mime/respond_to_test.rb @@ -661,10 +661,6 @@ class RespondToControllerTest < ActionController::TestCase end def test_variant_inline_syntax - get :variant_inline_syntax, format: :js - assert_equal "text/javascript", @response.content_type - assert_equal "js", @response.body - get :variant_inline_syntax assert_equal "text/html", @response.content_type assert_equal "none", @response.body @@ -674,6 +670,12 @@ class RespondToControllerTest < ActionController::TestCase assert_equal "phone", @response.body end + def test_variant_inline_syntax_with_format + get :variant_inline_syntax, format: :js + assert_equal "text/javascript", @response.content_type + assert_equal "js", @response.body + end + def test_variant_inline_syntax_without_block get :variant_inline_syntax_without_block, params: { v: :phone } assert_equal "text/html", @response.content_type diff --git a/actionpack/test/dispatch/debug_exceptions_test.rb b/actionpack/test/dispatch/debug_exceptions_test.rb index 30772bd9ed..48eff700ce 100644 --- a/actionpack/test/dispatch/debug_exceptions_test.rb +++ b/actionpack/test/dispatch/debug_exceptions_test.rb @@ -75,6 +75,13 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest end end + class BoomerAPI < Boomer + def call(env) + env['action_dispatch.show_detailed_exceptions'] = @detailed + raise "puke!" + end + end + RoutesApp = Struct.new(:routes).new(SharedTestRoutes) ProductionApp = ActionDispatch::DebugExceptions.new(Boomer.new(false), RoutesApp) DevelopmentApp = ActionDispatch::DebugExceptions.new(Boomer.new(true), RoutesApp) @@ -155,6 +162,17 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest assert_match(/ActionController::ParameterMissing/, body) end + test "rescue with json on API request" do + @app = ActionDispatch::DebugExceptions.new(BoomerAPI.new(true), RoutesApp, true) + + get "/index.json", headers: { 'action_dispatch.show_exceptions' => true } + assert_response 500 + assert_no_match(/<header>/, body) + assert_no_match(/<body>/, body) + assert_equal "application/json", response.content_type + assert_match(/RuntimeError: puke/, body) + end + test "rescue with text error for xhr request" do @app = DevelopmentApp xhr_request_env = {'action_dispatch.show_exceptions' => true, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'} diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index 8972f3e74d..1b711e1bf8 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -3272,6 +3272,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest expected_params = { controller: 'downloads', action: 'show', + format: 'tar', id: '1' } |