Enchanced error handling
This commit is contained in:
		| @@ -8,18 +8,44 @@ import Parser | ||||
|  | ||||
| app = Flask(__name__) | ||||
|  | ||||
| def process_xml(request, type): | ||||
|     start = time.time_ns() | ||||
|  | ||||
|     response = dict() | ||||
|     try: | ||||
|         request_data = json.loads(request.get_data(as_text=True)) | ||||
|         xml = request_data['data'] | ||||
|         xsd = request_data['process'] | ||||
|         response['result'] = Parser.xsd(xml, xsd) | ||||
|         response['status'] = "OK" | ||||
|     except KeyError as e: | ||||
|         response['result'] = "Missing key: " + str(e) | ||||
|         response['status'] = "ERR" | ||||
|     except Exception as e: | ||||
|         response['result'] = str(e) | ||||
|         response['status'] = "ERR" | ||||
|     finally: | ||||
|         exec_time = (time.time_ns() - start) / 10**6 | ||||
|         response['time'] = f"{exec_time:.03f}" | ||||
|         response['processor'] = "libxml2 over lxml" | ||||
|         return json.dumps(response) | ||||
|  | ||||
|  | ||||
| @app.route("/xpath", methods=["POST"]) | ||||
| def xpath(): | ||||
|     start = time.time_ns() | ||||
|     request_data = json.loads(request.get_data(as_text=True)) | ||||
|     xml = request_data['data'] | ||||
|     xpath = request_data['process'] | ||||
|      | ||||
|     response = dict() | ||||
|     try: | ||||
|         request_data = json.loads(request.get_data(as_text=True)) | ||||
|         xml = request_data['data'] | ||||
|         xpath = request_data['process'] | ||||
|         response['result'] = Parser.xpath(xml, xpath) | ||||
|         response['status'] = "OK" | ||||
|     except BaseException as e: | ||||
|     except KeyError as e: | ||||
|         response['result'] = "Missing key: " + str(e) | ||||
|         response['status'] = "ERR" | ||||
|     except Exception as e: | ||||
|         response['result'] = str(e) | ||||
|         response['status'] = "ERR" | ||||
|     finally: | ||||
| @@ -30,36 +56,23 @@ def xpath(): | ||||
|  | ||||
| @app.route("/xsd", methods=["POST"]) | ||||
| def xsd(): | ||||
|     start = time.time_ns() | ||||
|     request_data = json.loads(request.get_data(as_text=True)) | ||||
|     xml = request_data['data'] | ||||
|     xsd = request_data['process'] | ||||
|  | ||||
|     response = dict() | ||||
|     try: | ||||
|         response['result'] = Parser.xsd(xml, xsd) | ||||
|         response['status'] = "OK" | ||||
|     except BaseException as e: | ||||
|         response['result'] = str(e) | ||||
|         response['status'] = "ERR" | ||||
|     finally: | ||||
|         exec_time = (time.time_ns() - start) / 10**6 | ||||
|         response['time'] = f"{exec_time:.03f}" | ||||
|         response['processor'] = "libxml2 over lxml" | ||||
|         return json.dumps(response) | ||||
|     return process_xml(request, "xsd") | ||||
|  | ||||
| @app.route("/xslt", methods=["POST"]) | ||||
| def xslt(): | ||||
|     start = time.time_ns() | ||||
|     request_data = request.get_json() | ||||
|     xml = request_data['data'] | ||||
|     xslt = request_data['process'] | ||||
|  | ||||
|     response = dict() | ||||
|     try: | ||||
|         request_data = json.loads(request.get_data(as_text=True)) | ||||
|         xml = request_data['data'] | ||||
|         xslt = request_data['process'] | ||||
|         response['result'] = Parser.xslt(xml, xslt) | ||||
|         response['status'] = "OK" | ||||
|     except BaseException as e: | ||||
|     except KeyError as e: | ||||
|         response['result'] = "Missing key: " + str(e) | ||||
|         response['status'] = "ERR" | ||||
|     except Exception as e: | ||||
|         response['result'] = str(e) | ||||
|         response['status'] = "ERR" | ||||
|     finally: | ||||
|   | ||||
		Reference in New Issue
	
	Block a user