Skip to content

Commit

Permalink
fix: parse error message properly for manual installs (#541)
Browse files Browse the repository at this point in the history
* fix: parse error message properly for manual installs

* fix(DHIS2-15304): display correct error message when session is expired

* refactor: implement code review comments to make the api.request method more resilient
  • Loading branch information
kabaros authored Dec 13, 2023
1 parent a00f1e8 commit ea2c62f
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useConfig } from '@dhis2/app-runtime'
import i18n from '@dhis2/d2-i18n'

class Api {
constructor({ baseUrl }) {
Expand All @@ -10,9 +11,25 @@ class Api {
method,
body,
credentials: 'include',
}).then((res) => {
if (res.status < 200 || res.status >= 300) {
throw new Error(res.statusText)
redirect: 'manual',
}).then(async (res) => {
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 (errorBody) {
throw errorBody
}
return res
})
Expand Down

0 comments on commit ea2c62f

Please sign in to comment.