Implemented 400 response code on error

This commit is contained in:
2023-02-08 14:28:11 +01:00
parent 07f2adcc1f
commit 96f80e4589

View File

@@ -21,33 +21,35 @@ def process_xml(request: request, type: str) -> str:
:rtype: str
"""
start = time.time_ns()
response = dict()
code = 200
response_json = dict()
try:
request_data = json.loads(request.get_data(as_text=True))
data = request_data['data']
process = request_data['process']
if (type == "xsd"):
response['result'] = Parser.xsd(data, process)
response_json['result'] = Parser.xsd(data, process)
elif (type == "xslt"):
response['result'] = Parser.xslt(data, process)
response_json['result'] = Parser.xslt(data, process)
elif (type == "xpath"):
response['result'] = Parser.xpath(data, process)
response_json['result'] = Parser.xpath(data, process)
else:
raise ValueError("Valid operation types are: xsd, xslt, xpath")
response['status'] = "OK"
response_json['status'] = "OK"
except KeyError as e:
response['result'] = "Missing key: " + str(e)
response['status'] = "ERR"
response_json['result'] = "Missing key: " + str(e)
response_json['status'] = "ERR"
code = 400
except Exception as e:
response['result'] = str(e)
response['status'] = "ERR"
response_json['result'] = str(e)
response_json['status'] = "ERR"
code = 400
finally:
exec_time = (time.time_ns() - start) / 10**6
response['time'] = f"{exec_time:.03f}"
response['processor'] = "libxml2 over lxml"
return json.dumps(response)
response_json['time'] = f"{exec_time:.03f}"
response_json['processor'] = "libxml2 over lxml"
return json.dumps(response_json), code
@app.route("/xpathpost", methods=["POST"])