From db73c56eca625ce9647ae11826146717b9fd57da Mon Sep 17 00:00:00 2001 From: Brian Dennis Date: Tue, 19 Sep 2017 21:57:18 -0500 Subject: [PATCH] Add notification of Twitter rate limiting (#13) * add rate limiting detection to handler * alert user on rate limit error * Update api.js * Add morphmorph dep --- handlers/twitter.js | 15 +++++++++++++-- lib/api.js | 11 +++++++++++ package.json | 1 + yarn.lock | 4 ++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/handlers/twitter.js b/handlers/twitter.js index 807be1e..0d15e0b 100644 --- a/handlers/twitter.js +++ b/handlers/twitter.js @@ -1,4 +1,7 @@ const Twitter = require('twitter') +const morph = require('morphmorph') + +const RATE_LIMIT_CODE = 420 const client = new Twitter({ consumer_key: process.env.TWITTER_CONSUMER_KEY, @@ -9,11 +12,19 @@ const client = new Twitter({ const uploadImage = (data) => client.post('media/upload', { media_data: data }) const uploadTweet = (media) => client.post('statuses/update', { status: 'Carbon Copy', media_ids: media.media_id_string }) -const extractImageUrl = (response) => response.entities.media[0].display_url +const extractImageUrl = morph.get('entities.media.0.display_url') +const extractErrorCode = morph.get('0.code') const respondSuccess = (res, url) => res.json({ url }) const respondFail = (res, err) => { - console.error(`Error: ${err.message || err}`) + const errorCode = extractErrorCode(err) + + // check for rate limit + if (errorCode === RATE_LIMIT_CODE) { + return res.status(420).send() + } + + console.error(`Error: ${errorCode || err.message}`) res.status(500).send() } diff --git a/lib/api.js b/lib/api.js index c4d1f89..d98ca3c 100644 --- a/lib/api.js +++ b/lib/api.js @@ -1,6 +1,7 @@ import axios from 'axios' const DOMAIN = process.browser ? document.location.origin : '' +const RATE_LIMIT_CODE = 420 const gistClient = axios.create({ baseURL: 'https://api.github.com', @@ -19,6 +20,7 @@ async function tweet (encodedImage) { .then(url => encodeURIComponent(`Built with #Carbon, by @dawn_labs ${url}`)) .then(uri => `https://twitter.com/intent/tweet?text=${uri}`) .then(openTwitterUrl) + .catch(checkIfRateLimited) } const getGist = (id) => { @@ -40,6 +42,15 @@ function openTwitterUrl (twitterUrl) { window.open(twitterUrl, 'twitter', opts) } +function checkIfRateLimited (err) { + if (err.response.status === RATE_LIMIT_CODE) { + alert('Oh no! Looks like to many people are trying to tweet right now and we\'ve been rate limited. Try again soon or save and upload manually!') + return + } + + throw err +} + export default { getGist, diff --git a/package.json b/package.json index 181308d..88041a2 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "form-data": "^2.2.0", "highlight.js": "^9.12.0", "morgan": "^1.8.2", + "morphmorph": "0.0.2", "next": "^3.2.2", "react": "^15.6.1", "react-click-outside": "^2.3.1", diff --git a/yarn.lock b/yarn.lock index cbde3fb..1543eeb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2407,6 +2407,10 @@ morgan@^1.8.2: on-finished "~2.3.0" on-headers "~1.0.1" +morphmorph@0.0.2: + version "0.0.2" + resolved "https://registry.yarnpkg.com/morphmorph/-/morphmorph-0.0.2.tgz#b2401b5e976ca4f4e068b730145aa64634aaa689" + ms@2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"