Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add try and catch around the routes we use for fetching data from medium and for event s from air table also put a timeout on fetching from sortable.
  • Loading branch information
aaronmgdr committed Dec 5, 2019
1 parent a0109bf commit ab69f37
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
17 changes: 13 additions & 4 deletions packages/web/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import addToCRM from '../server/addToCRM'
import ecoFundSubmission from '../server/EcoFundApp'
import { RequestType } from '../src/fauceting/FaucetInterfaces'
import nextI18next from '../src/i18n'
import { abort } from '../src/utils/abortableFetch'
import latestAnnouncements from './Announcement'
import { faucetOrInviteController } from './controllers'
import getFormattedEvents from './EventHelpers'
Expand Down Expand Up @@ -154,13 +155,21 @@ function wwwRedirect(req, res, nextAction) {
})

server.get('/proxy/medium', async (_, res) => {
const articlesdata = await getFormattedMediumArticles()
res.json(articlesdata)
try {
const articlesdata = await getFormattedMediumArticles()
res.json(articlesdata)
} catch (e) {
res.status(e.statusCode || 500).json({ message: e.message || 'unknownError' })
}
})

server.get('/proxy/events/', async (_, res) => {
const events = await getFormattedEvents()
res.json(events)
try {
const events = await Promise.race([getFormattedEvents(), abort('Events from Airtable')])
res.json(events)
} catch (e) {
res.status(e.statusCode || 500).json({ message: e.message || 'unknownError' })
}
})

server.get('*', (req, res) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/web/src/utils/abortableFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ export default async function abortableFetch(url: string, options = {}) {
return Promise.race([fetch(url, { ...options }), abort(url)])
}

export async function abort(url: string, milliseconds = 800) {
export async function abort(message: string, milliseconds = 800) {
return new Promise((_, reject) =>
setTimeout(() => {
reject(new Error(`from abortableFetch: Took to Long to Fetch ${url}`))
reject(new Error(`from abortableFetch: Took to Long to Fetch ${message}`))
}, milliseconds)
)
}

0 comments on commit ab69f37

Please sign in to comment.