Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Trigger on_failure for 404s #122

Merged
merged 1 commit into from

3 participants

@muffinista

Unless I'm mistaken, Curl::Easy doesn't actually trigger on_failure for a 404 response. The docs say that it is thrown only for 50x errors, which matches my personal experience, and I've also seen it echoed here: http://stackoverflow.com/questions/10076465/ruby-curb-callbacks-order

This pull request adds an on_complete handler which checks to see if the request threw a 404, and if it did, it will call the passed in on_failure method.

@joshrickard

Just stumbled on this issue myself with a dead (404) feed that was not triggering the callbacks. +1

@ezkl ezkl merged commit 1e37df0 into feedjira:master
@ezkl

Thanks, @muffinista!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 20, 2012
  1. @muffinista

    trigger on_failure for 404s

    muffinista authored
This page is out of date. Refresh to see the latest.
Showing with 44 additions and 5 deletions.
  1. +13 −1 lib/feedzirra/feed.rb
  2. +31 −4 spec/feedzirra/feed_spec.rb
View
14 lib/feedzirra/feed.rb
@@ -292,7 +292,19 @@ def self.add_url_to_multi(multi, url, url_queue, responses, options)
options[:on_failure].call(url, c.response_code, c.header_str, c.body_str) if options.has_key?(:on_failure)
end
end
-
+
+ #
+ # trigger on_failure for 404s
+ #
+ curl.on_complete do |c|
+ add_url_to_multi(multi, url_queue.shift, url_queue, responses, options) unless url_queue.empty?
+ responses[url] = c.response_code
+
+ if c.response_code == 404 && options.has_key?(:on_failure)
+ options[:on_failure].call(url, c.response_code, c.header_str, c.body_str)
+ end
+ end
+
curl.on_failure do |c, err|
add_url_to_multi(multi, url_queue.shift, url_queue, responses, options) unless url_queue.empty?
responses[url] = c.response_code
View
35 spec/feedzirra/feed_spec.rb
@@ -373,17 +373,17 @@ def self.able_to_parse?(val)
describe 'on failure' do
before(:each) do
- @headers = "HTTP/1.0 404 Not Found\r\nDate: Thu, 29 Jan 2009 03:55:24 GMT\r\nServer: Apache\r\nX-FB-Host: chi-write6\r\nLast-Modified: Wed, 28 Jan 2009 04:10:32 GMT\r\n"
- @body = 'Page could not be found.'
+ @headers = "HTTP/1.0 500 Something Bad\r\nDate: Thu, 29 Jan 2009 03:55:24 GMT\r\nServer: Apache\r\nX-FB-Host: chi-write6\r\nLast-Modified: Wed, 28 Jan 2009 04:10:32 GMT\r\n"
+ @body = 'Sorry, something broke'
- @easy_curl.stub!(:response_code).and_return(404)
+ @easy_curl.stub!(:response_code).and_return(500)
@easy_curl.stub!(:header_str).and_return(@headers)
@easy_curl.stub!(:body_str).and_return(@body)
end
it 'should call proc if :on_failure option is passed' do
failure = lambda { |url, feed| }
- failure.should_receive(:call).with(@paul_feed[:url], 404, @headers, @body)
+ failure.should_receive(:call).with(@paul_feed[:url], 500, @headers, @body)
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, { :on_failure => failure })
@easy_curl.on_failure.call(@easy_curl)
end
@@ -394,6 +394,33 @@ def self.able_to_parse?(val)
@easy_curl.on_failure.call(@easy_curl)
responses.length.should == 1
+ responses[@paul_feed[:url]].should == 500
+ end
+ end
+
+ describe 'on complete for 404s' do
+ before(:each) do
+ @headers = "HTTP/1.0 404 Not Found\r\nDate: Thu, 29 Jan 2009 03:55:24 GMT\r\nServer: Apache\r\nX-FB-Host: chi-write6\r\nLast-Modified: Wed, 28 Jan 2009 04:10:32 GMT\r\n"
+ @body = 'Page could not be found.'
+
+ @easy_curl.stub!(:response_code).and_return(404)
+ @easy_curl.stub!(:header_str).and_return(@headers)
+ @easy_curl.stub!(:body_str).and_return(@body)
+ end
+
+ it 'should call proc if :on_failure option is passed' do
+ complete = lambda { |url| }
+ complete.should_receive(:call).with(@paul_feed[:url], 404, @headers, @body)
+ Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, { :on_failure => complete })
+ @easy_curl.on_complete.call(@easy_curl)
+ end
+
+ it 'should return the http code in the responses' do
+ responses = {}
+ Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], responses, {})
+ @easy_curl.on_complete.call(@easy_curl)
+
+ responses.length.should == 1
responses[@paul_feed[:url]].should == 404
end
end
Something went wrong with that request. Please try again.