Commit 910c64fa authored by Titouan Soulard's avatar Titouan Soulard

erp5_api_style: updates calls to `logError`

parent 432d1024
import json
from zExceptions import NotFound
# an external script is need to extract content from body: REQUEST.get('BODY') # an external script is need to extract content from body: REQUEST.get('BODY')
mode = mode or context.REQUEST.form.get("mode", "get") mode = mode or context.REQUEST.form.get("mode", "get")
text_content = text_content or context.REQUEST.form.get("text_content", "") text_content = text_content or context.REQUEST.form.get("text_content", "")
...@@ -7,20 +10,9 @@ web_section_value = context.getWebSectionValue() ...@@ -7,20 +10,9 @@ web_section_value = context.getWebSectionValue()
if web_section_value: if web_section_value:
context.REQUEST.set("web_section_relative_url", web_section_value.getRelativeUrl()) context.REQUEST.set("web_section_relative_url", web_section_value.getRelativeUrl())
import json
portal = context.getPortalObject() portal = context.getPortalObject()
response = container.REQUEST.RESPONSE 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): def getCorrespondingActionAndReturn(traversed_document, action_id, text_data, list_error=False):
return traversed_document.getTypeInfo().getDefaultViewFor( return traversed_document.getTypeInfo().getDefaultViewFor(
traversed_document, view=action_id traversed_document, view=action_id
...@@ -56,7 +48,6 @@ def json_loads_byteified(json_text): ...@@ -56,7 +48,6 @@ def json_loads_byteified(json_text):
ignore_dicts=True ignore_dicts=True
) )
mode_dict = { mode_dict = {
"put": "configuration_put_action_type", "put": "configuration_put_action_type",
"get": "configuration_get_action_type", "get": "configuration_get_action_type",
...@@ -64,17 +55,26 @@ mode_dict = { ...@@ -64,17 +55,26 @@ mode_dict = {
"allDocs": "configuration_alldocs_action_type", "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: if mode not in mode_dict:
return "Used Mode is not defined in the mode list %s" % mode_dict.keys() raise NotImplementedError("Used Mode is not defined in the mode list %s" % mode_dict.keys())
# Check JSON Form # Check JSON Form
if not data_dict: if not data_dict:
try: try:
data = json_loads_byteified(text_content) data = json_loads_byteified(text_content)
except BaseException as e: 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): 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: else:
data = data_dict data = data_dict
...@@ -85,25 +85,28 @@ if mode in ("get", "put"): ...@@ -85,25 +85,28 @@ if mode in ("get", "put"):
try: try:
document_id = data["id"] document_id = data["id"]
except KeyError: 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: 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: try:
document = portal.restrictedTraverse(str(document_id)) document = portal.restrictedTraverse(str(document_id))
except KeyError: 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: else:
document = context.jIOWebSection_getObjectFromData(data) document = context.jIOWebSection_getObjectFromData(data)
if document is None: if document is None:
return logError( raise NotFound("Document has not been found")
"The data you provided did not allow to find the corresponding document",
error_name="API-JSON-DOCUMENT-NOT-FOUND",
error_code=404,
)
else: else:
document = context document = context
result_list = []
error_dict = {} error_dict = {}
match = False match = False
...@@ -121,16 +124,17 @@ for erp5_action_key in erp5_action_dict.keys(): ...@@ -121,16 +124,17 @@ for erp5_action_key in erp5_action_dict.keys():
document, view_action['id'], data, list_error=True document, view_action['id'], data, list_error=True
) )
match = 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 mode == "post":
if response.getStatus() == 200: if response.getStatus() == 200:
response.setStatus(201) response.setStatus(201)
return result return result
except ValueError as e: except ValueError as e:
if mode in ("put", "get"): if mode in ("put", "get"):
return logError( return portal.ERP5Site_logApiErrorAndReturn(
"data did not match schema: %s" % e, error_code=400,
error_name="INPUT-DATA-DOES-NOT-MATCH-EXPECTED-DATA" error_message="Data did not match schema: %s" % str(e),
error_name="API-JSON-SCHEMA-ERROR",
) )
try: try:
error_dict.update(json.loads(str(e))) error_dict.update(json.loads(str(e)))
...@@ -151,6 +155,7 @@ if not match: ...@@ -151,6 +155,7 @@ if not match:
error_message = "Data did not validate against interface schemas" error_message = "Data did not validate against interface schemas"
if error_dict: if error_dict:
error_kw["detail_list"] = sorted(error_dict.items()) error_kw["detail_list"] = sorted(error_dict.items())
return logError(error_message, **error_kw) return portal.ERP5Site_logApiErrorAndReturn(
error_message=error_message,
raise ValueError("Unreachable code reached") **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