From 9888bd09ad802554e5938300a7bb81fa724c9605 Mon Sep 17 00:00:00 2001 From: burnettk Date: Tue, 11 Oct 2022 10:08:05 -0400 Subject: [PATCH] rename code to error_code --- poetry.lock | 15 ++++- src/spiffworkflow_backend/models/user.py | 2 +- .../routes/process_api_blueprint.py | 56 +++++++++---------- src/spiffworkflow_backend/routes/user.py | 27 +++++---- .../routes/user_blueprint.py | 26 +++++---- .../services/authentication_service.py | 12 ++-- .../services/error_handling_service.py | 4 +- .../services/process_instance_processor.py | 10 ++-- .../services/process_instance_service.py | 6 +- .../services/process_model_service.py | 8 +-- .../services/secret_service.py | 30 +++++----- .../services/spec_file_service.py | 4 +- .../services/user_service.py | 9 +-- .../helpers/base_test.py | 3 +- .../integration/test_secret_service.py | 4 +- 15 files changed, 119 insertions(+), 97 deletions(-) diff --git a/poetry.lock b/poetry.lock index 7317832f..d6fb15ee 100644 --- a/poetry.lock +++ b/poetry.lock @@ -639,7 +639,7 @@ werkzeug = "*" type = "git" url = "https://github.com/sartography/flask-bpmn" reference = "main" -resolved_reference = "b33362f2d62bbb4c1d1a1c992f6b057c8e71431c" +resolved_reference = "2e4b592fa36406423d69cd24018f720a4c041d8a" [[package]] name = "Flask-Cors" @@ -1858,7 +1858,7 @@ pytz = "*" type = "git" url = "https://github.com/sartography/SpiffWorkflow" reference = "main" -resolved_reference = "804889ce3b993c909ea795047dd18ea0ed6e5a99" +resolved_reference = "f30221dc6bb52753952369fc3bc5ee55ceef762f" [[package]] name = "SQLAlchemy" @@ -2905,7 +2905,18 @@ py = [ {file = "py-1.11.0.tar.gz", hash = "sha256:51c75c4126074b472f746a24399ad32f6053d1b34b68d2fa41e558e6f4a98719"}, ] pyasn1 = [ + {file = "pyasn1-0.4.8-py2.4.egg", hash = "sha256:fec3e9d8e36808a28efb59b489e4528c10ad0f480e57dcc32b4de5c9d8c9fdf3"}, + {file = "pyasn1-0.4.8-py2.5.egg", hash = "sha256:0458773cfe65b153891ac249bcf1b5f8f320b7c2ce462151f8fa74de8934becf"}, + {file = "pyasn1-0.4.8-py2.6.egg", hash = "sha256:5c9414dcfede6e441f7e8f81b43b34e834731003427e5b09e4e00e3172a10f00"}, + {file = "pyasn1-0.4.8-py2.7.egg", hash = "sha256:6e7545f1a61025a4e58bb336952c5061697da694db1cae97b116e9c46abcf7c8"}, {file = "pyasn1-0.4.8-py2.py3-none-any.whl", hash = "sha256:39c7e2ec30515947ff4e87fb6f456dfc6e84857d34be479c9d4a4ba4bf46aa5d"}, + {file = "pyasn1-0.4.8-py3.1.egg", hash = "sha256:78fa6da68ed2727915c4767bb386ab32cdba863caa7dbe473eaae45f9959da86"}, + {file = "pyasn1-0.4.8-py3.2.egg", hash = "sha256:08c3c53b75eaa48d71cf8c710312316392ed40899cb34710d092e96745a358b7"}, + {file = "pyasn1-0.4.8-py3.3.egg", hash = "sha256:03840c999ba71680a131cfaee6fab142e1ed9bbd9c693e285cc6aca0d555e576"}, + {file = "pyasn1-0.4.8-py3.4.egg", hash = "sha256:7ab8a544af125fb704feadb008c99a88805126fb525280b2270bb25cc1d78a12"}, + {file = "pyasn1-0.4.8-py3.5.egg", hash = "sha256:e89bf84b5437b532b0803ba5c9a5e054d21fec423a89952a74f87fa2c9b7bce2"}, + {file = "pyasn1-0.4.8-py3.6.egg", hash = "sha256:014c0e9976956a08139dc0712ae195324a75e142284d5f87f1a87ee1b068a359"}, + {file = "pyasn1-0.4.8-py3.7.egg", hash = "sha256:99fcc3c8d804d1bc6d9a099921e39d827026409a58f2a720dcdb89374ea0c776"}, {file = "pyasn1-0.4.8.tar.gz", hash = "sha256:aef77c9fb94a3ac588e87841208bdec464471d9871bd5050a287cc9a475cd0ba"}, ] pycodestyle = [ diff --git a/src/spiffworkflow_backend/models/user.py b/src/spiffworkflow_backend/models/user.py index b4431d0e..90d7f584 100644 --- a/src/spiffworkflow_backend/models/user.py +++ b/src/spiffworkflow_backend/models/user.py @@ -50,7 +50,7 @@ class UserModel(SpiffworkflowBaseDBModel): ap_value: str = ap_type.value return ap_value raise ApiError( - code="invalid_service", + error_code="invalid_service", message=f"Could not validate service with value: {value}", ) diff --git a/src/spiffworkflow_backend/routes/process_api_blueprint.py b/src/spiffworkflow_backend/routes/process_api_blueprint.py index 4057e8c8..b88043e3 100644 --- a/src/spiffworkflow_backend/routes/process_api_blueprint.py +++ b/src/spiffworkflow_backend/routes/process_api_blueprint.py @@ -154,7 +154,7 @@ def process_group_show( except ProcessEntityNotFoundError as exception: raise ( ApiError( - code="process_group_cannot_be_found", + error_code="process_group_cannot_be_found", message=f"Process group cannot be found: {process_group_id}", status_code=400, ) @@ -169,7 +169,7 @@ def process_model_add( process_model_info = ProcessModelInfoSchema().load(body) if process_model_info is None: raise ApiError( - code="process_model_could_not_be_created", + error_code="process_model_could_not_be_created", message=f"Process Model could not be created from given body: {body}", status_code=400, ) @@ -180,7 +180,7 @@ def process_model_add( ) if process_group is None: raise ApiError( - code="process_model_could_not_be_created", + error_code="process_model_could_not_be_created", message=f"Process Model could not be created from given body because Process Group could not be found: {body}", status_code=400, ) @@ -259,7 +259,7 @@ def get_file(process_group_id: str, process_model_id: str, file_name: str) -> An files = SpecFileService.get_files(process_model, file_name) if len(files) == 0: raise ApiError( - code="unknown file", + error_code="unknown file", message=f"No information exists for file {file_name}" f" it does not exist in workflow {process_model_id}.", status_code=404, @@ -283,7 +283,7 @@ def process_model_file_update( request_file_contents = request_file.stream.read() if not request_file_contents: raise ApiError( - code="file_contents_empty", + error_code="file_contents_empty", message="Given request file does not have any content", status_code=400, ) @@ -311,7 +311,7 @@ def process_model_file_delete( except FileNotFoundError as exception: raise ( ApiError( - code="process_model_file_cannot_be_found", + error_code="process_model_file_cannot_be_found", message=f"Process model file cannot be found: {file_name}", status_code=400, ) @@ -326,7 +326,7 @@ def add_file(process_group_id: str, process_model_id: str) -> flask.wrappers.Res request_file = get_file_from_request() if not request_file.filename: raise ApiError( - code="could_not_get_filename", + error_code="could_not_get_filename", message="Could not get filename from request", status_code=400, ) @@ -379,7 +379,7 @@ def process_instance_run( ErrorHandlingService().handle_error(processor, e) task = processor.bpmn_process_instance.last_task raise ApiError.from_task( - code="unknown_exception", + error_code="unknown_exception", message=f"An unknown error occurred. Original error: {e}", status_code=400, task=task, @@ -507,7 +507,7 @@ def message_start( if message_model is None: raise ( ApiError( - code="unknown_message", + error_code="unknown_message", message=f"Could not find message with identifier: {message_identifier}", status_code=404, ) @@ -516,7 +516,7 @@ def message_start( if "payload" not in body: raise ( ApiError( - code="missing_payload", + error_code="missing_payload", message="Body is missing payload.", status_code=400, ) @@ -538,7 +538,7 @@ def message_start( if message_instance is None: raise ( ApiError( - code="cannot_find_waiting_message", + error_code="cannot_find_waiting_message", message=f"Could not find waiting message for identifier {message_identifier} " f"and process instance {process_instance.id}", status_code=400, @@ -558,7 +558,7 @@ def message_start( if message_triggerable_process_model is None: raise ( ApiError( - code="cannot_start_message", + error_code="cannot_start_message", message=f"Message with identifier cannot be start with message: {message_identifier}", status_code=400, ) @@ -607,7 +607,7 @@ def process_instance_list( ): raise ( ApiError( - code="unexpected_condition", + error_code="unexpected_condition", message="Something went very wrong", status_code=500, ) @@ -730,7 +730,7 @@ def process_instance_report_update( ).first() if process_instance_report is None: raise ApiError( - code="unknown_process_instance_report", + error_code="unknown_process_instance_report", message="Unknown process instance report", status_code=404, ) @@ -754,7 +754,7 @@ def process_instance_report_delete( ).first() if process_instance_report is None: raise ApiError( - code="unknown_process_instance_report", + error_code="unknown_process_instance_report", message="Unknown process instance report", status_code=404, ) @@ -798,7 +798,7 @@ def process_instance_report_show( ).first() if process_instance_report is None: raise ApiError( - code="unknown_process_instance_report", + error_code="unknown_process_instance_report", message="Unknown process instance report", status_code=404, ) @@ -926,7 +926,7 @@ def task_show(process_instance_id: int, task_id: str) -> flask.wrappers.Response if not form_schema_file_name: raise ( ApiError( - code="missing_form_file", + error_code="missing_form_file", message=f"Cannot find a form file for process_instance_id: {process_instance_id}, task_id: {task_id}", status_code=500, ) @@ -987,7 +987,7 @@ def task_submit( if spiff_task.state != TaskState.READY: raise ( ApiError( - code="invalid_state", + error_code="invalid_state", message="You may not update a task unless it is in the READY state.", status_code=400, ) @@ -1041,7 +1041,7 @@ def script_unit_test_create( file = SpecFileService.get_files(process_model, process_model.primary_file_name)[0] if file is None: raise ApiError( - code="cannot_find_file", + error_code="cannot_find_file", message=f"Could not find the primary bpmn file for process_model: {process_model.id}", status_code=404, ) @@ -1063,7 +1063,7 @@ def script_unit_test_create( ) if len(script_task_elements) == 0: raise ApiError( - code="missing_script_task", + error_code="missing_script_task", message=f"Cannot find a script task with id: {bpmn_task_identifier}", status_code=404, ) @@ -1139,7 +1139,7 @@ def get_file_from_request() -> Any: request_file = connexion.request.files.get("file") if not request_file: raise ApiError( - code="no_file_given", + error_code="no_file_given", message="Given request does not contain a file", status_code=400, ) @@ -1156,7 +1156,7 @@ def get_process_model(process_model_id: str, process_group_id: str) -> ProcessMo except ProcessEntityNotFoundError as exception: raise ( ApiError( - code="process_model_cannot_be_found", + error_code="process_model_cannot_be_found", message=f"Process model cannot be found: {process_model_id}", status_code=400, ) @@ -1171,7 +1171,7 @@ def find_principal_or_raise() -> PrincipalModel: if principal is None: raise ( ApiError( - code="principal_not_found", + error_code="principal_not_found", message=f"Principal not found from user id: {g.user.id}", status_code=400, ) @@ -1195,7 +1195,7 @@ def find_active_task_by_id_or_raise( ) raise ( ApiError( - code="task_not_found", + error_code="task_not_found", message=message, status_code=400, ) @@ -1213,7 +1213,7 @@ def find_process_instance_by_id_or_raise( if process_instance is None: raise ( ApiError( - code="process_instance_cannot_be_found", + error_code="process_instance_cannot_be_found", message=f"Process instance cannot be found: {process_instance_id}", status_code=400, ) @@ -1264,7 +1264,7 @@ def get_spiff_task_from_process_instance( if spiff_task is None: raise ( ApiError( - code="empty_task", + error_code="empty_task", message="Processor failed to obtain task.", status_code=500, ) @@ -1355,7 +1355,7 @@ def _get_required_parameter_or_raise(parameter: str, post_body: dict[str, Any]) if return_value is None or return_value == "": raise ( ApiError( - code="missing_required_parameter", + error_code="missing_required_parameter", message=f"Parameter is missing from json request body: {parameter}", status_code=400, ) @@ -1386,7 +1386,7 @@ def _update_form_schema_with_task_data_as_needed( if task_data_var not in task_data: raise ( ApiError( - code="missing_task_data_var", + error_code="missing_task_data_var", message=f"Task data is missing variable: {task_data_var}", status_code=500, ) diff --git a/src/spiffworkflow_backend/routes/user.py b/src/spiffworkflow_backend/routes/user.py index 7414d5eb..e5c08d41 100644 --- a/src/spiffworkflow_backend/routes/user.py +++ b/src/spiffworkflow_backend/routes/user.py @@ -66,7 +66,7 @@ def verify_token(token: Optional[str] = None) -> Dict[str, Optional[Union[str, i except Exception as e: current_app.logger.error(f"Exception raised in get_token: {e}") raise ApiError( - code="fail_get_user_info", + error_code="fail_get_user_info", message="Cannot get user info from token", ) from e @@ -80,14 +80,14 @@ def verify_token(token: Optional[str] = None) -> Dict[str, Optional[Union[str, i ) if user_model is None: raise ApiError( - code="invalid_user", + error_code="invalid_user", message="Invalid user. Please log in.", status_code=401, ) # no user_info else: raise ApiError( - code="no_user_info", message="Cannot retrieve user info" + error_code="no_user_info", message="Cannot retrieve user info" ) else: @@ -95,7 +95,7 @@ def verify_token(token: Optional[str] = None) -> Dict[str, Optional[Union[str, i "token_type not in decode_token in verify_token" ) raise ApiError( - code="invalid_token", + error_code="invalid_token", message="Invalid token. Please log in.", status_code=401, ) @@ -110,10 +110,10 @@ def verify_token(token: Optional[str] = None) -> Dict[str, Optional[Union[str, i return {"uid": g.user.id, "sub": g.user.id, "scope": scope} # return validate_scope(token, user_info, user_model) else: - raise ApiError(code="no_user_id", message="Cannot get a user id") + raise ApiError(error_code="no_user_id", message="Cannot get a user id") raise ApiError( - code="invalid_token", message="Cannot validate token.", status_code=401 + error_code="invalid_token", message="Cannot validate token.", status_code=401 ) # no token -- do we ever get here? # else: @@ -132,7 +132,7 @@ def verify_token(token: Optional[str] = None) -> Dict[str, Optional[Union[str, i # # else: # raise ApiError( - # code="no_auth_token", + # error_code="no_auth_token", # message="No authorization token was available.", # status_code=401, # ) @@ -190,7 +190,7 @@ def encode_auth_token(sub: str, token_type: Optional[str] = None) -> str: else: current_app.logger.error("Missing SECRET_KEY in encode_auth_token") raise ApiError( - code="encode_error", message="Missing SECRET_KEY in encode_auth_token" + error_code="encode_error", message="Missing SECRET_KEY in encode_auth_token" ) return jwt.encode( payload, @@ -258,14 +258,14 @@ def login_return(code: str, state: str, session_state: str) -> Optional[Response return redirect(redirect_url) raise ApiError( - code="invalid_login", + error_code="invalid_login", message="Login failed. Please try again", status_code=401, ) else: raise ApiError( - code="invalid_token", + error_code="invalid_token", message="Login failed. Please try again", status_code=401, ) @@ -317,7 +317,9 @@ def get_decoded_token(token: str) -> Optional[Dict]: decoded_token = jwt.decode(token, options={"verify_signature": False}) except Exception as e: print(f"Exception in get_token_type: {e}") - raise ApiError(code="invalid_token", message="Cannot decode token.") from e + raise ApiError( + error_code="invalid_token", message="Cannot decode token." + ) from e else: if "token_type" in decoded_token or "iss" in decoded_token: return decoded_token @@ -326,7 +328,8 @@ def get_decoded_token(token: str) -> Optional[Dict]: f"Unknown token type in get_decoded_token: token: {token}" ) raise ApiError( - code="unknown_token", message="Unknown token type in get_decoded_token" + error_code="unknown_token", + message="Unknown token type in get_decoded_token", ) # try: # # see if we have an open_id token diff --git a/src/spiffworkflow_backend/routes/user_blueprint.py b/src/spiffworkflow_backend/routes/user_blueprint.py index da28af92..29bbddcd 100644 --- a/src/spiffworkflow_backend/routes/user_blueprint.py +++ b/src/spiffworkflow_backend/routes/user_blueprint.py @@ -31,7 +31,7 @@ user_blueprint = Blueprint("main", __name__) # if user is not None: # raise ( # ApiError( -# code="user_already_exists", +# error_code="user_already_exists", # message=f"User already exists: {username}", # status_code=409, # ) @@ -45,7 +45,7 @@ user_blueprint = Blueprint("main", __name__) # db.session.add(user) # except IntegrityError as exception: # raise ( -# ApiError(code="integrity_error", message=repr(exception), status_code=500) +# ApiError(error_code="integrity_error", message=repr(exception), status_code=500) # ) from exception # # try: @@ -68,7 +68,7 @@ def delete_user(username: str) -> flask.wrappers.Response: if user is None: raise ( ApiError( - code="user_cannot_be_found", + error_code="user_cannot_be_found", message=f"User cannot be found: {username}", status_code=400, ) @@ -87,7 +87,7 @@ def create_group(group_name: str) -> flask.wrappers.Response: if group is not None: raise ( ApiError( - code="group_already_exists", + error_code="group_already_exists", message=f"Group already exists: {group_name}", status_code=409, ) @@ -98,7 +98,9 @@ def create_group(group_name: str) -> flask.wrappers.Response: db.session.add(group) except IntegrityError as exception: raise ( - ApiError(code="integrity_error", message=repr(exception), status_code=500) + ApiError( + error_code="integrity_error", message=repr(exception), status_code=500 + ) ) from exception db.session.commit() @@ -112,7 +114,7 @@ def delete_group(group_name: str) -> flask.wrappers.Response: if group is None: raise ( ApiError( - code="group_cannot_be_found", + error_code="group_cannot_be_found", message=f"Group cannot be found: {group_name}", status_code=400, ) @@ -136,7 +138,7 @@ def assign_user_to_group() -> flask.wrappers.Response: if user_group_assignment is not None: raise ( ApiError( - code="user_is_already_in_group", + error_code="user_is_already_in_group", message=f"User ({user.id}) is already in group ({group.id})", status_code=409, ) @@ -165,7 +167,7 @@ def remove_user_from_group() -> flask.wrappers.Response: if user_group_assignment is None: raise ( ApiError( - code="user_not_in_group", + error_code="user_not_in_group", message=f"User ({user.id}) is not in group ({group.id})", status_code=400, ) @@ -195,7 +197,7 @@ def get_user_from_request() -> Any: if user_id is None: raise ( ApiError( - code="user_id_is_required", + error_code="user_id_is_required", message="Attribute user_id is required", status_code=400, ) @@ -205,7 +207,7 @@ def get_user_from_request() -> Any: if user is None: raise ( ApiError( - code="user_cannot_be_found", + error_code="user_cannot_be_found", message=f"User cannot be found: {user_id}", status_code=400, ) @@ -220,7 +222,7 @@ def get_group_from_request() -> Any: if group_id is None: raise ( ApiError( - code="group_id_is_required", + error_code="group_id_is_required", message="Attribute group_id is required", status_code=400, ) @@ -230,7 +232,7 @@ def get_group_from_request() -> Any: if group is None: raise ( ApiError( - code="group_cannot_be_found", + error_code="group_cannot_be_found", message=f"Group cannot be found: {group_id}", status_code=400, ) diff --git a/src/spiffworkflow_backend/services/authentication_service.py b/src/spiffworkflow_backend/services/authentication_service.py index 3cfbb8c1..73559590 100644 --- a/src/spiffworkflow_backend/services/authentication_service.py +++ b/src/spiffworkflow_backend/services/authentication_service.py @@ -66,21 +66,21 @@ class PublicAuthenticationService: except Exception as e: current_app.logger.error(f"Exception in get_user_info_from_id_token: {e}") raise ApiError( - code="token_error", + error_code="token_error", message=f"Exception in get_user_info_from_id_token: {e}", status_code=401, ) from e if request_response.status_code == 401: raise ApiError( - code="invalid_token", message="Please login", status_code=401 + error_code="invalid_token", message="Please login", status_code=401 ) elif request_response.status_code == 200: user_info: dict = json.loads(request_response.text) return user_info raise ApiError( - code="user_info_error", + error_code="user_info_error", message="Cannot get user info in get_user_info_from_id_token", status_code=401, ) @@ -180,7 +180,9 @@ class PublicAuthenticationService: decoded_token = jwt.decode(id_token, options={"verify_signature": False}) except Exception as e: raise ApiError( - code="bad_id_token", message="Cannot decode id_token", status_code=401 + error_code="bad_id_token", + message="Cannot decode id_token", + status_code=401, ) from e if decoded_token["iss"] != f"{open_id_server_url}/realms/{open_id_realm_name}": valid = False @@ -203,7 +205,7 @@ class PublicAuthenticationService: if now > decoded_token["exp"]: raise ApiError( - code="invalid_token", + error_code="invalid_token", message="Your token is expired. Please Login", status_code=401, ) diff --git a/src/spiffworkflow_backend/services/error_handling_service.py b/src/spiffworkflow_backend/services/error_handling_service.py index f14585cf..36c66d93 100644 --- a/src/spiffworkflow_backend/services/error_handling_service.py +++ b/src/spiffworkflow_backend/services/error_handling_service.py @@ -108,4 +108,6 @@ class FailingService: @staticmethod def fail_as_service() -> None: """It fails.""" - raise ApiError(code="failing_service", message="This is my failing service") + raise ApiError( + error_code="failing_service", message="This is my failing service" + ) diff --git a/src/spiffworkflow_backend/services/process_instance_processor.py b/src/spiffworkflow_backend/services/process_instance_processor.py index 5b2a162f..e4c7d6b2 100644 --- a/src/spiffworkflow_backend/services/process_instance_processor.py +++ b/src/spiffworkflow_backend/services/process_instance_processor.py @@ -313,7 +313,7 @@ class ProcessInstanceProcessor: except MissingSpecError as ke: raise ApiError( - code="unexpected_process_instance_structure", + error_code="unexpected_process_instance_structure", message="Failed to deserialize process_instance" " '%s' due to a mis-placed or missing task '%s'" % (self.process_model_identifier, str(ke)), @@ -519,7 +519,7 @@ class ProcessInstanceProcessor: if principal is None: raise ( ApiError( - code="principal_not_found", + error_code="principal_not_found", message=f"Principal not found from user id: {user_id}", status_code=400, ) @@ -621,7 +621,7 @@ class ProcessInstanceProcessor: if bpmn_file_full_path is None: raise ( ApiError( - code="could_not_find_bpmn_process_identifier", + error_code="could_not_find_bpmn_process_identifier", message="Could not find the the given bpmn process identifier from any sources: %s" % bpmn_process_identifier, ) @@ -684,7 +684,7 @@ class ProcessInstanceProcessor: ): raise ( ApiError( - code="no_primary_bpmn_error", + error_code="no_primary_bpmn_error", message="There is no primary BPMN process id defined for process_model %s" % process_model_info.id, ) @@ -702,7 +702,7 @@ class ProcessInstanceProcessor: ) except ValidationException as ve: raise ApiError( - code="process_instance_validation_error", + error_code="process_instance_validation_error", message="Failed to parse the Workflow Specification. " + "Error is '%s.'" % str(ve), file_name=ve.filename, diff --git a/src/spiffworkflow_backend/services/process_instance_service.py b/src/spiffworkflow_backend/services/process_instance_service.py index a53dadec..c15ed5b1 100644 --- a/src/spiffworkflow_backend/services/process_instance_service.py +++ b/src/spiffworkflow_backend/services/process_instance_service.py @@ -213,7 +213,7 @@ class ProcessInstanceService: """Get_users_assigned_to_task.""" if processor.process_instance_model.process_initiator_id is None: raise ApiError.from_task( - code="invalid_workflow", + error_code="invalid_workflow", message="A process instance must have a user_id.", task=spiff_task, ) @@ -246,7 +246,7 @@ class ProcessInstanceService: lane_uids.append(user["value"]) else: raise ApiError.from_task( - code="task_lane_user_error", + error_code="task_lane_user_error", message="Spiff Task %s lane user dict must have a key called 'value' with the user's uid in it." % spiff_task.task_spec.name, task=spiff_task, @@ -255,7 +255,7 @@ class ProcessInstanceService: lane_uids.append(user) else: raise ApiError.from_task( - code="task_lane_user_error", + error_code="task_lane_user_error", message="Spiff Task %s lane user is not a string or dict" % spiff_task.task_spec.name, task=spiff_task, diff --git a/src/spiffworkflow_backend/services/process_model_service.py b/src/spiffworkflow_backend/services/process_model_service.py index 20b00e7f..c48c9d98 100644 --- a/src/spiffworkflow_backend/services/process_model_service.py +++ b/src/spiffworkflow_backend/services/process_model_service.py @@ -83,7 +83,7 @@ class ProcessModelService(FileSystemService): ).all() if len(instances) > 0: raise ApiError( - code="existing_instances", + error_code="existing_instances", message=f"We cannot delete the model `{process_model_id}`, there are existing instances that depend on it.", ) process_model = self.get_process_model(process_model_id) @@ -260,7 +260,7 @@ class ProcessModelService(FileSystemService): process_group = ProcessGroup(**data) if process_group is None: raise ApiError( - code="process_group_could_not_be_loaded_from_disk", + error_code="process_group_could_not_be_loaded_from_disk", message=f"We could not load the process_group from disk from: {dir_item}", ) else: @@ -300,13 +300,13 @@ class ProcessModelService(FileSystemService): spec = ProcessModelInfo(**data) if spec is None: raise ApiError( - code="process_model_could_not_be_loaded_from_disk", + error_code="process_model_could_not_be_loaded_from_disk", message=f"We could not load the process_model from disk with data: {data}", ) else: if name is None: raise ApiError( - code="missing_name_of_process_model", + error_code="missing_name_of_process_model", message="Missing name of process model. It should be given", ) diff --git a/src/spiffworkflow_backend/services/secret_service.py b/src/spiffworkflow_backend/services/secret_service.py index 6add9d79..214bf2a3 100644 --- a/src/spiffworkflow_backend/services/secret_service.py +++ b/src/spiffworkflow_backend/services/secret_service.py @@ -47,7 +47,7 @@ class SecretService: db.session.commit() except Exception as e: raise ApiError( - code="create_secret_error", + error_code="create_secret_error", message=f"There was an error creating a secret with key: {key} and value ending with: {value[:-4]}. " f"Original error is {e}", ) from e @@ -63,7 +63,7 @@ class SecretService: return secret else: raise ApiError( - code="missing_secret_error", + error_code="missing_secret_error", message=f"Unable to locate a secret with the name: {key}. ", ) @@ -83,18 +83,18 @@ class SecretService: db.session.commit() except Exception as e: raise ApiError( - code="update_secret_error", + error_code="update_secret_error", message=f"There was an error updating the secret with key: {key}, and value: {value}", ) from e else: raise ApiError( - code="update_secret_error", + error_code="update_secret_error", message=f"User: {creator_user_id} cannot update the secret with key : {key}", status_code=401, ) else: raise ApiError( - code="update_secret_error", + error_code="update_secret_error", message=f"Cannot update secret with key: {key}. Resource does not exist.", status_code=404, ) @@ -110,18 +110,18 @@ class SecretService: db.session.commit() except Exception as e: raise ApiError( - code="delete_secret_error", + error_code="delete_secret_error", message=f"Could not delete secret with key: {key}. Original error is: {e}", ) from e else: raise ApiError( - code="delete_secret_error", + error_code="delete_secret_error", message=f"User: {user_id} cannot delete the secret with key : {key}", status_code=401, ) else: raise ApiError( - code="delete_secret_error", + error_code="delete_secret_error", message=f"Cannot delete secret with key: {key}. Resource does not exist.", status_code=404, ) @@ -145,7 +145,7 @@ class SecretService: except IntegrityError as ie: db.session.rollback() raise ApiError( - code="add_allowed_process_error", + error_code="add_allowed_process_error", message=f"Error adding allowed_process with secret {secret_model.id}, " f"and path: {allowed_relative_path}. Resource already exists. " f"Original error is {ie}", @@ -155,7 +155,7 @@ class SecretService: # TODO: should we call db.session.rollback() here? # db.session.rollback() raise ApiError( - code="add_allowed_process_error", + error_code="add_allowed_process_error", message=f"Could not create an allowed process for secret with key: {secret_model.key} " f"with path: {allowed_relative_path}. " f"Original error is {e}", @@ -163,13 +163,13 @@ class SecretService: return secret_process_model else: raise ApiError( - code="add_allowed_process_error", + error_code="add_allowed_process_error", message=f"User: {user_id} cannot modify the secret with key : {secret_model.key}", status_code=401, ) else: raise ApiError( - code="add_allowed_process_error", + error_code="add_allowed_process_error", message=f"Cannot add allowed process to secret with key: {secret_id}. Resource does not exist.", status_code=404, ) @@ -191,19 +191,19 @@ class SecretService: db.session.commit() except Exception as e: raise ApiError( - code="delete_allowed_process_error", + error_code="delete_allowed_process_error", message=f"There was an exception deleting allowed_process: {allowed_process_id}. " f"Original error is: {e}", ) from e else: raise ApiError( - code="delete_allowed_process_error", + error_code="delete_allowed_process_error", message=f"User: {user_id} cannot delete the allowed_process with id : {allowed_process_id}", status_code=401, ) else: raise ApiError( - code="delete_allowed_process_error", + error_code="delete_allowed_process_error", message=f"Cannot delete allowed_process: {allowed_process_id}. Resource does not exist.", status_code=404, ) diff --git a/src/spiffworkflow_backend/services/spec_file_service.py b/src/spiffworkflow_backend/services/spec_file_service.py index db77ce9e..d591d46a 100644 --- a/src/spiffworkflow_backend/services/spec_file_service.py +++ b/src/spiffworkflow_backend/services/spec_file_service.py @@ -217,12 +217,12 @@ class SpecFileService(FileSystemService): except ValidationException as ve: if ve.args[0].find("No executable process tag found") >= 0: raise ApiError( - code="missing_executable_option", + error_code="missing_executable_option", message="No executable process tag found. Please make sure the Executable option is set in the workflow.", ) from ve else: raise ApiError( - code="validation_error", + error_code="validation_error", message=f"There was an error validating your workflow. Original message is: {ve}", ) from ve else: diff --git a/src/spiffworkflow_backend/services/user_service.py b/src/spiffworkflow_backend/services/user_service.py index 3698554b..b755fc6e 100644 --- a/src/spiffworkflow_backend/services/user_service.py +++ b/src/spiffworkflow_backend/services/user_service.py @@ -47,7 +47,8 @@ class UserService: except Exception as e: db.session.rollback() raise ApiError( - code="add_user_error", message=f"Could not add user {username}" + error_code="add_user_error", + message=f"Could not add user {username}", ) from e self.create_principal(user_model.id) return user_model @@ -58,7 +59,7 @@ class UserService: # Don't really want to send service_id. raise ( ApiError( - code="user_already_exists", + error_code="user_already_exists", message=f"User already exists: {username}", status_code=409, ) @@ -260,7 +261,7 @@ class UserService: if principal: return principal raise ApiError( - code="no_principal_found", + error_code="no_principal_found", message=f"No principal was found for user_id: {user_id}", ) @@ -278,7 +279,7 @@ class UserService: db.session.rollback() current_app.logger.error(f"Exception in create_principal: {e}") raise ApiError( - code="add_principal_error", + error_code="add_principal_error", message=f"Could not create principal {user_id}", ) from e return principal diff --git a/tests/spiffworkflow_backend/helpers/base_test.py b/tests/spiffworkflow_backend/helpers/base_test.py index 145a93c7..4e8de580 100644 --- a/tests/spiffworkflow_backend/helpers/base_test.py +++ b/tests/spiffworkflow_backend/helpers/base_test.py @@ -43,7 +43,8 @@ class BaseTest: return user raise ApiError( - code="create_user_error", message=f"Cannot find or create user: {username}" + error_code="create_user_error", + message=f"Cannot find or create user: {username}", ) @staticmethod diff --git a/tests/spiffworkflow_backend/integration/test_secret_service.py b/tests/spiffworkflow_backend/integration/test_secret_service.py index 41ea675c..94637fac 100644 --- a/tests/spiffworkflow_backend/integration/test_secret_service.py +++ b/tests/spiffworkflow_backend/integration/test_secret_service.py @@ -95,7 +95,7 @@ class TestSecretService(SecretServiceTestHelpers): self.add_test_secret(user) with pytest.raises(ApiError) as ae: self.add_test_secret(user) - assert ae.value.code == "create_secret_error" + assert ae.value.error_code == "create_secret_error" def test_get_secret(self, app: Flask, with_db_and_bpmn_file_cleanup: None) -> None: """Test_get_secret.""" @@ -154,7 +154,7 @@ class TestSecretService(SecretServiceTestHelpers): with pytest.raises(ApiError) as ae: SecretService.update_secret(secret.key + "x", "some_new_value", user.id) assert "Resource does not exist" in ae.value.message - assert ae.value.code == "update_secret_error" + assert ae.value.error_code == "update_secret_error" def test_delete_secret( self, app: Flask, client: FlaskClient, with_db_and_bpmn_file_cleanup: None