Skip to content

Commit

Permalink
standardize exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
essweine committed Jan 30, 2024
1 parent bf34434 commit aa63d2f
Show file tree
Hide file tree
Showing 23 changed files with 39 additions and 51 deletions.
20 changes: 16 additions & 4 deletions SpiffWorkflow/bpmn/parser/event_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ def parse_error_event(self, error_event):
"""Parse the errorEventDefinition node and return an instance of ErrorEventDefinition."""
error_ref = error_event.get('errorRef')
if error_ref:
error = one(self.doc_xpath('.//bpmn:error[@id="%s"]' % error_ref))
try:
error = one(self.doc_xpath('.//bpmn:error[@id="%s"]' % error_ref))
except Exception:
self.raise_validation_exception('Expected an error node', node=error_event)
error_code = error.get('errorCode')
name = error.get('name')
else:
Expand All @@ -101,7 +104,10 @@ def parse_escalation_event(self, escalation_event):

escalation_ref = escalation_event.get('escalationRef')
if escalation_ref:
escalation = one(self.doc_xpath('.//bpmn:escalation[@id="%s"]' % escalation_ref))
try:
escalation = one(self.doc_xpath('.//bpmn:escalation[@id="%s"]' % escalation_ref))
except Exception:
self.raise_validation_exception('Expected an Escalation node', node=escalation_event)
escalation_code = escalation.get('escalationCode')
name = escalation.get('name')
else:
Expand All @@ -112,7 +118,10 @@ def parse_message_event(self, message_event):

message_ref = message_event.get('messageRef')
if message_ref is not None:
message = one(self.doc_xpath('.//bpmn:message[@id="%s"]' % message_ref))
try:
message = one(self.doc_xpath('.//bpmn:message[@id="%s"]' % message_ref))
except Exception:
self.raise_validation_exception('Expected a Message node', node=message_event)
name = message.get('name')
description = self.get_event_description(message_event)
correlations = self.get_message_correlations(message_ref)
Expand All @@ -127,7 +136,10 @@ def parse_signal_event(self, signal_event):

signal_ref = signal_event.get('signalRef')
if signal_ref:
signal = one(self.doc_xpath('.//bpmn:signal[@id="%s"]' % signal_ref))
try:
signal = one(self.doc_xpath('.//bpmn:signal[@id="%s"]' % signal_ref))
except Exception:
self.raise_validation_exception('Expected a Signal node', node=signal_event)
name = signal.get('name')
else:
name = signal_event.getparent().get('name')
Expand Down
5 changes: 2 additions & 3 deletions SpiffWorkflow/bpmn/parser/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA


BPMN_MODEL_NS = 'http://www.omg.org/spec/BPMN/20100524/MODEL'
DIAG_INTERCHANGE_NS = "http://www.omg.org/spec/BPMN/20100524/DI"
DIAG_COMMON_NS = "http://www.omg.org/spec/DD/20100524/DC"
Expand All @@ -34,8 +33,8 @@ def one(nodes, or_none=False):
"""
if not nodes and or_none:
return None
assert len(
nodes) == 1, 'Expected 1 result. Received %d results.' % (len(nodes))

assert len(nodes) == 1, 'Expected 1 result. Received %d results.' % (len(nodes))
return nodes[0]


Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion SpiffWorkflow/bpmn/serializer/migration/version_1_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from SpiffWorkflow.util.task import TaskState
from SpiffWorkflow.bpmn.specs.event_definitions.timer import LOCALTZ

from .exceptions import VersionMigrationError
from ..exceptions import VersionMigrationError

def td_to_iso(td):
total = td.total_seconds()
Expand Down
3 changes: 2 additions & 1 deletion SpiffWorkflow/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ def __init__(self,
"""
if not right_attribute and not right:
raise ValueError('require argument: right_attribute or right')
assert left_attribute is not None
if left_attribute is None:
raise ValueError('left attribute is None')
self.left_attribute = left_attribute
self.right_attribute = right_attribute
self.right = right
Expand Down
6 changes: 0 additions & 6 deletions SpiffWorkflow/serializer/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,6 @@ def deserialize_simple(self, wf_spec, s_state):
self.deserialize_task_spec(wf_spec, s_state, spec=spec)
return spec

def deserialize_generic(self, wf_spec, s_state,newclass):
assert isinstance(wf_spec, WorkflowSpec)
spec = newclass(wf_spec, s_state['name'])
self.deserialize_task_spec(wf_spec, s_state, spec=spec)
return spec

def serialize_start_task(self, spec):
return self.serialize_task_spec(spec)

