Expose all picoFeed errors to the Frontend when adding a subscription

This commit is contained in:
Mathias Kresin 2015-04-10 23:34:07 +02:00
parent 84ac8fc316
commit 1afb73b82d
13 changed files with 163 additions and 55 deletions

View File

@ -159,17 +159,50 @@ Router\action('subscribe', function() {
} }
$values += array('url' => trim($url), 'download_content' => 0, 'rtl' => 0, 'cloak_referrer' => 0); $values += array('url' => trim($url), 'download_content' => 0, 'rtl' => 0, 'cloak_referrer' => 0);
$feed_id = Model\Feed\create($values['url'], $values['download_content'], $values['rtl'], $values['cloak_referrer']);
if ($feed_id > 0) { try {
$feed_id = Model\Feed\create($values['url'], $values['download_content'], $values['rtl'], $values['cloak_referrer']);
}
catch (UnexpectedValueException $e) {
$error_message = t('This subscription already exists.');
}
catch (PicoFeed\Client\InvalidCertificateException $e) {
$error_message = t('Invalid SSL certificate.');
}
catch (PicoFeed\Client\InvalidUrlException $e) {
// picoFeed uses this exception for multiple reasons, but doesn't
// provide an exception code to distinguish what exactly happend here
$error_message = $e->getMessage();
}
catch (PicoFeed\Client\MaxRedirectException $e) {
$error_message = t('Maximum number of HTTP redirections exceeded.');
}
catch (PicoFeed\Client\MaxSizeException $e) {
$error_message = t('The content size exceeds to maximum allowed size.');
}
catch (PicoFeed\Client\TimeoutException $e) {
$error_message = t('Connection timeout.');
}
catch (PicoFeed\Parser\MalformedXmlException $e) {
$error_message = t('Feed is malformed.');
}
catch (PicoFeed\Reader\SubscriptionNotFoundException $e) {
$error_message = t('Unable to find a subscription.');
}
catch (PicoFeed\Reader\UnsupportedFeedFormatException $e) {
$error_message = t('Unable to detect the feed format.');
}
if (isset($feed_id) && $feed_id !== false) {
Session\flash(t('Subscription added successfully.')); Session\flash(t('Subscription added successfully.'));
Response\redirect('?action=feed-items&feed_id='.$feed_id); Response\redirect('?action=feed-items&feed_id='.$feed_id);
} }
else if ($feed_id === -2) {
Session\flash_error(t('This subscription already exists.'));
}
else { else {
Session\flash_error(t('Unable to find a subscription.')); if (! isset($error_message)) {
$error_message = t('Error occured.');
}
Session\flash_error($error_message);
} }
Response\html(Template\layout('add', array( Response\html(Template\layout('add', array(

View File

@ -3,6 +3,7 @@
require __DIR__.'/common.php'; require __DIR__.'/common.php';
use JsonRPC\Server; use JsonRPC\Server;
use PicoFeed\PicoFeedException;
$server = new Server; $server = new Server;
$server->authentication(array( $server->authentication(array(
@ -24,7 +25,16 @@ $server->register('feed.info', function ($feed_id) {
// Add a new feed // Add a new feed
$server->register('feed.create', function($url) { $server->register('feed.create', function($url) {
$result = Model\Feed\create($url); try {
$result = Model\Feed\create($url);
}
catch (PicoFeedException $e) {
$result = false;
}
catch (UnexpectedValueException $e) {
$result = false;
}
Model\Config\write_debug(); Model\Config\write_debug();
return $result; return $result;

View File

@ -231,4 +231,11 @@ return array(
'Original link marks article as read' => 'تحويل العنوان إلى مقروء بمجرد النقر على الرابط الأصلي للعنوان', 'Original link marks article as read' => 'تحويل العنوان إلى مقروء بمجرد النقر على الرابط الأصلي للعنوان',
'Cloak the image referrer' => 'Cloak the image referrer', 'Cloak the image referrer' => 'Cloak the image referrer',
// 'This subscription already exists.' => '', // 'This subscription already exists.' => '',
// 'Connection timeout.' => '',
// 'Error occured.' => '',
// 'Feed is malformed.' => '',
// 'Invalid SSL certificate.' => '',
// 'Maximum number of HTTP redirections exceeded.' => '',
// 'The content size exceeds to maximum allowed size.' => '',
// 'Unable to detect the feed format.' => '',
); );

View File

@ -231,4 +231,11 @@ return array(
'Original link marks article as read' => 'Původní odkaz označí článek za přečtený', 'Original link marks article as read' => 'Původní odkaz označí článek za přečtený',
'Cloak the image referrer' => 'Zamaskovat původce obrázků', 'Cloak the image referrer' => 'Zamaskovat původce obrázků',
// 'This subscription already exists.' => '', // 'This subscription already exists.' => '',
// 'Connection timeout.' => '',
// 'Error occured.' => '',
// 'Feed is malformed.' => '',
// 'Invalid SSL certificate.' => '',
// 'Maximum number of HTTP redirections exceeded.' => '',
// 'The content size exceeds to maximum allowed size.' => '',
// 'Unable to detect the feed format.' => '',
); );

View File

@ -231,4 +231,11 @@ return array(
// 'Original link marks article as read' => '', // 'Original link marks article as read' => '',
// 'Cloak the image referrer' => '', // 'Cloak the image referrer' => '',
// 'This subscription already exists.' => '', // 'This subscription already exists.' => '',
// 'Connection timeout.' => '',
// 'Error occured.' => '',
// 'Feed is malformed.' => '',
// 'Invalid SSL certificate.' => '',
// 'Maximum number of HTTP redirections exceeded.' => '',
// 'The content size exceeds to maximum allowed size.' => '',
// 'Unable to detect the feed format.' => '',
); );

View File

@ -231,4 +231,11 @@ return array(
'Original link marks article as read' => 'Link original marca artículo cómo leído', 'Original link marks article as read' => 'Link original marca artículo cómo leído',
// 'Cloak the image referrer' => '', // 'Cloak the image referrer' => '',
// 'This subscription already exists.' => '', // 'This subscription already exists.' => '',
// 'Connection timeout.' => '',
// 'Error occured.' => '',
// 'Feed is malformed.' => '',
// 'Invalid SSL certificate.' => '',
// 'Maximum number of HTTP redirections exceeded.' => '',
// 'The content size exceeds to maximum allowed size.' => '',
// 'Unable to detect the feed format.' => '',
); );

View File

@ -231,4 +231,11 @@ return array(
'Original link marks article as read' => 'Marquer les articles comme lu lors d\'un clic sur le lien original', 'Original link marks article as read' => 'Marquer les articles comme lu lors d\'un clic sur le lien original',
'Cloak the image referrer' => 'Falsifier le référent pour les images', 'Cloak the image referrer' => 'Falsifier le référent pour les images',
'This subscription already exists.' => 'Cet abonnement existe déjà.', 'This subscription already exists.' => 'Cet abonnement existe déjà.',
'Connection timeout.' => 'Connection timeout.',
'Error occured.' => 'Error occured.',
'Feed is malformed.' => 'Feed is malformed.',
'Invalid SSL certificate.' => 'Invalid SSL certificate.',
'Maximum number of HTTP redirections exceeded.' => 'Maximum number of HTTP redirections exceeded.',
'The content size exceeds to maximum allowed size.' => 'The content size exceeds to maximum allowed size.',
'Unable to detect the feed format.' => 'Unable to detect the feed format.',
); );

View File

@ -231,4 +231,11 @@ return array(
// 'Original link marks article as read' => '', // 'Original link marks article as read' => '',
// 'Cloak the image referrer' => '', // 'Cloak the image referrer' => '',
// 'This subscription already exists.' => '', // 'This subscription already exists.' => '',
// 'Connection timeout.' => '',
// 'Error occured.' => '',
// 'Feed is malformed.' => '',
// 'Invalid SSL certificate.' => '',
// 'Maximum number of HTTP redirections exceeded.' => '',
// 'The content size exceeds to maximum allowed size.' => '',
// 'Unable to detect the feed format.' => '',
); );

View File

@ -231,4 +231,11 @@ return array(
// 'Original link marks article as read' => '', // 'Original link marks article as read' => '',
// 'Cloak the image referrer' => '', // 'Cloak the image referrer' => '',
// 'This subscription already exists.' => '', // 'This subscription already exists.' => '',
// 'Connection timeout.' => '',
// 'Error occured.' => '',
// 'Feed is malformed.' => '',
// 'Invalid SSL certificate.' => '',
// 'Maximum number of HTTP redirections exceeded.' => '',
// 'The content size exceeds to maximum allowed size.' => '',
// 'Unable to detect the feed format.' => '',
); );

View File

@ -231,4 +231,11 @@ return array(
'Original link marks article as read' => 'Клик на оригиналну везу обележава чланак прочитаним', 'Original link marks article as read' => 'Клик на оригиналну везу обележава чланак прочитаним',
'Cloak the image referrer' => 'Прикривај рефератора слика', 'Cloak the image referrer' => 'Прикривај рефератора слика',
// 'This subscription already exists.' => '', // 'This subscription already exists.' => '',
// 'Connection timeout.' => '',
// 'Error occured.' => '',
// 'Feed is malformed.' => '',
// 'Invalid SSL certificate.' => '',
// 'Maximum number of HTTP redirections exceeded.' => '',
// 'The content size exceeds to maximum allowed size.' => '',
// 'Unable to detect the feed format.' => '',
); );

View File

@ -231,4 +231,11 @@ return array(
'Original link marks article as read' => 'Klik na originalnu vezu obeležava članak pročitanim', 'Original link marks article as read' => 'Klik na originalnu vezu obeležava članak pročitanim',
'Cloak the image referrer' => 'Prikrivaj referatora slika', 'Cloak the image referrer' => 'Prikrivaj referatora slika',
// 'This subscription already exists.' => '', // 'This subscription already exists.' => '',
// 'Connection timeout.' => '',
// 'Error occured.' => '',
// 'Feed is malformed.' => '',
// 'Invalid SSL certificate.' => '',
// 'Maximum number of HTTP redirections exceeded.' => '',
// 'The content size exceeds to maximum allowed size.' => '',
// 'Unable to detect the feed format.' => '',
); );

View File

@ -231,4 +231,11 @@ return array(
// 'Original link marks article as read' => '', // 'Original link marks article as read' => '',
// 'Cloak the image referrer' => '', // 'Cloak the image referrer' => '',
// 'This subscription already exists.' => '', // 'This subscription already exists.' => '',
// 'Connection timeout.' => '',
// 'Error occured.' => '',
// 'Feed is malformed.' => '',
// 'Invalid SSL certificate.' => '',
// 'Maximum number of HTTP redirections exceeded.' => '',
// 'The content size exceeds to maximum allowed size.' => '',
// 'Unable to detect the feed format.' => '',
); );

View File

@ -151,60 +151,55 @@ function import_opml($content)
// Add a new feed from an URL // Add a new feed from an URL
function create($url, $enable_grabber = false, $force_rtl = false, $cloak_referrer = false) function create($url, $enable_grabber = false, $force_rtl = false, $cloak_referrer = false)
{ {
try { $feed_id = false;
$db = Database::get('db');
// Discover the feed $db = Database::get('db');
$reader = new Reader(Config\get_reader_config());
$resource = $reader->discover($url);
// Feed already there // Discover the feed
if ($db->table('feeds')->eq('feed_url', $resource->getUrl())->count()) { $reader = new Reader(Config\get_reader_config());
return -2; $resource = $reader->discover($url);
}
// Parse the feed // Feed already there
$parser = $reader->getParser( if ($db->table('feeds')->eq('feed_url', $resource->getUrl())->count()) {
$resource->getUrl(), throw new \UnexpectedValueException;
$resource->getContent(), }
$resource->getEncoding()
); // Parse the feed
$parser = $reader->getParser(
if ($enable_grabber) { $resource->getUrl(),
$parser->enableContentGrabber(); $resource->getContent(),
} $resource->getEncoding()
);
$feed = $parser->execute();
if ($enable_grabber) {
// Save the feed $parser->enableContentGrabber();
$result = $db->table('feeds')->save(array( }
'title' => $feed->getTitle(),
'site_url' => $feed->getSiteUrl(), $feed = $parser->execute();
'feed_url' => $feed->getFeedUrl(),
'download_content' => $enable_grabber ? 1 : 0, // Save the feed
'rtl' => $force_rtl ? 1 : 0, $result = $db->table('feeds')->save(array(
'last_modified' => $resource->getLastModified(), 'title' => $feed->getTitle(),
'last_checked' => time(), 'site_url' => $feed->getSiteUrl(),
'etag' => $resource->getEtag(), 'feed_url' => $feed->getFeedUrl(),
'cloak_referrer' => $cloak_referrer ? 1 : 0, 'download_content' => $enable_grabber ? 1 : 0,
)); 'rtl' => $force_rtl ? 1 : 0,
'last_modified' => $resource->getLastModified(),
if ($result) { 'last_checked' => time(),
'etag' => $resource->getEtag(),
$feed_id = $db->getConnection()->getLastId(); 'cloak_referrer' => $cloak_referrer ? 1 : 0,
));
Item\update_all($feed_id, $feed->getItems());
fetch_favicon($feed_id, $feed->getSiteUrl(), $feed->getIcon()); if ($result) {
$feed_id = $db->getConnection()->getLastId();
Config\write_debug();
Item\update_all($feed_id, $feed->getItems());
return (int) $feed_id; fetch_favicon($feed_id, $feed->getSiteUrl(), $feed->getIcon());
}
} }
catch (PicoFeedException $e) {}
Config\write_debug(); Config\write_debug();
return -1;
return $feed_id;
} }
// Refresh all feeds // Refresh all feeds