This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Show errors 500 and 400 on preview #639
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
503a121
feat(river): use HAPI FHIR $validate in preview
simonvadee bfd46e7
clean(settings): remove PYROG_API_URL
simonvadee a24267f
clean(docker): use env variables in docker-compose
simonvadee 1fc63ba
fix(preview): update preview serializer
simonvadee ef15842
feat(app): use OperationOutcomeIssue in preview errors
simonvadee e18286c
deps: bump "requests" package to 2.26.0
simonvadee 6a312e5
deps: add test dependency to "responses"
simonvadee 480026d
fix(fhir_api): validate() does not raise on non-2XX
simonvadee 81db5ef
tests(adapters): add tests for fhir_api adapter
simonvadee 084980e
test(preview): test preview with validation errors
simonvadee ca4a42a
refactor(tests): clean fhir_api adapter tests
simonvadee 50d0092
fix(tests): scope of api_client is function
simonvadee 8e4c5b7
fix(fhir_api): differentiate _get and _post
simonvadee 703bd38
fix(tests): scope api_client for "module"
simonvadee 0d0ba75
chore(tests): add fhir_api marker
simonvadee 29cd41b
fix(tests): remove unsued snapshots
simonvadee 5635678
fix[app]: handle api errors in preview with notistack and not only al…
102de13
refactor(app): creation of an handling error function
06b813e
fix(app): add handling of server errors
7a29917
Merge branch 'v4' into mc/app/showErrorsOnPreview
Klochette 43dd5d6
fix(app): merging v4 in branch
84c6f14
fix(app): corrections from reviews and add functions for different er…
b88472e
fix(app): fix error display on validation
1051621
fix(app): river schema reset
77ee7b0
fix(app): resolve reviews, change api generated to what's in v4
a6ae5ae
fix(app): delete cast for color
15bb1b7
fix(app): reset to v4
3483a36
fix(app): reset ambr
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import React, { useState, useEffect, useCallback } from "react"; | ||
|
||
import { IResource } from "@ahryman40k/ts-fhir-types/lib/R4"; | ||
import { Icon } from "@blueprintjs/core"; | ||
|
@@ -17,7 +17,9 @@ import { | |
useMediaQuery, | ||
} from "@material-ui/core"; | ||
import Alert, { Color } from "@material-ui/lab/Alert"; | ||
import { FetchBaseQueryError } from "@reduxjs/toolkit/dist/query"; | ||
import clsx from "clsx"; | ||
import { useSnackbar } from "notistack"; | ||
import { useTranslation } from "react-i18next"; | ||
import ReactJson from "react-json-view"; | ||
import { useParams } from "react-router-dom"; | ||
|
@@ -29,6 +31,10 @@ import { | |
useApiOwnersRetrieveQuery, | ||
useApiPreviewCreateMutation, | ||
} from "services/api/endpoints"; | ||
import { | ||
apiValidationErrorFromResponse, | ||
ApiValidationError, | ||
} from "services/api/errors"; | ||
import { | ||
ExplorationResponse, | ||
OperationOutcomeIssue, | ||
|
@@ -136,6 +142,7 @@ const getAlertSeverityFromOperationOutcomeIssue = ( | |
const Preview = (): JSX.Element => { | ||
const classes = useStyles(); | ||
const { t } = useTranslation(); | ||
const { enqueueSnackbar } = useSnackbar(); | ||
const prefersDarkMode = useMediaQuery("(prefers-color-scheme: dark)"); | ||
const { mappingId } = useParams<{ | ||
mappingId?: string; | ||
|
@@ -144,20 +151,19 @@ const Preview = (): JSX.Element => { | |
const [exploration, setExploration] = useState< | ||
ExplorationResponse | null | undefined | ||
>(undefined); | ||
|
||
const [alerts, setAlerts] = useState< | ||
AlertOperationOutcomeIssue[] | undefined | ||
>(undefined); | ||
|
||
const [preview, setPreview] = useState<IResource | undefined>(undefined); | ||
|
||
const handleAlertClose = (index: number) => { | ||
if (alerts) { | ||
const newAlerts = [...alerts]; | ||
newAlerts.splice(index, 1); | ||
setAlerts(newAlerts); | ||
} | ||
}; | ||
|
||
const [preview, setPreview] = useState<IResource | undefined>(undefined); | ||
|
||
const { data: mapping } = useApiResourcesRetrieveQuery( | ||
{ id: mappingId ?? "" }, | ||
{ skip: !mappingId } | ||
|
@@ -172,39 +178,71 @@ const Preview = (): JSX.Element => { | |
|
||
const [apiExploreCreate] = useApiExploreCreateMutation(); | ||
|
||
useEffect(() => { | ||
if (mappingId && owner && exploration === undefined) { | ||
setExploration(null); | ||
const handleValidationError = useCallback( | ||
({ | ||
errors, | ||
errorStatus, | ||
errorField, | ||
}: { | ||
errors?: ApiValidationError<unknown>; | ||
errorStatus: number | "FETCH_ERROR" | "PARSING_ERROR" | "CUSTOM_ERROR"; | ||
errorField: string; | ||
}) => { | ||
if (errors) { | ||
const errorEntries = Object.entries(errors); | ||
errorEntries.forEach(([key, text]) => { | ||
for (const error in text) { | ||
enqueueSnackbar( | ||
t<string>("catchValidationErrorPrompt", { | ||
query: errorField, | ||
errorStatus: errorStatus, | ||
errorKey: key, | ||
errorText: text[error], | ||
}), | ||
{ | ||
variant: "error", | ||
} | ||
); | ||
} | ||
}); | ||
} | ||
}, | ||
[enqueueSnackbar, t] | ||
); | ||
|
||
const explore = async () => { | ||
try { | ||
const exploration = await apiExploreCreate({ | ||
explorationRequestRequest: { | ||
owner: owner?.name ?? "", | ||
table: mapping?.primary_key_table ?? "", | ||
resource_id: mappingId, | ||
}, | ||
}).unwrap(); | ||
setExploration(exploration); | ||
} catch (e) { | ||
setAlerts([ | ||
{ | ||
severity: "error", | ||
diagnostics: e.error, | ||
code: "internal", | ||
}, | ||
]); | ||
} | ||
}; | ||
explore(); | ||
} | ||
}, [ | ||
apiExploreCreate, | ||
exploration, | ||
mapping?.primary_key_table, | ||
mappingId, | ||
owner, | ||
]); | ||
const handleError = useCallback( | ||
(error: FetchBaseQueryError) => { | ||
const validationError = apiValidationErrorFromResponse(error); | ||
switch (error.status) { | ||
case "PARSING_ERROR": | ||
enqueueSnackbar( | ||
t<string>("catchValidationErrorPrompt", { | ||
query: error.status, | ||
errorStatus: error.originalStatus.toString(), | ||
errorText: error.error, | ||
}), | ||
{ variant: "error" } | ||
); | ||
break; | ||
case "FETCH_ERROR": | ||
case "CUSTOM_ERROR": | ||
error.data && | ||
enqueueSnackbar(`${error.status} : ${error.data as string}`, { | ||
variant: "error", | ||
}); | ||
break; | ||
default: | ||
validationError && | ||
handleValidationError({ | ||
errors: validationError, | ||
errorStatus: error.status, | ||
errorField: "Preview", | ||
}); | ||
break; | ||
} | ||
}, | ||
[enqueueSnackbar, handleValidationError, t] | ||
); | ||
|
||
const [apiPreviewCreate] = useApiPreviewCreateMutation(); | ||
|
||
|
@@ -230,21 +268,46 @@ const Preview = (): JSX.Element => { | |
severity: getAlertSeverityFromOperationOutcomeIssue(error), | ||
})) | ||
); | ||
} catch (e) { | ||
setAlerts([ | ||
{ | ||
severity: "error", | ||
diagnostics: e.error, | ||
code: "internal", | ||
}, | ||
]); | ||
} catch (error) { | ||
handleError(error as FetchBaseQueryError); | ||
} | ||
}; | ||
previewCreate(); | ||
} | ||
} | ||
}; | ||
|
||
// load the data for the table preview list | ||
useEffect(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a small comment explaining what this useEffect does? |
||
if (mappingId && owner && exploration === undefined) { | ||
setExploration(null); | ||
|
||
const explore = async () => { | ||
try { | ||
const _exploration = await apiExploreCreate({ | ||
explorationRequestRequest: { | ||
owner: owner?.name ?? "", | ||
table: mapping?.primary_key_table ?? "", | ||
resource_id: mappingId, | ||
}, | ||
}).unwrap(); | ||
setExploration(_exploration); | ||
} catch (error) { | ||
handleError(error as FetchBaseQueryError); | ||
} | ||
}; | ||
explore(); | ||
} | ||
}, [ | ||
apiExploreCreate, | ||
exploration, | ||
mapping?.primary_key_table, | ||
mappingId, | ||
owner, | ||
handleError, | ||
enqueueSnackbar, | ||
]); | ||
|
||
return ( | ||
<Container className={classes.container} maxWidth="xl"> | ||
<BackButton className={classes.button} /> | ||
|
@@ -318,8 +381,8 @@ const Preview = (): JSX.Element => { | |
) : ( | ||
<div className={classes.texts}> | ||
<Typography> | ||
{t("clickOnAFireIcon")}{" "} | ||
<Icon icon={IconNames.FLAME} className={classes.iconFlame} />{" "} | ||
{t("clickOnAFireIcon")} | ||
<Icon icon={IconNames.FLAME} className={classes.iconFlame} /> | ||
{t("inOrderToPreview")} | ||
</Typography> | ||
</div> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Casting the error variable here doesn't raise any eslint error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it doesn't
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nvm I thought we were in a catch statement
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nvm I thought we were in a catch statement