Proper error handling for node type editor

This commit is contained in:
Sybren A. Stüvel 2018-02-01 14:13:01 +01:00
parent 284873ddd4
commit 1ca2f336c4
2 changed files with 17 additions and 6 deletions

View File

@ -580,13 +580,14 @@ def edit_node_type(project_url, node_type_name):
api = system_util.pillar_api() api = system_util.pillar_api()
# Fetch the Node or 404 # Fetch the Node or 404
try: try:
project = Project.find_one({ project = Project.find_one({'where': {'url': project_url}}, api=api)
'where': '{"url" : "%s"}' % (project_url)}, api=api)
except ResourceNotFound: except ResourceNotFound:
return abort(404) return abort(404)
utils.attach_project_pictures(project, api) utils.attach_project_pictures(project, api)
node_type = project.get_node_type(node_type_name) node_type = project.get_node_type(node_type_name)
form = NodeTypeForm() form = NodeTypeForm()
if form.validate_on_submit(): if form.validate_on_submit():
# Update dynamic & form schemas # Update dynamic & form schemas
dyn_schema = json.loads(form.dyn_schema.data) dyn_schema = json.loads(form.dyn_schema.data)
@ -618,6 +619,15 @@ def edit_node_type(project_url, node_type_name):
form.dyn_schema.data = json.dumps(dyn_schema, indent=4) form.dyn_schema.data = json.dumps(dyn_schema, indent=4)
form.permissions.data = json.dumps(permissions, indent=4) form.permissions.data = json.dumps(permissions, indent=4)
if request.method == 'POST':
# Send back a JSON response, as this is actually called
# from JS instead of rendered as page.
if form.errors:
resp = jsonify({'_message': str(form.errors)})
resp.status_code = 400
return resp
return jsonify({'_message': 'ok'})
return render_template('projects/edit_node_type_embed.html', return render_template('projects/edit_node_type_embed.html',
form=form, form=form,
project=project, project=project,

View File

@ -82,13 +82,14 @@ script.
var data = $node_type_form.serialize(); var data = $node_type_form.serialize();
$.post(url, data, function(datata) { $.post(url, data)
})
.done(function(){ .done(function(){
toastr.success("Node Type: {{ node_type['name'] | undertitle }}", "Saved") toastr.success("Node Type: {{ node_type['name'] | undertitle }}", "Saved")
}) })
.fail(function(){ .fail(function(err) {
toastr.error("Node Type: {{ node_type['name'] | undertitle }}", "Could not save") toastr.error(
xhrErrorResponseMessage(err),
"Could not save {{ node_type['name'] | undertitle }}")
}); });
}); });
}); });