Skip to content

Commit

Permalink
Api catches *all* errors, which means you can't use Goliath::Rack::Va…
Browse files Browse the repository at this point in the history
…lidationError 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
  • Loading branch information
Philip (flip) Kromer committed Apr 18, 2011
1 parent f392f43 commit 87e0897
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/goliath/api.rb
Expand Up @@ -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"))

Expand Down
38 changes: 38 additions & 0 deletions 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
Expand All @@ -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

0 comments on commit 87e0897

Please sign in to comment.