From fa096c7af55644995107889adb0f332073bbca5d Mon Sep 17 00:00:00 2001 From: ajasnosz <139114006+ajasnosz@users.noreply.github.com> Date: Wed, 10 Apr 2024 13:59:03 +0200 Subject: [PATCH] fix: add error handling (#77) * fix: add error handling * chore: update node to 20.12 --- .github/workflows/ci-build.yaml | 4 ++-- .github/workflows/ci-main.yaml | 2 +- .github/workflows/ci-release.yaml | 6 ++--- 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 ----- frontend/packages/manager/package.json | 2 +- .../src/components/menu_header/Header.jsx | 6 +++++ 12 files changed, 56 insertions(+), 20 deletions(-) delete mode 100644 frontend/packages/manager/CHANGELOG.md diff --git a/.github/workflows/ci-build.yaml b/.github/workflows/ci-build.yaml index 07928f2..02583c5 100644 --- a/.github/workflows/ci-build.yaml +++ b/.github/workflows/ci-build.yaml @@ -78,7 +78,7 @@ jobs: cache-to: type=inline - uses: actions/setup-node@v4.0.2 with: - node-version: "20.12.0" + node-version: "20.12" build-backend: name: build-backend @@ -128,4 +128,4 @@ jobs: cache-to: type=inline - uses: actions/setup-node@v4.0.2 with: - node-version: "20.12.0" + node-version: "20.12" diff --git a/.github/workflows/ci-main.yaml b/.github/workflows/ci-main.yaml index 6e2a183..2fde50d 100644 --- a/.github/workflows/ci-main.yaml +++ b/.github/workflows/ci-main.yaml @@ -56,7 +56,7 @@ jobs: strategy: matrix: node-version: - - 20.12.0 + - 20.12 steps: - uses: actions/checkout@v4 - name: Set Node.js ${{ matrix.node-version }} diff --git a/.github/workflows/ci-release.yaml b/.github/workflows/ci-release.yaml index 9b65c59..f5ef5e4 100644 --- a/.github/workflows/ci-release.yaml +++ b/.github/workflows/ci-release.yaml @@ -73,7 +73,7 @@ jobs: cache-to: type=inline - uses: actions/setup-node@v4.0.2 with: - node-version: "20.12.0" + node-version: "20.12" build-backend: name: build-backend @@ -123,7 +123,7 @@ jobs: cache-to: type=inline - uses: actions/setup-node@v4.0.2 with: - node-version: "20.12.0" + node-version: "20.12" release: name: Release needs: [build-frontend, build-backend] @@ -138,7 +138,7 @@ jobs: persist-credentials: false - uses: actions/setup-node@v4.0.2 with: - node-version: "20.12.0" + node-version: "20.12" - name: Semantic Release id: version uses: cycjimmy/semantic-release-action@v4.1.0 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/package.json b/frontend/packages/manager/package.json index cf728e4..f11c552 100644 --- a/frontend/packages/manager/package.json +++ b/frontend/packages/manager/package.json @@ -77,6 +77,6 @@ "styled-components": "5.1.1" }, "engines": { - "node": "20.12.0" + "node": "^20.12" } } 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 = {