Expand Down
3 changes: 1 addition & 2 deletions SpiffWorkflow/serializer/prettyxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ def deserialize_task_spec(self, workflow, start_node, read_specs):
pass
elif node.tag == 'description':
kwargs['description'] = node.text
elif node.tag == 'successor' \
or node.tag == 'default-successor':
elif node.tag == 'successor' or node.tag == 'default-successor':
if not node.text:
self.raise_parser_exception('Empty %s tag' % node.tag)
successors.append((None, node.text))
Expand Down
1 change: 0 additions & 1 deletion SpiffWorkflow/specs/AcquireMutex.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def __init__(self, wf_spec, name, mutex, **kwargs):
:type kwargs: dict
:param kwargs: See :class:`SpiffWorkflow.specs.TaskSpec`.
"""
assert mutex is not None
TaskSpec.__init__(self, wf_spec, name, **kwargs)
self.mutex = mutex

Expand Down
3 changes: 0 additions & 3 deletions SpiffWorkflow/specs/Choose.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ def __init__(self, wf_spec, name, context, choice=None, **kwargs):
:type kwargs: dict
:param kwargs: See :class:`SpiffWorkflow.specs.TaskSpec`.
"""
assert wf_spec is not None
assert name is not None
assert context is not None
# HACK: inherit from TaskSpec (not Trigger) on purpose.
TaskSpec.__init__(self, wf_spec, name, **kwargs)
self.context = context
Expand Down
1 change: 0 additions & 1 deletion SpiffWorkflow/specs/ExclusiveChoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ def connect(self, taskspec):
:type task_spec: TaskSpec
:param task_spec: The following task spec.
"""
assert self.default_task_spec is None
self.default_task_spec = taskspec.name
super().connect(taskspec)

Expand Down
2 changes: 0 additions & 2 deletions SpiffWorkflow/specs/Execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ def __init__(self, wf_spec, name, args=None, **kwargs):
:type kwargs: dict
:param kwargs: kwargs to pass-through to TaskSpec initializer.
"""
assert wf_spec is not None
assert name is not None
TaskSpec.__init__(self, wf_spec, name, **kwargs)
self.args = args

