lib/grape/middleware/formatter.rb in grape-0.2.6 vs lib/grape/middleware/formatter.rb in grape-0.3.0
- old
+ new
@@ -22,21 +22,25 @@
end
def after
status, headers, bodies = *@app_response
formatter = Grape::Formatter::Base.formatter_for env['api.format'], options
- bodymap = bodies.collect do |body|
- formatter.call body, env
+ begin
+ bodymap = bodies.collect do |body|
+ formatter.call body, env
+ end
+ rescue Exception => e
+ throw :error, :status => 500, :message => e.message
end
headers['Content-Type'] = content_type_for(env['api.format']) unless headers['Content-Type']
Rack::Response.new(bodymap, status, headers).to_a
end
private
def read_body_input
- if (request.post? || request.put?) && (! request.form_data?) && (! request.parseable_data?) && (request.content_length.to_i > 0)
+ if (request.post? || request.put? || request.patch?) && (! request.form_data?) && (! request.parseable_data?) && (request.content_length.to_i > 0)
if env['rack.input'] && (body = env['rack.input'].read).length > 0
begin
fmt = mime_types[request.media_type] if request.media_type
if content_type_for(fmt)
parser = Grape::Parser::Base.parser_for fmt, options
@@ -48,11 +52,11 @@
rescue Exception => e
throw :error, :status => 400, :message => e.message
end
end
else
- throw :error, :status => 406, :message => 'The requested content-type is not supported.'
+ throw :error, :status => 406, :message => "The requested content-type '#{request.media_type}' is not supported."
end
ensure
env['rack.input'].rewind
end
end
@@ -62,10 +66,10 @@
def negotiate_content_type
fmt = format_from_extension || format_from_params || options[:format] || format_from_header || options[:default_format]
if content_type_for(fmt)
env['api.format'] = fmt
else
- throw :error, :status => 406, :message => 'The requested format is not supported.'
+ throw :error, :status => 406, :message => "The requested format '#{fmt}' is not supported."
end
end
def format_from_extension
parts = request.path.split('.')