Skip to content

Commit

Permalink
feat: better error handling for fetch call
Browse files Browse the repository at this point in the history
  • Loading branch information
0hsn committed Dec 27, 2024
1 parent 1ca2959 commit d6ac37f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 26 deletions.
52 changes: 26 additions & 26 deletions chk/modules/fetch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,40 +25,38 @@ 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())

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
Expand All @@ -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,
},
Expand Down
53 changes: 53 additions & 0 deletions tests/modules/fetch/fetch_module_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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)

0 comments on commit d6ac37f

Please sign in to comment.