Enchanced error handling
This commit is contained in:
		| @@ -8,18 +8,44 @@ import Parser | |||||||
|  |  | ||||||
| app = Flask(__name__) | 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"]) | @app.route("/xpath", methods=["POST"]) | ||||||
| def xpath(): | def xpath(): | ||||||
|     start = time.time_ns() |     start = time.time_ns() | ||||||
|     request_data = json.loads(request.get_data(as_text=True)) |  | ||||||
|     xml = request_data['data'] |  | ||||||
|     xpath = request_data['process'] |  | ||||||
|      |      | ||||||
|     response = dict() |     response = dict() | ||||||
|     try: |     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['result'] = Parser.xpath(xml, xpath) | ||||||
|         response['status'] = "OK" |         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['result'] = str(e) | ||||||
|         response['status'] = "ERR" |         response['status'] = "ERR" | ||||||
|     finally: |     finally: | ||||||
| @@ -30,36 +56,23 @@ def xpath(): | |||||||
|  |  | ||||||
| @app.route("/xsd", methods=["POST"]) | @app.route("/xsd", methods=["POST"]) | ||||||
| def xsd(): | def xsd(): | ||||||
|     start = time.time_ns() |     return process_xml(request, "xsd") | ||||||
|     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) |  | ||||||
|  |  | ||||||
| @app.route("/xslt", methods=["POST"]) | @app.route("/xslt", methods=["POST"]) | ||||||
| def xslt(): | def xslt(): | ||||||
|     start = time.time_ns() |     start = time.time_ns() | ||||||
|     request_data = request.get_json() |  | ||||||
|     xml = request_data['data'] |  | ||||||
|     xslt = request_data['process'] |  | ||||||
|  |  | ||||||
|     response = dict() |     response = dict() | ||||||
|     try: |     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['result'] = Parser.xslt(xml, xslt) | ||||||
|         response['status'] = "OK" |         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['result'] = str(e) | ||||||
|         response['status'] = "ERR" |         response['status'] = "ERR" | ||||||
|     finally: |     finally: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user