Skip to content
This repository has been archived by the owner on Jan 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #788 from LiskHQ/787-newsfeed-crash
Browse files Browse the repository at this point in the history
Add safety check during Twitter response URL parse
  • Loading branch information
MichalTuleja authored Aug 10, 2018
2 parents 76413a7 + 832287f commit 9a74688
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions services/newsfeed/TwitterService.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,22 @@ const client = new Twitter({
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
});

const safeRef = (obj, path) => {
try {
// eslint-disable-next-line
return path.split('.').reduce((xs, x) => (xs && xs[x]) ? xs[x] : null, obj);
} catch (e) {
return null;
}
};

const tweetUrl = (o) => {
if (o.retweeted_status) {
return o.retweeted_status.entities.urls[0].url;
return safeRef(o, 'retweeted_status.entities.urls.0.url');
} else if (o.extended_entities) {
return o.extended_entities.media[0].url;
return safeRef(o, 'extended_entities.media.0.url');
} else if (o.entities) {
return o.entities.urls[0].url;
return safeRef(o, 'entities.urls.0.url');
}
return null;
};
Expand Down

0 comments on commit 9a74688

Please sign in to comment.