From 97926cb568e2464fd01b79cbd68667228d2ff9b9 Mon Sep 17 00:00:00 2001
From: Bjarke Madsen <bjarke@nordu.net>
Date: Thu, 3 Aug 2023 12:04:24 +0200
Subject: [PATCH] Add a bit more robust error handling
---
compendium_v2/routes/survey.py | 12 ++--
.../src/SurveyContainerComponent.tsx | 60 ++++++++++++-------
2 files changed, 43 insertions(+), 29 deletions(-)
diff --git a/compendium_v2/routes/survey.py b/compendium_v2/routes/survey.py
index 898a6b9e..a9b26d34 100644
--- a/compendium_v2/routes/survey.py
+++ b/compendium_v2/routes/survey.py
@@ -257,7 +257,7 @@ def try_survey(year) -> Any:
"""
survey = db.session.scalar(select(Survey).where(Survey.year == year))
if not survey:
- return "Survey not found", 404
+ return jsonify({'success': False, 'message': 'Survey not found'}), 404
return jsonify({
"model": survey.survey,
@@ -283,7 +283,7 @@ def inspect_survey(year) -> Any:
"""
survey = db.session.scalar(select(Survey).where(Survey.year == year))
if not survey:
- return "Survey not found", 404
+ return jsonify({'success': False, 'message': 'Survey not found'}), 404
def visible_visitor(object, items):
for key, value in items:
@@ -323,11 +323,11 @@ def load_survey(year, nren_name) -> Any:
"""
nren = db.session.scalar(select(NREN).filter(NREN.name == nren_name))
if not nren:
- return "NREN not found", 404
+ return jsonify({'success': False, 'message': 'Survey not found'}), 404
survey = db.session.scalar(select(Survey).where(Survey.year == year))
if not survey:
- return "Survey not found", 404
+ return jsonify({'success': False, 'message': 'Survey not found'}), 404
if not check_access_nren(current_user, nren):
return jsonify({'success': False, 'message': 'You do not have permissions to access this survey.'}), 403
@@ -373,11 +373,11 @@ def save_survey(year, nren_name) -> Any:
nren = db.session.scalar(select(NREN).filter(NREN.name == nren_name))
if nren is None:
- return "NREN not found", 404
+ return jsonify({'success': False, 'message': 'Survey not found'}), 404
survey = db.session.scalar(select(Survey).where(Survey.year == year))
if survey is None:
- return "Survey not found", 404
+ return jsonify({'success': False, 'message': 'Survey not found'}), 404
if not check_access_nren(current_user, nren):
return jsonify({'success': False, 'message': 'You do not have permission to edit this survey.'}), 403
diff --git a/survey-frontend/src/SurveyContainerComponent.tsx b/survey-frontend/src/SurveyContainerComponent.tsx
index c419e971..ccebe300 100644
--- a/survey-frontend/src/SurveyContainerComponent.tsx
+++ b/survey-frontend/src/SurveyContainerComponent.tsx
@@ -16,39 +16,53 @@ function SurveyContainerComponent({ loadFrom, saveTo = '', readonly = false }) {
const [surveyModel, setSurveyModel] = useState<Model>();
const verificationStatus = useRef<Map<string, VerificationStatus>>(new Map());
const { year, nren } = useParams();
+ const [error, setError] = useState<string>('loading survey...');
useEffect(() => {
- getModel();
- }, []);
+ async function getModel() {
+ const response = await fetch(loadFrom + year + (nren ? '/' + nren : '')) // year is always set, nren stays empty for inspect and try
+ const json = await response.json();
- if (surveyModel === undefined) {
- return 'loading survey...'
- }
+ if (!response.ok) {
- async function getModel() {
- const response = await fetch(loadFrom + year + (nren ? '/' + nren : '')) // year is always set, nren stays empty for inspect and try
- const json = await response.json();
+ if ('message' in json) {
+ throw new Error(json.message);
+ } else {
+ throw new Error(`Request failed with status ${response.status}`);
+ }
+ }
- for (const questionName in json["verification_status"]) {
- verificationStatus.current.set(questionName, json["verification_status"][questionName]);
- }
- const survey = new Model(json['model']);
- survey.setVariable('surveyyear', year);
- survey.setVariable('previousyear', parseInt(year!) - 1);
-
- survey.data = json['data'];
- survey.clearIncorrectValues(true); // TODO test if this really removes all old values and such
- survey.currentPageNo = json['page'];
- survey.showNavigationButtons = false;
- survey.showTOC = false;
- if (readonly) {
- survey.mode = 'display';
+ for (const questionName in json["verification_status"]) {
+ verificationStatus.current.set(questionName, json["verification_status"][questionName]);
+ }
+
+ const survey = new Model(json['model']);
+ survey.setVariable('surveyyear', year);
+ survey.setVariable('previousyear', parseInt(year!) - 1);
+
+ survey.data = json['data'];
+ survey.clearIncorrectValues(true); // TODO test if this really removes all old values and such
+ survey.currentPageNo = json['page'];
+ survey.showNavigationButtons = false;
+ survey.showTOC = false;
+ if (readonly) {
+ survey.mode = 'display';
+ }
+
+ setSurveyModel(survey);
}
- setSurveyModel(survey);
+ getModel().catch(error => setError('Error when loading survey: ' + error.message))
+
+ });
+ console.log(surveyModel)
+ if (surveyModel === undefined) {
+ return error
}
+
+
function saveSurveyData(survey, success?, failure?) {
if (saveTo == '') {
return;
--
GitLab