Skip to content

Commit

Permalink
refactor: implement code review comments to make the api.request meth…
Browse files Browse the repository at this point in the history
…od more resilient
  • Loading branch information
kabaros committed Dec 12, 2023
1 parent 2b4e162 commit fa8c1ba
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@ class Api {
credentials: 'include',
redirect: 'manual',
}).then(async (res) => {
if (res.type === 'opaqueredirect') {
throw {
message: i18n.t(
'Your session has expired. Please refresh the page and login before trying again.'
),
let errorBody
try {
if (res.type === 'opaqueredirect') {
errorBody = {
message: i18n.t(
'Your session has expired. Please refresh the page and login before trying again.'
),
}
} else if (res.status < 200 || res.status >= 300) {
errorBody = await res.json()
}
} catch (err) {
throw new Error(i18n.t('An unexpected error occurred'))
}
if (res.status < 200 || res.status >= 300) {
const errorBody = await res.json()

if (errorBody) {
throw errorBody
}
return res
Expand Down

0 comments on commit fa8c1ba

Please sign in to comment.