Skip to content

Commit

Permalink
feat: In case of user content error, response in structured way to av…
Browse files Browse the repository at this point in the history
…oid loosing history (#342)
  • Loading branch information
cedric05 authored Dec 14, 2024
1 parent 7a473af commit f1fc1fa
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 35 deletions.
72 changes: 56 additions & 16 deletions dotextensions/server/handlers/basic_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,8 @@ def get_method(self):
return RunHttpFileHandler.name

def run(self, command: Command) -> Result:
config = self.get_config(command)
try:
if config.curl:
req = self.get_curl_comp(config)
result = req.get_curl_output()
result = Result(
id=command.id,
result={
"body": result,
"headers": {
"Content-Type": mimetypes.types_map[".sh"],
},
},
)
else:
comp = self.get_request_comp(config)
result = self.get_request_result(command, comp)
result = self.execute(command)
except DotHttpException as exc:
logger.error(f"dothttp exception happened {exc}", exc_info=True)
result = Result(
Expand All @@ -76,7 +61,25 @@ def run(self, command: Command) -> Result:
result = Result(
id=command.id, result={"error_message": str(exc), "error": True}
)
return result

def execute(self, command):
config = self.get_config(command)
if config.curl:
req = self.get_curl_comp(config)
result = req.get_curl_output()
result = Result(
id=command.id,
result={
"body": result,
"headers": {
"Content-Type": mimetypes.types_map[".sh"],
},
},
)
else:
comp = self.get_request_comp(config)
result = self.get_request_result(command, comp)
return result

def get_curl_comp(self, config):
Expand Down Expand Up @@ -253,6 +256,43 @@ def get_request_comp(self, config):
def get_curl_comp(self, config):
return ContentCurlCompiler(config)

def run(self, command: Command) -> Result:
"""
When handling content, if an exception is raised, the response is not in the usual format.
Instead, it returns an error message. It is better to respond with a structured response.
"""
try:
return self.execute(command)
except DotHttpException as exc:
logger.error(f"dothttp exception happened {exc}", exc_info=True)
error_result = exc.message
except RequestException as exc:
logger.error(f"exception from requests {exc}", exc_info=True)
error_result = str(exc)
except Exception as exc:
logger.error(f"unknown error happened {exc}", exc_info=True)
error_result = str(exc)
response = {
"body": error_result,
"status": 0,
"method": "REQUEST_EXECUTION_ERROR",
"url": "REQUEST_EXECUTION_ERROR",
"headers": {
"Content-Type": "text/plain",
},
"output_file":"",
"error": True,
"error_message": error_result,
"contentType": "text/plain",
}
result = {
"response": response,
"script_result": {"stdout": "", "error": "", "properties":{}, "tests":[]},
"http": "REQUEST_EXECUTION_ERROR",
"filenameExtension": ".txt",
}
result.update(response)
return Result(id=command.id, result=result)

class FormatHttpFileHandler(BaseHandler):
method = "/file/format"
Expand Down
3 changes: 2 additions & 1 deletion dothttp/utils/property_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,9 @@ def validate_n_gen(prop, cache: Dict[str, Property]):
if "=" in prop:
key_values = prop.split("=")
if len(key_values) != 2:
prop_name = key_values[0]
raise HttpFileException(
message="default property should not have multiple `=`"
message=f"Property `{prop_name}` should not contain multiple `=` signs"
)
key, value = key_values
# strip white space for keys
Expand Down
60 changes: 45 additions & 15 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dothttp-req"
version = "0.0.43a28"
version = "0.0.43a29"
description = "Dothttp is Simple http client for testing and development"
authors = ["Prasanth <[email protected]>"]
license = "MIT"
Expand Down Expand Up @@ -30,7 +30,7 @@ parsys-requests-unixsocket = "0.3.2"
requests-aws4auth = "1.3.1"
requests-ntlm = "1.3.0"
restrictedpython = "7.4"
faker = "33.0.0"
faker = "33.1.0"
requests-hawk = "1.2.1"
msal = "1.31.1"
pyyaml = "6.0.2"
Expand All @@ -44,7 +44,7 @@ flask = "3.0.2"
python-magic = "^0.4.27"
js2py = "0.74"
flask-cors = "^5.0.0"
pytest = "^8.3.3"
pytest = "^8.3.4"
pytest-html = "^4.1.1"

[tool.poetry.extras]
Expand Down

0 comments on commit f1fc1fa

Please sign in to comment.