Commit 0fc4db86 authored by webchick's avatar webchick
Browse files

Issue #1875792 by Berdir, rootatwc, rbayliss: Standardize Guzzle exception handling.

parent 6f3e7073
......@@ -12,6 +12,7 @@
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Guzzle\Http\Exception\BadResponseException;
use Guzzle\Http\Exception\RequestException;
/**
* Defines a default fetcher implementation.
......@@ -48,6 +49,12 @@ function fetch(Feed $feed) {
$feed->modified = strtotime($response->getLastModified());
$feed->http_headers = $response->getHeaders();
// Update the feed URL in case of a 301 redirect.
if ($previous_response = $response->getPreviousResponse()) {
if ($previous_response->getStatusCode() == 301 && $location = $response->getPreviousResponse()->getLocation()) {
$feed->url->value = $location;
}
}
return TRUE;
}
catch (BadResponseException $e) {
......@@ -56,5 +63,10 @@ function fetch(Feed $feed) {
drupal_set_message(t('The feed from %site seems to be broken because of error "%error".', array('%site' => $feed->label(), '%error' => $response->getStatusCode() . ' ' . $response->getReasonPhrase())));
return FALSE;
}
catch (RequestException $e) {
watchdog('aggregator', 'The feed from %site seems to be broken due to "%error".', array('%site' => $feed->label(), '%error' => $e->getMessage()), WATCHDOG_WARNING);
drupal_set_message(t('The feed from %site seems to be broken because of error "%error".', array('%site' => $feed->label(), '%error' => $e->getMessage())));
return FALSE;
}
}
}
......@@ -73,4 +73,33 @@ function testHtmlEntitiesSample() {
$this->assertResponse(200, format_string('Feed %name exists.', array('%name' => $feed->label())));
$this->assertRaw("Quote" Amp&");
}
/**
* Tests error handling when an invalid feed is added.
*/
function testRedirectFeed() {
// Simulate a typo in the URL to force a curl exception.
$invalid_url = url('aggregator/redirect', array('absolute' => TRUE));
$feed = entity_create('aggregator_feed', array('url' => $invalid_url));
$feed->save();
aggregator_refresh($feed);
// Make sure that the feed URL was updated correctly.
$this->assertEqual($feed->url->value, url('aggregator/test-feed', array('absolute' => TRUE)));
}
/**
* Tests error handling when an invalid feed is added.
*/
function testInvalidFeed() {
// Simulate a typo in the URL to force a curl exception.
$invalid_url = 'http:/www.drupal.org';
$feed = entity_create('aggregator_feed', array('url' => $invalid_url, 'title' => $this->randomName()));
$feed->save();
// Update the feed. Use the UI to be able to check the message easily.
$this->drupalGet('admin/config/services/aggregator');
$this->clickLink(t('Update items'));
$this->assertRaw(t('The feed from %title seems to be broken because of error "%error"', array('%title' => $feed->label(), '%error' => "[curl] 6: Couldn't resolve host 'http' [url] /")));
}
}
......@@ -11,6 +11,13 @@ function aggregator_test_menu() {
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['aggregator/redirect'] = array(
'title' => 'Test feed with a redirect',
'description' => "A feed that redirects to another one",
'page callback' => 'aggregator_test_redirect',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
......@@ -56,3 +63,10 @@ function aggregator_test_feed($use_last_modified = FALSE, $use_etag = FALSE) {
print $feed;
}
/**
* Page callback that redirects to another feed.
*/
function aggregator_test_redirect() {
drupal_goto('aggregator/test-feed', array(), 301);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment