Skip to content

Commit

Permalink
fix: add error handling (#77)
Browse files Browse the repository at this point in the history
* fix: add error handling

* chore: update node to 20.12
  • Loading branch information
ajasnosz authored Apr 10, 2024
1 parent 485065d commit fa096c7
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
cache-to: type=inline
- uses: actions/[email protected]
with:
node-version: "20.12.0"
node-version: "20.12"

build-backend:
name: build-backend
Expand Down Expand Up @@ -128,4 +128,4 @@ jobs:
cache-to: type=inline
- uses: actions/[email protected]
with:
node-version: "20.12.0"
node-version: "20.12"
2 changes: 1 addition & 1 deletion .github/workflows/ci-main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ci-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
cache-to: type=inline
- uses: actions/[email protected]
with:
node-version: "20.12.0"
node-version: "20.12"

build-backend:
name: build-backend
Expand Down Expand Up @@ -123,7 +123,7 @@ jobs:
cache-to: type=inline
- uses: actions/[email protected]
with:
node-version: "20.12.0"
node-version: "20.12"
release:
name: Release
needs: [build-frontend, build-backend]
Expand All @@ -138,7 +138,7 @@ jobs:
persist-credentials: false
- uses: actions/[email protected]
with:
node-version: "20.12.0"
node-version: "20.12"
- name: Semantic Release
id: version
uses: cycjimmy/[email protected]
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

### Changed
- add error handling for apply changes action

## [1.0.2]

### Changed
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ source venv/bin/activate
Next step is to install required `python3` packages:

```shell
cd backend
pip3 install -r requirements.txt
```

Expand All @@ -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
```

Expand Down
22 changes: 20 additions & 2 deletions backend/SC4SNMP_UI_backend/apply_changes/handling_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions backend/SC4SNMP_UI_backend/apply_changes/routes.py
Original file line number Diff line number Diff line change
@@ -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))
Expand All @@ -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
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
2 changes: 1 addition & 1 deletion frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -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 ./
Expand Down
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"packages/*"
],
"engines": {
"node": "20.12.0"
"node": "^20.12"
},
"dependencies": {
"cors": "^2.8.5",
Expand Down
6 changes: 0 additions & 6 deletions frontend/packages/manager/CHANGELOG.md

This file was deleted.

2 changes: 1 addition & 1 deletion frontend/packages/manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@
"styled-components": "5.1.1"
},
"engines": {
"node": "20.12.0"
"node": "^20.12"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit fa096c7

Please sign in to comment.