Expand Down
3 changes: 0 additions & 3 deletions SpiffWorkflow/specs/Gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ def __init__(self, wf_spec, name, context, **kwargs):
:type kwargs: dict
:param kwargs: See :class:`SpiffWorkflow.specs.TaskSpec`.
"""
assert wf_spec is not None
assert name is not None
assert context is not None
TaskSpec.__init__(self, wf_spec, name, **kwargs)
self.context = context

Expand Down
1 change: 0 additions & 1 deletion SpiffWorkflow/specs/MultiChoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def connect_if(self, condition, task_spec):
condition -- a condition (Condition)
taskspec -- the conditional task spec
"""
assert task_spec is not None
self._outputs.append(task_spec.name)
self.cond_task_specs.append((condition, task_spec.name))
task_spec._connect_notify(self)
Expand Down
1 change: 0 additions & 1 deletion SpiffWorkflow/specs/ReleaseMutex.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def __init__(self, wf_spec, name, mutex, **kwargs):
:type kwargs: dict
:param kwargs: See :class:`SpiffWorkflow.specs.TaskSpec`.
"""
assert mutex is not None
TaskSpec.__init__(self, wf_spec, name, **kwargs)
self.mutex = mutex

Expand Down
2 changes: 0 additions & 2 deletions SpiffWorkflow/specs/SubWorkflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ def __init__(self,
:type kwargs: dict
:param kwargs: See :class:`SpiffWorkflow.specs.TaskSpec`.
"""
assert wf_spec is not None
assert name is not None
super(SubWorkflow, self).__init__(wf_spec, name, **kwargs)
self.file = None
self.in_assign = in_assign is not None and in_assign or []
Expand Down
1 change: 0 additions & 1 deletion SpiffWorkflow/specs/ThreadMerge.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def __init__(self,
:type kwargs: dict
:param kwargs: See :class:`SpiffWorkflow.specs.Join`.
"""
assert split_task is not None
Join.__init__(self, wf_spec, name, split_task, **kwargs)

def _start(self, my_task):
Expand Down
2 changes: 0 additions & 2 deletions SpiffWorkflow/specs/Transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ def __init__(self, wf_spec, name, transforms=None, **kwargs):
:type kwargs: dict
:param kwargs: See :class:`SpiffWorkflow.specs.TaskSpec`.
"""
assert wf_spec is not None
assert name is not None
TaskSpec.__init__(self, wf_spec, name, **kwargs)
self.transforms = transforms

Expand Down
4 changes: 0 additions & 4 deletions SpiffWorkflow/specs/Trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ def __init__(self, wf_spec, name, context, times=1, **kwargs):
:type kwargs: dict
:param kwargs: See :class:`SpiffWorkflow.specs.TaskSpec`.
"""
assert wf_spec is not None
assert name is not None
assert context is not None
assert isinstance(context, list)
TaskSpec.__init__(self, wf_spec, name, **kwargs)
self.context = context
self.times = times
Expand Down
3 changes: 0 additions & 3 deletions SpiffWorkflow/specs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ def __init__(self, wf_spec, name, **kwargs):
:type post_assign: list((str, object))
:param post_assign: a list of name/value pairs
"""
assert wf_spec is not None
assert name is not None
self._wf_spec = wf_spec
self.name = str(name)
self.description = kwargs.get('description', None)
Expand Down Expand Up @@ -279,7 +277,6 @@ def _on_ready(self, my_task):
:type my_task: Task
:param my_task: The associated task in the task tree.
"""
assert my_task is not None
self.test()

# Assign variables, if so requested.
Expand Down
20 changes: 16 additions & 4 deletions SpiffWorkflow/spiff/parser/event_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ def parse_message_event(self, message_event):

message_ref = message_event.get('messageRef')
if message_ref:
message = one(self.doc_xpath('.//bpmn:message[@id="%s"]' % message_ref))
try:
message = one(self.doc_xpath('.//bpmn:message[@id="%s"]' % message_ref))
except Exception:
self.raise_validation_error('Expected a Message node', node=message_event)
name = message.get('name')
extensions = self.parse_extensions(message)
correlations = self.get_message_correlations(message_ref)
Expand All @@ -61,7 +64,10 @@ def parse_signal_event(self, signal_event):
"""Parse a Spiff signal event"""
signal_ref = signal_event.get('signalRef')
if signal_ref is not None:
signal = one(self.doc_xpath(f'.//bpmn:signal[@id="{signal_ref}"]'))
try:
signal = one(self.doc_xpath(f'.//bpmn:signal[@id="{signal_ref}"]'))
except Exception:
self.raise_validation_error('Expected a Signal node', node=signal_event)
name = signal.get('name')
extensions = self.parse_extensions(signal)
expression = extensions.get('payloadExpression')
Expand All @@ -75,7 +81,10 @@ def parse_error_event(self, error_event):
"""Parse a Spiff error event"""
error_ref = error_event.get('errorRef')
if error_ref is not None:
error = one(self.doc_xpath(f'.//bpmn:error[@id="{error_ref}"]'))
try:
error = one(self.doc_xpath(f'.//bpmn:error[@id="{error_ref}"]'))
except Exception:
self.raise_validation_error('Expected an Error node', node=error_event)
name = error.get('name')
code = error.get('errorCode')
extensions = self.parse_extensions(error)
Expand All @@ -90,7 +99,10 @@ def parse_escalation_event(self, escalation_event):
"""Parse a Spiff error event"""
escalation_ref = escalation_event.get('escalationRef')
if escalation_ref is not None:
escalation = one(self.doc_xpath(f'.//bpmn:escalation[@id="{escalation_ref}"]'))
try:
escalation = one(self.doc_xpath(f'.//bpmn:escalation[@id="{escalation_ref}"]'))
except Exception:
self.raise_validation_error('Expected an Escalation node', node=escalation_event)
name = escalation.get('name')
code = escalation.get('escalationCode')
extensions = self.parse_extensions(escalation)
Expand Down
4 changes: 0 additions & 4 deletions SpiffWorkflow/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ def __init__(self, workflow, task_spec, parent=None, state=TaskState.MAYBE, id=N
state (`TaskState`): the state of this task (default MAYBE)
id: an optional id (defaults to a random UUID)
"""
assert workflow is not None
assert task_spec is not None

self.id = id or uuid4()
workflow.tasks[self.id] = self
self.workflow = workflow
Expand Down Expand Up @@ -278,7 +275,6 @@ def _sync_children(self, task_specs, state=TaskState.MAYBE):

def _child_added_notify(self, child):
"""Called by another task to let us know that a child was added."""
assert child is not None
self._children.append(child.id)

def _drop_children(self, force=False):
Expand Down
1 change: 0 additions & 1 deletion SpiffWorkflow/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def __init__(self, workflow_spec, deserializing=False):
workflow_spec (`WorkflowSpec`): the spec that describes this workflow
deserializing (bool): whether this workflow is being deserialized
"""
assert workflow_spec is not None
self.spec = workflow_spec
self.data = {}
self.locks = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from SpiffWorkflow import TaskState
from SpiffWorkflow.bpmn.script_engine import PythonScriptEngine, TaskDataEnvironment
from SpiffWorkflow.bpmn.serializer.migration.exceptions import VersionMigrationError
from SpiffWorkflow.bpmn.serializer.exceptions import VersionMigrationError

from .BaseTestCase import BaseTestCase

Expand Down

0 comments on commit aa63d2f

Please sign in to comment.