Commit 4ac87bac authored by Titouan Soulard's avatar Titouan Soulard

erp5_api_style: updates calls to `logError`

parent 60be9c17
import json
from zExceptions import NotFound
# an external script is need to extract content from body: REQUEST.get('BODY')
mode = mode or context.REQUEST.form.get("mode", "get")
text_content = text_content or context.REQUEST.form.get("text_content", "")
......@@ -7,20 +10,9 @@ web_section_value = context.getWebSectionValue()
if web_section_value:
context.REQUEST.set("web_section_relative_url", web_section_value.getRelativeUrl())
import json
portal = context.getPortalObject()
response = container.REQUEST.RESPONSE
# XXX Hardcoded, to be removed
def logError(e, error_name="", error_code=400, detail_list=None):
return portal.ERP5Site_logApiErrorAndReturn(
error_code=error_code,
error_message=e,
error_name=error_name,
detail_list=detail_list,
)
def getCorrespondingActionAndReturn(traversed_document, action_id, text_data, list_error=False):
return traversed_document.getTypeInfo().getDefaultViewFor(
traversed_document, view=action_id
......@@ -56,7 +48,6 @@ def json_loads_byteified(json_text):
ignore_dicts=True
)
mode_dict = {
"put": "configuration_put_action_type",
"get": "configuration_get_action_type",
......@@ -64,17 +55,26 @@ mode_dict = {
"allDocs": "configuration_alldocs_action_type",
}
# Not expected to be called anytime since allowed modes are already restricted in component
if mode not in mode_dict:
return "Used Mode is not defined in the mode list %s" % list(mode_dict)
raise NotImplementedError("Used Mode is not defined in the mode list %s" % mode_dict.keys())
# Check JSON Form
if not data_dict:
try:
data = json_loads_byteified(text_content)
except BaseException as e:
return logError(str(e), error_name="API-JSON-INVALID-JSON")
return portal.ERP5Site_logApiErrorAndReturn(
error_code=400,
error_message=str(e),
error_name="API-JSON-INVALID-JSON",
)
if not isinstance(data, dict):
return logError("Did not received a JSON Object", error_name="API-JSON-NOT-JSON-OBJECT")
return portal.ERP5Site_logApiErrorAndReturn(
error_code=400,
error_message="Did not received a JSON Object",
error_name="API-JSON-NOT-JSON-OBJECT",
)
else:
data = data_dict
......@@ -85,25 +85,28 @@ if mode in ("get", "put"):
try:
document_id = data["id"]
except KeyError:
return logError("Cannot find id property", error_name="API-JSON-NO-ID-PROPERTY")
return portal.ERP5Site_logApiErrorAndReturn(
error_code=400,
error_message="Cannot find id property",
error_name="API-JSON-NO-ID-PROPERTY",
)
except TypeError:
return logError("Did not received a JSON Object", error_name="API-JSON-NOT-JSON-OBJECT")
return portal.ERP5Site_logApiErrorAndReturn(
error_code=400,
error_message="Did not received a JSON Object",
error_name="API-JSON-NOT-JSON-OBJECT",
)
try:
document = portal.restrictedTraverse(str(document_id))
except KeyError:
return logError("Document has not been found", error_name="API-DOCUMENT-NOT-FOUND", error_code=404)
raise NotFound("Document has not been found")
else:
document = context.jIOWebSection_getObjectFromData(data)
if document is None:
return logError(
"The data you provided did not allow to find the corresponding document",
error_name="API-JSON-DOCUMENT-NOT-FOUND",
error_code=404,
)
raise NotFound("Document has not been found")
else:
document = context
result_list = []
error_dict = {}
match = False
......@@ -121,16 +124,17 @@ for erp5_action_key in erp5_action_dict.keys():
document, view_action['id'], data, list_error=True
)
match = True
# If an object is created, 201 code is exptected
# If an object is created, 201 code is expected
if mode == "post":
if response.getStatus() == 200:
response.setStatus(201)
return result
except ValueError as e:
if mode in ("put", "get"):
return logError(
"data did not match schema: %s" % e,
error_name="INPUT-DATA-DOES-NOT-MATCH-EXPECTED-DATA"
return portal.ERP5Site_logApiErrorAndReturn(
error_code=400,
error_message="Data did not match schema: %s" % str(e),
error_name="API-JSON-SCHEMA-ERROR",
)
try:
error_dict.update(json.loads(str(e)))
......@@ -151,6 +155,7 @@ if not match:
error_message = "Data did not validate against interface schemas"
if error_dict:
error_kw["detail_list"] = sorted(error_dict.items())
return logError(error_message, **error_kw)
raise ValueError("Unreachable code reached")
return portal.ERP5Site_logApiErrorAndReturn(
error_message=error_message,
**error_kw
)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment