move common script errors to script so it can keep the error messaging consistent across scripts

This commit is contained in:
jasquat 2023-03-07 09:17:32 -05:00
parent 66e254a3b0
commit 1cfeb32c3b
3 changed files with 28 additions and 10 deletions

View File

@ -7,7 +7,6 @@ from spiffworkflow_backend.models.process_model import ProcessModelInfo
from spiffworkflow_backend.models.script_attributes_context import ( from spiffworkflow_backend.models.script_attributes_context import (
ScriptAttributesContext, ScriptAttributesContext,
) )
from spiffworkflow_backend.scripts.script import ProcessModelIdentifierMissingError
from spiffworkflow_backend.scripts.script import Script from spiffworkflow_backend.scripts.script import Script
@ -38,10 +37,8 @@ class GetMarkdownFileDownloadLink(Script):
label = parts[1].split("=")[1] label = parts[1].split("=")[1]
process_model_identifier = script_attributes_context.process_model_identifier process_model_identifier = script_attributes_context.process_model_identifier
if process_model_identifier is None: if process_model_identifier is None:
raise ProcessModelIdentifierMissingError( raise self.get_proces_model_identifier_is_missing_error(
"The process model identifier was not given to script" "markdown_file_download_link"
" 'markdown_file_download_link'. This script needs to be run from"
" within the context of a process model."
) )
modified_process_model_identifier = ( modified_process_model_identifier = (
ProcessModelInfo.modify_process_identifier_for_path_param( ProcessModelInfo.modify_process_identifier_for_path_param(
@ -49,6 +46,10 @@ class GetMarkdownFileDownloadLink(Script):
) )
) )
process_instance_id = script_attributes_context.process_instance_id process_instance_id = script_attributes_context.process_instance_id
if process_instance_id is None:
raise self.get_proces_instance_id_is_missing_error(
"save_process_instance_metadata"
)
url = current_app.config["SPIFFWORKFLOW_BACKEND_URL"] url = current_app.config["SPIFFWORKFLOW_BACKEND_URL"]
url += ( url += (
f"/v1.0/process-data-file-download/{modified_process_model_identifier}/" f"/v1.0/process-data-file-download/{modified_process_model_identifier}/"

View File

@ -8,7 +8,6 @@ from spiffworkflow_backend.models.process_instance_metadata import (
from spiffworkflow_backend.models.script_attributes_context import ( from spiffworkflow_backend.models.script_attributes_context import (
ScriptAttributesContext, ScriptAttributesContext,
) )
from spiffworkflow_backend.scripts.script import ProcessInstanceIdMissingError
from spiffworkflow_backend.scripts.script import Script from spiffworkflow_backend.scripts.script import Script
@ -28,10 +27,8 @@ class SaveProcessInstanceMetadata(Script):
"""Run.""" """Run."""
metadata_dict = args[0] metadata_dict = args[0]
if script_attributes_context.process_instance_id is None: if script_attributes_context.process_instance_id is None:
raise ProcessInstanceIdMissingError( raise self.get_proces_instance_id_is_missing_error(
"The process instance id was not given to script" "save_process_instance_metadata"
" 'save_process_instance_metadata'. This script needs to be run from"
" within the context of a process instance."
) )
for key, value in metadata_dict.items(): for key, value in metadata_dict.items():
pim = ProcessInstanceMetadataModel.query.filter_by( pim = ProcessInstanceMetadataModel.query.filter_by(

View File

@ -57,6 +57,26 @@ class Script:
+ "does not properly implement the run function.", + "does not properly implement the run function.",
) )
def get_proces_instance_id_is_missing_error(
self, script_name: str
) -> ProcessInstanceIdMissingError:
"""Return the error so we can raise it from the script and mypy will be happy."""
raise ProcessInstanceIdMissingError(
"The process instance id was not given to script"
f" '{script_name}'. This script needs to be run from"
" within the context of a process instance."
)
def get_proces_model_identifier_is_missing_error(
self, script_name: str
) -> ProcessModelIdentifierMissingError:
"""Return the error so we can raise it from the script and mypy will be happy."""
return ProcessModelIdentifierMissingError(
"The process model identifier was not given to script"
f" '{script_name}'. This script needs to be run from"
" within the context of a process model."
)
@staticmethod @staticmethod
def requires_privileged_permissions() -> bool: def requires_privileged_permissions() -> bool:
"""It seems safer to default to True and make safe functions opt in for any user to run them. """It seems safer to default to True and make safe functions opt in for any user to run them.