Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parse error message properly for manual installs #541

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we need this: If we're explicitly using res.json(), shouldn't we also explicity set the Content-Type header to application/json?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we need this:

you mean the redirect: manual - we need that, because the API is not returning 401, it just redirects to login.action, and if you don't specify manual then it returns 200 (from the login.action) - I added a link for the three possible modes for fetch (the other option could be to throw an error on redirect, but we can't keep the default)

it defaults to that application/json

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant the sentence following the colon. But if this defaults to application/json, then we're fine (although I don't see where the default is applied?)

}).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
Loading