From d6ac37f66cb6e942c5b3178bde6d1e310c70ec5a Mon Sep 17 00:00:00 2001 From: HSN Date: Fri, 27 Dec 2024 22:30:08 +0600 Subject: [PATCH] feat: better error handling for fetch call --- chk/modules/fetch/__init__.py | 52 +++++++++++------------ tests/modules/fetch/fetch_module_test.py | 53 ++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 26 deletions(-) diff --git a/chk/modules/fetch/__init__.py b/chk/modules/fetch/__init__.py index 52ebf686..3e80c043 100644 --- a/chk/modules/fetch/__init__.py +++ b/chk/modules/fetch/__init__.py @@ -7,9 +7,12 @@ from chk.infrastructure.document import VersionedDocumentSupport from chk.infrastructure.file_loader import ExecuteContext, FileContext from chk.infrastructure.logging import debug, error_trace, with_catch_log -from chk.infrastructure.symbol_table import ExecResponse, ExposeManager, \ - VariableTableManager, \ - Variables +from chk.infrastructure.symbol_table import ( + ExecResponse, + ExposeManager, + VariableTableManager, + Variables, +) from chk.infrastructure.view import PresentationService, die_with_error from chk.modules.fetch.entities import FetchTask from chk.modules.fetch.services import FetchPresenter, HttpDocumentSupport @@ -22,11 +25,6 @@ def call(file_ctx: FileContext, exec_ctx: ExecuteContext) -> ExecResponse: debug(file_ctx) debug(exec_ctx) - r_exception: Exception | None = None - variable_doc = Variables() - output_data = Variables() - exposed_data = {} - try: http_doc = HttpDocumentSupport.from_file_context(file_ctx) debug(http_doc.model_dump_json()) @@ -34,28 +32,31 @@ def call(file_ctx: FileContext, exec_ctx: ExecuteContext) -> ExecResponse: VersionedDocumentSupport.validate_with_schema( HttpDocumentSupport.build_schema(), http_doc ) + except Exception as ex: + error_trace(exception=sys.exc_info()).error(ex) + return ExecResponse(file_ctx=file_ctx, exec_ctx=exec_ctx, exception=ex) - VariableTableManager.handle(variable_doc, http_doc, exec_ctx) - debug(variable_doc.data) + variable_doc = Variables() + VariableTableManager.handle(variable_doc, http_doc, exec_ctx) + debug(variable_doc.data) - HttpDocumentSupport.process_request_template(http_doc, variable_doc) - debug(http_doc.model_dump()) + HttpDocumentSupport.process_request_template(http_doc, variable_doc) + debug(http_doc.model_dump()) - # try: + try: response = HttpDocumentSupport.execute_request(http_doc) - - output_data = Variables({"_response": response.model_dump()}) - debug(output_data.data) - - exposed_data = ExposeManager.get_exposed_replaced_data( - http_doc, - {**variable_doc.data, **output_data.data}, - ) - debug(exposed_data) - except Exception as ex: - r_exception = ex error_trace(exception=sys.exc_info()).error(ex) + return ExecResponse(file_ctx=file_ctx, exec_ctx=exec_ctx, exception=ex) + + output_data = Variables({"_response": response.model_dump()}) + debug(output_data.data) + + exposed_data = ExposeManager.get_exposed_replaced_data( + http_doc, + {**variable_doc.data, **output_data.data}, + ) + debug(exposed_data) # TODO: instead if sending specific report items, and making presentable in other # module, we should prepare and long and short form of presentable that can be @@ -74,10 +75,9 @@ def call(file_ctx: FileContext, exec_ctx: ExecuteContext) -> ExecResponse: exec_ctx=exec_ctx, variables_exec=output_data, variables=variable_doc, - exception=r_exception, exposed=exposed_data, report={ - "is_success": r_exception is None, + "is_success": True, "request_method": request_method, "request_url": request_url, }, diff --git a/tests/modules/fetch/fetch_module_test.py b/tests/modules/fetch/fetch_module_test.py index e3c104e1..4b6d3e7e 100644 --- a/tests/modules/fetch/fetch_module_test.py +++ b/tests/modules/fetch/fetch_module_test.py @@ -3,7 +3,11 @@ """ Fetch module tests """ + +import requests + from chk.infrastructure.file_loader import ExecuteContext, FileContext +from chk.infrastructure.symbol_table import ExecResponse from chk.modules.fetch import call @@ -29,3 +33,52 @@ def test_call_pass(): er = call(file_ctx, exec_ctx) assert "_response" in er.variables_exec + + @staticmethod + def test_fails_for_doc_with_no_version(): + file_ctx = FileContext( + document={ + # "version": "default:http:0.7.2", + "request": { + "url": "https://jsonplaceholder.typicode.com/albums/1", + "method": "GET", + }, + } + ) + + exec_ctx = ExecuteContext( + options={ + "dump": True, + "format": False, + } + ) + + er = call(file_ctx, exec_ctx) + + assert isinstance(er, ExecResponse) + assert isinstance(er.exception, RuntimeError) + + @staticmethod + def test_fails_for_http_error(): + file_ctx = FileContext( + document={ + "version": "default:http:0.7.2", + "request": { + "url": "https://jsonplaceholdery.typicode.com/albums/1", + "method": "GET", + }, + } + ) + + exec_ctx = ExecuteContext( + options={ + "dump": True, + "format": False, + } + ) + + er = call(file_ctx, exec_ctx) + + assert isinstance(er, ExecResponse) + assert isinstance(er.exception, requests.ConnectionError) +