From 9ea01762a52f37fa40805d5773e163651f08230f Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Sun, 26 Jun 2022 19:55:36 +0100 Subject: [PATCH] Add a check for rate limiting on timeline fetch --- src/services/api/api.service.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/services/api/api.service.js b/src/services/api/api.service.js index b9be5eb0..0820c0f7 100644 --- a/src/services/api/api.service.js +++ b/src/services/api/api.service.js @@ -462,6 +462,16 @@ const deleteList = ({ id, credentials }) => { }) } +const errorStatusMapper = (response) => { + console.log(response) + switch (response.status) { + case 429: + return 'You are rate limited' + default: + return `Unexpected status: ${response.status}` + } +} + const fetchConversation = ({ id, credentials }) => { let urlContext = MASTODON_STATUS_CONTEXT_URL(id) return fetch(urlContext, { headers: authHeaders(credentials) }) @@ -469,7 +479,7 @@ const fetchConversation = ({ id, credentials }) => { if (data.ok) { return data } - throw new Error('Error fetching timeline', data) + throw new Error(['Error fetching timeline', errorStatusMapper(data)]) }) .then((data) => data.json()) .then(({ ancestors, descendants }) => ({ @@ -485,7 +495,7 @@ const fetchStatus = ({ id, credentials }) => { if (data.ok) { return data } - throw new Error('Error fetching timeline', data) + throw new Error(['Error fetching timeline', errorStatusMapper(data)]) }) .then((data) => data.json()) .then((data) => parseStatus(data)) @@ -657,8 +667,15 @@ const fetchTimeline = ({ }) return data }) - .then((data) => data.json()) .then((data) => { + if (data.ok) return data.json() + + throw new Error(errorStatusMapper(data)) + }) + .then((data) => { + if (data.error) { + throw new Error(data.error) + } if (!data.errors) { return { data: data.map(isNotifications ? parseNotification : parseStatus), pagination } } else {