From 83b4e9073f0852afc065ef398bd3ad9b5a6db29c Mon Sep 17 00:00:00 2001 From: Jorge Bejar Date: Mon, 6 Jul 2015 21:33:19 -0300 Subject: Response when error should be formatted properly in Rails API if local request --- actionpack/lib/action_dispatch/http/parameters.rb | 6 +++--- .../lib/action_dispatch/middleware/debug_exceptions.rb | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) (limited to 'actionpack/lib') 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 -- cgit v1.2.3