From 9e13e5d164d3e3b8defc7f0dfd6d55f74b525054 Mon Sep 17 00:00:00 2001 From: floatingghost Date: Sun, 26 Jun 2022 19:02:49 +0000 Subject: [PATCH] Add cleaner message for rate-limited users (#19) Reviewed-on: https://akkoma.dev/AkkomaGang/pleroma-fe/pulls/19 --- src/services/api/api.service.js | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/services/api/api.service.js b/src/services/api/api.service.js index b9be5eb0..c942535e 100644 --- a/src/services/api/api.service.js +++ b/src/services/api/api.service.js @@ -462,6 +462,20 @@ const deleteList = ({ id, credentials }) => { }) } +const errorStatusMapper = (response) => { + console.log(response) + switch (response.status) { + case 429: + return 'You are rate limited' + case 500: + return 'Internal Server Error' + case 503: + return 'The backend is not available' + 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 +483,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 +499,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 +671,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 {