From d91d283657da1ad4f72450d6871e3332f6f6ec7a Mon Sep 17 00:00:00 2001 From: ajasnosz Date: Tue, 9 Apr 2024 15:07:40 +0200 Subject: [PATCH] fix: add error handling --- CHANGELOG.md | 5 +++++ README.md | 2 +- .../apply_changes/handling_chain.py | 22 +++++++++++++++++-- .../apply_changes/routes.py | 17 ++++++++++++-- frontend/Dockerfile | 2 +- frontend/package.json | 2 +- frontend/packages/manager/CHANGELOG.md | 6 ----- .../src/components/menu_header/Header.jsx | 6 +++++ 8 files changed, 49 insertions(+), 13 deletions(-) delete mode 100644 frontend/packages/manager/CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 56d705e..223178b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## Unreleased + +### Changed +- add error handling for apply changes action + ## [1.0.2] ### Changed diff --git a/README.md b/README.md index 0542b25..603f32e 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,7 @@ source venv/bin/activate Next step is to install required `python3` packages: ```shell +cd backend pip3 install -r requirements.txt ``` @@ -80,7 +81,6 @@ docker run --rm -d -p 27017:27017 --name example-mongo mongo:4.4.6 To start backend service run: ```yaml -cd backend flask run ``` diff --git a/backend/SC4SNMP_UI_backend/apply_changes/handling_chain.py b/backend/SC4SNMP_UI_backend/apply_changes/handling_chain.py index fd8e51a..595933c 100644 --- a/backend/SC4SNMP_UI_backend/apply_changes/handling_chain.py +++ b/backend/SC4SNMP_UI_backend/apply_changes/handling_chain.py @@ -18,6 +18,17 @@ mongo_inventory = mongo_client.sc4snmp.inventory_ui mongo_profiles = mongo_client.sc4snmp.profiles_ui + +class EmptyValuesFileException(Exception): + def __init__(self, filename): + self.message = f"{filename} cannot be empty. Check sc4snmp documentation for template." + super().__init__(self.message) + +class YamlParserException(Exception): + def __init__(self, filename): + self.message = f"Error occurred while reading {filename}. Check yaml syntax." + super().__init__(self.message) + class Handler(ABC): @abstractmethod def set_next(self, handler): @@ -71,8 +82,15 @@ def handle(self, request: dict): values_file_resolved = False values = {} if values_file_resolved: - with open(values_file_path, "r") as file: - values = yaml.load(file) + try: + with open(values_file_path, "r") as file: + values = yaml.load(file) + except ruamel.yaml.parser.ParserError as e: + current_app.logger.error(f"Error occurred while reading {VALUES_FILE}. Check yaml syntax.") + raise YamlParserException(VALUES_FILE) + if values is None: + current_app.logger.error(f"{VALUES_FILE} cannot be empty. Check sc4snmp documentation for template.") + raise EmptyValuesFileException(VALUES_FILE) if not values_file_resolved or KEEP_TEMP_FILES.lower() in ["t", "true", "y", "yes", "1"]: delete_temp_files = False diff --git a/backend/SC4SNMP_UI_backend/apply_changes/routes.py b/backend/SC4SNMP_UI_backend/apply_changes/routes.py index c6089f4..88c769d 100644 --- a/backend/SC4SNMP_UI_backend/apply_changes/routes.py +++ b/backend/SC4SNMP_UI_backend/apply_changes/routes.py @@ -1,7 +1,9 @@ -from flask import Blueprint, jsonify +from flask import Blueprint, jsonify, current_app from flask_cors import cross_origin from SC4SNMP_UI_backend.apply_changes.apply_changes import ApplyChanges +from SC4SNMP_UI_backend.apply_changes.handling_chain import EmptyValuesFileException, YamlParserException import os +import traceback apply_changes_blueprint = Blueprint('common_blueprint', __name__) JOB_CREATION_RETRIES = int(os.getenv("JOB_CREATION_RETRIES", 10)) @@ -19,4 +21,15 @@ def apply_changes(): else: message = f"Configuration will be updated in approximately {job_delay} seconds." result = jsonify({"message": message}) - return result, 200 \ No newline at end of file + return result, 200 + +@apply_changes_blueprint.errorhandler(Exception) +@cross_origin() +def handle_exception(e): + current_app.logger.error(traceback.format_exc()) + if isinstance(e, (EmptyValuesFileException, YamlParserException)): + result = jsonify({"message": e.message}) + return result, 400 + + result = jsonify({"message": "Undentified error. Check logs."}) + return result, 400 \ No newline at end of file diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 7a9238c..a12a7da 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -1,4 +1,4 @@ -FROM node:20.12.0-alpine as build-step +FROM node:20.12-alpine as build-step WORKDIR /frontend ENV PATH /frontend/node_modules/.bin:$PATH COPY package.json yarn.lock lerna.json ./ diff --git a/frontend/package.json b/frontend/package.json index e011eb4..d8836ac 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -19,7 +19,7 @@ "packages/*" ], "engines": { - "node": "20.12.0" + "node": "^20.12" }, "dependencies": { "cors": "^2.8.5", diff --git a/frontend/packages/manager/CHANGELOG.md b/frontend/packages/manager/CHANGELOG.md deleted file mode 100644 index ec5626d..0000000 --- a/frontend/packages/manager/CHANGELOG.md +++ /dev/null @@ -1,6 +0,0 @@ -# Change Log - -0.0.1 – Release date: TBA -------- - -* Initial version diff --git a/frontend/packages/manager/src/components/menu_header/Header.jsx b/frontend/packages/manager/src/components/menu_header/Header.jsx index e62a0b2..a8b83e1 100644 --- a/frontend/packages/manager/src/components/menu_header/Header.jsx +++ b/frontend/packages/manager/src/components/menu_header/Header.jsx @@ -54,6 +54,12 @@ function Header(){ ErrCtx.setMessage(response.data.message); } }) + .catch((error) => { + console.log(error) + ErrCtx.setOpen(true); + ErrCtx.setErrorType("error"); + ErrCtx.setMessage("Error: " + error.response.data.message); + }) }; const addButtonLabel = {