views:

321

answers:

4

Basically, we have this here module that we offer to our users that want to include a feed from elsewhere on their pages. I works great, no sweat. The problem is that whenever users mishandle the feed link on their hands we have to manually remove the module from existence because Zend Feed crashes and burns the entire page just like any fatal error. Normally, one would expect that a code block such as..

try { // Test piece straight off the Zend tutorial
    $slashdotRss = Zend_Feed::import('http://rss.slashdot.org/Slashdot/slashdot');
} catch (Zend_Feed_Exception $e) {
    // feed import failed
    echo "Exception caught importing feed: {$e->getMessage()}\n";
    exit;
}

.. would BEHAVE if I were to enter 'httn://rss.grrllarrrlll.aarrg/Slashdot/slashdot' and say something along the lines of "404" or "What the shit". No. It dies. It crashes and dies. It crashes and burns and dies, completely ignoring all that happy trycatch methology right there.

So basically, do we have to write our on feedfetch or is there any simple remedy to Zend's slip?

Added log:

    exception 'Zend_Http_Client_Adapter_Exception' with message 'Unable to Connect to tcp://www.barglllrragglll:80. Error #10946: ' in /library/Zend/Http/Client/Adapter/Socket.php:148
#0 /library/Zend/Http/Client.php(827): Zend_Http_Client_Adapter_Socket->connect('www.barglllrragglll...', 80, false)
#1 /library/Zend/Feed.php(284): Zend_Http_Client->request()
...... Trace etc ....
+1  A: 

How about something along the lines of

$file = file("http://rss.grrrrrrrl..."); $rss = Zend_Feed::importString($file);

?

Amos Robinson
Tried it out with the valid slashdot address and got following angry voice inside my head: "Exception caught importing feed: DOMDocument cannot parse XML: DOMDocument::loadXML() expects parameter 1 to be string, array given", so unfortunately this doesn't seem to be the solution.
Oh but thanks, though.
file() returns an array, you want file_get_contents().
Tom Haigh
+2  A: 

Just out of curiosity, did you try catching other kinds of exception ? ie, not only Zend_Feed_Exception ?

Maybe, if there is some kind of 404 error during the "fetching" phase, it throws another exception ? (Because of relying on another component, like Zend_Http_Client ? )

Also, did you check your error_reporting level, to be sure errors would be reported ? Maybe in some log file somewhere, if display_errors is Off ?


As a sidenot, and not really an answer to your question, but Zend_Feed has some drawbacks (like returning different kind of data depending on the feed's format -- RSS vs ATOM, for instance).

Starting with Zend Framework 1.9 (right now, it's only available as a preview or alpha version, so don't using it in production !), there will be a Zend_Feed_Reader component, which should be more useful when consumming both RSS and ATOM Feeds.

For more informations, see


Edit after you added the log

For Zend_Feed, there is no problem with the Feed itself, so it doesn't throw a Zend_Feed-related Exception.

The problem you have here is another one, like wrong URL : it fails getting the data, and not analysing it ; it explains why the exception is not Zend_Feed-related, but Zend_Http_Client-related.

You might want to add some other exception-handling-code ; something like this :

try { // Test piece straight off the Zend tutorial
    $slashdotRss = Zend_Feed::import('http://rss.slashdot.org/Slashdot/slashdot');
} catch (Zend_Feed_Exception $e) {
    // feed import failed
    echo "Exception caught importing feed: {$e->getMessage()}\n";
    exit;
} catch (Zend_Http_Client_Exception $e) {
  echo "There is something wrong with the URL you provided for the feed";
  exit;
} catch (Exception $e) {
  echo "There is something wrong, we don't know what...";
  exit;
}

This way :

  • If the feed is not valid, you can tell the user
  • If there is an HTTP-related problem, you can tell the user too
  • If there is another problem you didn't think about, it still doesn't crash
Pascal MARTIN
Nono, we have full trace of errors, all of them leading to Zend Feed.php and subsequently to the HTTP Client. It is just very unnerving that a trycatch, that normally should maintain the page in the view is just ignored in such a vicious way. All I want to do is to let it fail silently like intended ("Didn't find any feed, sry"), not to fail to render anything else on the page.Regarding ZF 1.9, that would probably be a good time to upgrade. We're a bit behind - back in 1.5 (feels like 1998, I know), but few functions released in subsequent versions have really been of need for us.
Oh, Damn!For some reason, our running copy did indeed only expect the feed exception. Awesome, it now runs smootly as a cat. Thanks for the aid!
OK :-) well, a generic catch (Exception $e) would probably be enough, then ^^
Pascal MARTIN
(I didn't see your previous comment before sending mine ; you're welcome :-) )
Pascal MARTIN
A: 

Hey, you are not catching the right exception type try to catch

Zend_Http_Client_Adapter_Exception

or all at once:

catch (Exception $e)

btw it has a toString method so you can just echo $e no need to getMessages.

Leon
A: 

There is nice & easy zend_feeds tutoial at : http://smwebprof.blogspot.com

smwebprof