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
main
Brian Dennis 7 years ago committed by Michael Fix
parent 3a174b988c
commit db73c56eca

@ -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()
}

@ -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,

@ -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",

@ -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"

Loading…
Cancel
Save