Skip to content

Commit

Permalink
Merge pull request #290 from dpsk/master
Browse files Browse the repository at this point in the history
Change to_xml method options in the default error_formatter
  • Loading branch information
dblock committed Dec 13, 2012
2 parents efeb92b + 7984e1f commit 70809e2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* [#277](https://github.com/intridea/grape/pull/277): Added a DSL to declare `formatter` in API settings - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
* [#284](https://github.com/intridea/grape/pull/284): Added a DSL to declare `error_formatter` in API settings - [@dblock](https://github.com/dblock).
* [#285](https://github.com/intridea/grape/pull/285): Removed `error_format` from API settings, now matches request format - [@dblock](https://github.com/dblock).
* [#290](https://github.com/intridea/grape/pull/290): Add more readable root in xml `error_formatter` - [@dpsk](https://github.com/dpsk)
* Your contribution here.

0.2.2
Expand Down
1 change: 1 addition & 0 deletions grape.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rack-test'
s.add_development_dependency 'rspec', '~> 2.9'
s.add_development_dependency 'bundler'
s.add_development_dependency 'builder'

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/error_formatter/xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ module Xml
class << self

def call(message, backtrace, options = {})
result = message.is_a?(Hash) ? message : { :error => message }
result = message.is_a?(Hash) ? message : { :message => message }
if (options[:rescue_options] || {})[:backtrace] && backtrace && ! backtrace.empty?
result = result.merge({ :backtrace => backtrace })
end
result.respond_to?(:to_xml) ? result.to_xml : result.to_s
result.respond_to?(:to_xml) ? result.to_xml(:root => :error) : result.to_s
end

end
Expand Down
24 changes: 22 additions & 2 deletions spec/grape/middleware/exception_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'active_support/core_ext/hash'

describe Grape::Middleware::Error do

Expand Down Expand Up @@ -104,14 +105,33 @@ def app
'{"detail":"missing widget","error":"rain!"}'].should be_include(last_response.body)
end

it 'should be possible to return errors in xml format' do
@app ||= Rack::Builder.app do
use Grape::Middleware::Error, :rescue_all => true, :format => :xml
run ExceptionApp
end
get '/'
last_response.body.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<error>\n <message>rain!</message>\n</error>\n"
end

it 'should be possible to return hash errors in xml format' do
@app ||= Rack::Builder.app do
use Grape::Middleware::Error, :rescue_all => true, :format => :xml
run ErrorHashApp
end
get '/'
["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<error>\n <detail>missing widget</detail>\n <error>rain!</error>\n</error>\n",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<error>\n <error>rain!</error>\n <detail>missing widget</detail>\n</error>\n"].should be_include(last_response.body)
end

it 'should be possible to specify a custom formatter' do
@app ||= Rack::Builder.app do
use Grape::Middleware::Error,
:rescue_all => true,
:format => :custom,
:error_formatters => {
:custom => lambda { |message, backtrace, options|
{ :custom_formatter => message }.inspect
:custom => lambda { |message, backtrace, options|
{ :custom_formatter => message }.inspect
}
}
run ExceptionApp
Expand Down

0 comments on commit 70809e2

Please sign in to comment.