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

Conversation

kabaros
Copy link
Contributor

@kabaros kabaros commented Dec 11, 2023

fixes DHIS2-13883 and DHIS2-15304.

  1. With fetch, API errors are not thrown as errors, and we need to do res.json() to get their content. This fixes error handling there so that the error details are surfaced to the consumers.

image

  1. It was a bit trickier with unauthenticated requests, fetch - by default - follows the redirect implicitly so had to change its mode to manual then the response we get back doesn't have a 302 but a property we can check - more info here

image

@kabaros kabaros requested review from a team as code owners December 11, 2023 21:31
@dhis2-bot
Copy link
Contributor

dhis2-bot commented Dec 11, 2023

🚀 Deployed on https://pr-541--dhis2-app-management.netlify.app

@dhis2-bot dhis2-bot temporarily deployed to netlify December 11, 2023 21:33 Inactive
@kabaros kabaros marked this pull request as draft December 12, 2023 06:58
@kabaros kabaros force-pushed the DHIS2-13883/fix-manual-install-error-handling branch from 581cc18 to f40f287 Compare December 12, 2023 07:17
@dhis2-bot dhis2-bot temporarily deployed to netlify December 12, 2023 07:18 Inactive
@dhis2-bot dhis2-bot temporarily deployed to netlify December 12, 2023 09:39 Inactive
@kabaros kabaros marked this pull request as ready for review December 12, 2023 09:50
@Mohammer5 Mohammer5 requested review from stian-sandvold and tomzemp and removed request for stian-sandvold and tomzemp December 12, 2023 10:17
@@ -10,9 +11,18 @@ class Api {
method,
body,
credentials: 'include',
}).then((res) => {
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?)

src/api.js Outdated
if (res.status < 200 || res.status >= 300) {
throw new Error(res.statusText)
const errorBody = await res.json()

Choose a reason for hiding this comment

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

Is it guaranteed that we'll get a json response for non-200 responses? I'd expect 404 to be text/plain.
In that case this threw an error, the error message would be about not being able to parse json rather than the actual issue.

Copy link
Contributor Author

@kabaros kabaros Dec 12, 2023

Choose a reason for hiding this comment

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

so if we don't get JSON, then we're just back to where we started, this will throw an error that's handled by the consumer and then we just display an empty error message like before.

I am not happy with the solution in general as well, but this looked like the least intrusive one without doing a big refactor

@dhis2-bot dhis2-bot temporarily deployed to netlify December 12, 2023 15:31 Inactive
@tomzemp tomzemp self-requested a review December 12, 2023 20:48
Copy link
Member

@tomzemp tomzemp left a comment

Choose a reason for hiding this comment

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

Looks good 🚀 (I did the QA review)

When testing against my local instance, I didn't actually get this modified error when non-authenticated (I got Failed to install app: Failed to fetch), but I did get the Your session has expired error... when testing against debug.dhis2.org/dev

@kabaros kabaros merged commit ea2c62f into master Dec 13, 2023
7 checks passed
@kabaros kabaros deleted the DHIS2-13883/fix-manual-install-error-handling branch December 13, 2023 18:17
dhis2-bot added a commit that referenced this pull request Dec 13, 2023
## [100.2.30](v100.2.29...v100.2.30) (2023-12-13)

### Bug Fixes

* parse error message properly for manual installs ([#541](#541)) ([ea2c62f](ea2c62f))
@dhis2-bot
Copy link
Contributor

🎉 This PR is included in version 100.2.30 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants