2020-02-29 22:22:38 +00:00
|
|
|
from crc import ma, app
|
2019-12-27 18:50:03 +00:00
|
|
|
|
|
|
|
|
2020-02-10 21:19:23 +00:00
|
|
|
class ApiError(Exception):
|
2020-03-27 12:29:31 +00:00
|
|
|
def __init__(self, code, message, status_code=400,
|
|
|
|
file_name="", task_id="", task_name="", tag=""):
|
2020-02-29 22:22:38 +00:00
|
|
|
self.status_code = status_code
|
2020-03-27 12:29:31 +00:00
|
|
|
self.code = code # a short consistent string describing the error.
|
|
|
|
self.message = message # A detailed message that provides more information.
|
|
|
|
self.task_id = task_id or "" # OPTIONAL: The id of the task in the BPMN Diagram.
|
|
|
|
self.task_name = task_name or "" # OPTIONAL: The name of the task in the BPMN Diagram.
|
|
|
|
self.file_name = file_name or "" # OPTIONAL: The file that caused the error.
|
|
|
|
self.tag = tag or "" # OPTIONAL: The XML Tag that caused the issue.
|
2020-03-03 18:50:22 +00:00
|
|
|
Exception.__init__(self, self.message)
|
2019-12-27 18:50:03 +00:00
|
|
|
|
2020-03-27 12:29:31 +00:00
|
|
|
@classmethod
|
|
|
|
def from_task(cls, code, message, task, status_code=400):
|
|
|
|
"""Constructs an API Error with details pulled from the current task."""
|
|
|
|
instance = cls(code, message, status_code=status_code)
|
|
|
|
instance.task_id = task.task_spec.name or ""
|
|
|
|
instance.task_name = task.task_spec.description or ""
|
|
|
|
instance.file_name = task.workflow.spec.file or ""
|
|
|
|
return instance
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_task_spec(cls, code, message, task_spec, status_code=400):
|
|
|
|
"""Constructs an API Error with details pulled from the current task."""
|
|
|
|
instance = cls(code, message, status_code=status_code)
|
|
|
|
instance.task_id = task_spec.name or ""
|
|
|
|
instance.task_name = task_spec.description or ""
|
|
|
|
if task_spec._wf_spec:
|
|
|
|
instance.file_name = task_spec._wf_spec.file
|
|
|
|
return instance
|
|
|
|
|
2019-12-27 18:50:03 +00:00
|
|
|
|
|
|
|
class ApiErrorSchema(ma.Schema):
|
|
|
|
class Meta:
|
2020-03-27 12:29:31 +00:00
|
|
|
fields = ("code", "message", "workflow_name", "file_name", "task_name", "task_id")
|
2019-12-27 18:50:03 +00:00
|
|
|
|
2020-02-29 22:22:38 +00:00
|
|
|
|
|
|
|
@app.errorhandler(ApiError)
|
|
|
|
def handle_invalid_usage(error):
|
|
|
|
response = ApiErrorSchema().dump(error)
|
|
|
|
return response, error.status_code
|