From 0320bb5e299dd9da3d5c16e6334c42c7cee157c2 Mon Sep 17 00:00:00 2001 From: forest Date: Sun, 21 Feb 2021 14:26:00 -0600 Subject: [PATCH] fix potential exceptions in error handler --- capsulflask/spoke_api.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/capsulflask/spoke_api.py b/capsulflask/spoke_api.py index 198af95..01fbe54 100644 --- a/capsulflask/spoke_api.py +++ b/capsulflask/spoke_api.py @@ -141,9 +141,12 @@ def handle_create(operation_id, request_body): ) except: error_message = my_exec_info_message(sys.exc_info()) - params = f"email='{request_body['email']}', id='{request_body['id']}', " - params = f"{params}, template_image_file_name='{request_body['template_image_file_name']}', vcpus='{request_body['vcpus']}'" - params = f"{params}, memory_mb='{request_body['memory_mb']}', ssh_authorized_keys='{request_body['ssh_authorized_keys']}'" + params = f"email='{request_body['email'] if 'email' in request_body else 'KeyError'}', " + params= f"{params} id='{request_body['id'] if 'id' in request_body else 'KeyError'}', " + params= f"{params} template_image_file_name='{request_body['template_image_file_name'] if 'template_image_file_name' in request_body else 'KeyError'}', " + params= f"{params} vcpus='{request_body['vcpus'] if 'vcpus' in request_body else 'KeyError'}', " + params= f"{params} memory_mb='{request_body['memory_mb'] if 'memory_mb' in request_body else 'KeyError'}', " + params= f"{params} ssh_authorized_keys='{request_body['ssh_authorized_keys'] if 'ssh_authorized_keys' in request_body else 'KeyError'}', " current_app.logger.error(f"spoke_model.create({params}) failed: {error_message}") return jsonify(dict(assignment_status=assignment_status, error_message=error_message)) @@ -162,7 +165,9 @@ def handle_destroy(operation_id, request_body): current_app.config['SPOKE_MODEL'].destroy(id=request_body['id'], email=request_body['email']) except: error_message = my_exec_info_message(sys.exc_info()) - current_app.logger.error(f"current_app.config['SPOKE_MODEL'].destroy(id='{request_body['id']}', email='{request_body['email']}') failed: {error_message}") + params = f"email='{request_body['email'] if 'email' in request_body else 'KeyError'}', " + params= f"{params} id='{request_body['id'] if 'id' in request_body else 'KeyError'}', " + current_app.logger.error(f"current_app.config['SPOKE_MODEL'].destroy({params}) failed: {error_message}") return jsonify(dict(assignment_status="assigned", status="error", error_message=error_message)) return jsonify(dict(assignment_status="assigned", status="success")) @@ -184,7 +189,10 @@ def handle_vm_state_command(operation_id, request_body): current_app.config['SPOKE_MODEL'].vm_state_command(id=request_body['id'], email=request_body['email'], command=request_body['command']) except: error_message = my_exec_info_message(sys.exc_info()) - current_app.logger.error(f"current_app.config['SPOKE_MODEL'].vm_state_command(id='{request_body['id']}', email='{request_body['email']}, command='{request_body['command']}') failed: {error_message}") + params = f"email='{request_body['email'] if 'email' in request_body else 'KeyError'}', " + params= f"{params} id='{request_body['id'] if 'id' in request_body else 'KeyError'}', " + params= f"{params} command='{request_body['command'] if 'command' in request_body else 'KeyError'}', " + current_app.logger.error(f"current_app.config['SPOKE_MODEL'].vm_state_command({params}) failed: {error_message}") return jsonify(dict(assignment_status="assigned", status="error", error_message=error_message)) return jsonify(dict(assignment_status="assigned", status="success")) \ No newline at end of file