From 025861f40cd08c2190cc28594b7562ae1d26fe07 Mon Sep 17 00:00:00 2001 From: Sacha Greif Date: Sun, 30 Nov 2014 13:03:35 +0900 Subject: [PATCH] better error handling for incorrect URLs --- .../lib/client/autoform-postthumbnail.js | 7 +++++++ .../lib/server/get_embedly_data.js | 12 ++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/telescope-module-embedly/lib/client/autoform-postthumbnail.js b/packages/telescope-module-embedly/lib/client/autoform-postthumbnail.js index 8cd11b7d4..1342cc547 100644 --- a/packages/telescope-module-embedly/lib/client/autoform-postthumbnail.js +++ b/packages/telescope-module-embedly/lib/client/autoform-postthumbnail.js @@ -32,8 +32,15 @@ Template.afPostThumbnail.rendered = function () { var url = $urlField.val(); if (!!url) { $thumbnailContainer.addClass('loading'); + clearSeenErrors(); console.log('getting embedly data for '+url); Meteor.call('getEmbedlyData', url, function (error, data) { + if (error) { + console.log(error) + throwError(error.reason); + $thumbnailContainer.removeClass('loading'); + return + } if (data) { $img.attr('src', data.thumbnailUrl); $thumbnailUrlField.val(data.thumbnailUrl); diff --git a/packages/telescope-module-embedly/lib/server/get_embedly_data.js b/packages/telescope-module-embedly/lib/server/get_embedly_data.js index 4a0aa63a4..e920bd335 100644 --- a/packages/telescope-module-embedly/lib/server/get_embedly_data.js +++ b/packages/telescope-module-embedly/lib/server/get_embedly_data.js @@ -4,12 +4,12 @@ getEmbedlyData = function (url) { var embedlyKey = getSetting('embedlyKey'); var thumbnailWidth = getSetting('thumbnailWidth', 200); var thumbnailHeight = getSetting('thumbnailHeight', 125); + + if(!embedlyKey) + throw new Meteor.Error("Couldn't find an Embedly API key! Please add it to your Telescope settings.") try { - if(!embedlyKey) - throw new Error("Couldn't find an Embedly API key! Please add it to your Telescope settings.") - var result = Meteor.http.get(extractBase, { params: { key: embedlyKey, @@ -22,12 +22,16 @@ getEmbedlyData = function (url) { // console.log(result) - result.data.thumbnailUrl = result.data.images[0].url; // add thumbnailUrl as its own property + if (!!result.data.images && !!result.data.images.length) // there may not always be an image + result.data.thumbnailUrl = result.data.images[0].url; // add thumbnailUrl as its own property return _.pick(result.data, 'title', 'media', 'description', 'thumbnailUrl'); } catch (error) { console.log(error) + // the first 13 characters of the Embedly errors are "failed [400] ", so remove them and parse the rest + var errorObject = JSON.parse(error.message.substring(13)); + throw new Meteor.Error(errorObject.error_code, errorObject.error_message); return null; } }