From 87e08973360723c2d7c292fb3d57013f47b641f9 Mon Sep 17 00:00:00 2001 From: "Philip (flip) Kromer" Date: Sun, 17 Apr 2011 22:37:18 -0500 Subject: [PATCH] Api catches *all* errors, which means you can't use Goliath::Rack::ValidationError to catch an error raised within the Goliath::API class. I don't really like this implementation, but here's a patch to make Goliath::API pass through any Goliath::Validation::Error exceptions if Goliath::Rack::ValidationError is included --- lib/goliath/api.rb | 4 +++- spec/integration/valid_spec.rb | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/goliath/api.rb b/lib/goliath/api.rb index 1493b78b..0e6fc2ca 100644 --- a/lib/goliath/api.rb +++ b/lib/goliath/api.rb @@ -146,8 +146,10 @@ def call(env) else env[Goliath::Constants::ASYNC_CALLBACK].call([status, headers, body]) end - rescue Exception => e + + raise if (self.class.middlewares || []).map(&:first).any?{|mw| mw <= Goliath::Rack::ValidationError } && e.is_a?(Goliath::Validation::Error) + env.logger.error(e.message) env.logger.error(e.backtrace.join("\n")) diff --git a/spec/integration/valid_spec.rb b/spec/integration/valid_spec.rb index af7d3fe7..0fce10e0 100644 --- a/spec/integration/valid_spec.rb +++ b/spec/integration/valid_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'json' require File.join(File.dirname(__FILE__), '../../', 'examples/valid') describe Valid do @@ -20,3 +21,40 @@ end end end + +class ValidationErrorInEndpointButNoHandler < Goliath::API + use Goliath::Rack::Params + def response(env) + raise Goliath::Validation::Error.new(420, 'YOU MUST CHILL') + end +end + +class ValidationErrorInEndpoint < ValidationErrorInEndpointButNoHandler + use Goliath::Rack::ValidationError +end + +describe ValidationErrorInEndpoint do + let(:err) { Proc.new { fail "API request failed" } } + + it 'handles Goliath::Rack::ValidationError handle the error when included' do + with_api(ValidationErrorInEndpoint) do + get_request({}, err) do |c| + c.response.should == '[:error, "YOU MUST CHILL"]' + c.response_header.status.should == 420 + end + end + end +end + +describe ValidationErrorInEndpointButNoHandler do + let(:err) { Proc.new { fail "API request failed" } } + + it 'treats Goliath::Validation::Error same as any exception' do + with_api(ValidationErrorInEndpointButNoHandler) do + get_request({}, err) do |c| + c.response.should == '[:error, "YOU MUST CHILL"]' + c.response_header.status.should == 400 + end + end + end +end