2021-02-24 10:56:25 -05:00
import re
2021-02-19 14:49:53 -05:00
generic_message = """ Workflow validation failed. For more information about the error, see below. """
2021-03-01 16:41:55 -05:00
# known_errors is a dictionary of errors from validation that we want to give users a hint for solving their problem.
# The key is the known error, or part of the known error. It is a string.
# We use the key to see if we have a known error.
# The value is a dictionary that contains the hint for the user.
# If we want to capture details about the error we can use a regex for the key.
# If you use a regex, you must add a groups entry to the value dictionary.
# groups is a dictionary defining the values returned from the groups() method from the regex search.
# They key is the string used as a placeholder in the hint, and the value is the groups index.
# I know this explanation is confusing. If you have ideas for clarification, pull request welcome.
2021-02-12 14:18:42 -05:00
known_errors = { ' Error is Non-default exclusive outgoing sequence flow without condition ' :
2021-03-01 16:41:55 -05:00
{ ' hint ' : ' Add a Condition Type to your gateway path. ' } ,
2021-03-31 10:17:08 -04:00
' Could not set task title on task ( \ w+) with \' (.*) \' property because \\ 1: Error evaluating '
' expression \' (.*) \' , $ ' :
2021-03-01 16:41:55 -05:00
{ ' hint ' : ' You are overriding the title for task ` {task_id} `, using the ` {property} ` extension, and it is causing an error. Look under the extensions tab for the task, and check the value you are setting for the property. ' ,
' groups ' : { ' task_id ' : 0 , ' property ' : 1 } } }
2021-02-19 14:49:53 -05:00
2021-02-04 11:23:05 -05:00
class ValidationErrorService ( object ) :
""" Validation Error Service interprets messages return from api.workflow.validate_workflow_specification
Validation is run twice ,
once where we try to fill in all form fields
and a second time where we only fill in the required fields .
We get a list that contains possible errors from the validation . """
@staticmethod
def interpret_validation_errors ( errors ) :
if len ( errors ) == 0 :
2021-02-10 12:28:59 -05:00
return ( )
2021-02-19 14:49:53 -05:00
2021-02-12 14:18:42 -05:00
interpreted_errors = [ ]
for error_type in [ ' all ' , ' required ' ] :
if error_type in errors :
hint = generic_message
for known_key in known_errors :
2021-03-01 16:41:55 -05:00
regex = re . compile ( known_key )
result = regex . search ( errors [ error_type ] . message )
if result is not None :
2021-02-12 14:18:42 -05:00
if ' hint ' in known_errors [ known_key ] :
2021-03-01 16:41:55 -05:00
if ' groups ' in known_errors [ known_key ] :
caught = { }
for group in known_errors [ known_key ] [ ' groups ' ] :
group_id = known_errors [ known_key ] [ ' groups ' ] [ group ]
group_value = result . groups ( ) [ group_id ]
caught [ group ] = group_value
hint = known_errors [ known_key ] [ ' hint ' ] . format ( * * caught )
else :
hint = known_errors [ known_key ] [ ' hint ' ]
2021-02-19 14:49:53 -05:00
2021-02-12 14:18:42 -05:00
errors [ error_type ] . hint = hint
interpreted_errors . append ( errors [ error_type ] )
return interpreted_errors