Skip to content
Snippets Groups Projects
Commit 97926cb5 authored by Bjarke Madsen's avatar Bjarke Madsen
Browse files

Add a bit more robust error handling

parent d08c2618
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment