From 1fc8d441adee50935ce87f28bb6f29773ef631bd Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 1 Feb 2024 18:19:31 +0100 Subject: [PATCH] Removed fatal from db error handling & improved web error handling --- bot/src/bot_api/api.py | 4 +-- .../controller/auth_discord_controller.py | 2 +- bot/src/bot_api/exception/__init__.py | 26 ------------------- bot/src/bot_api/model/error_dto.py | 5 +--- bot/src/bot_api/route/route.py | 4 +-- bot/src/bot_api/service/auth_service.py | 4 +-- bot/src/bot_api/service/discord_service.py | 4 +-- .../exception/service_error_code_enum.py | 0 .../exception/service_exception.py | 2 +- bot/src/bot_data/db_context.py | 10 ++++++- bot/src/bot_graphql/abc/query_abc.py | 4 +-- .../dashboard/dashboard.component.ts | 6 +---- .../error-handler/error-handler.service.ts | 12 +++++---- 13 files changed, 30 insertions(+), 53 deletions(-) delete mode 100644 bot/src/bot_api/exception/__init__.py rename bot/src/{bot_api => bot_core}/exception/service_error_code_enum.py (100%) rename bot/src/{bot_api => bot_core}/exception/service_exception.py (83%) diff --git a/bot/src/bot_api/api.py b/bot/src/bot_api/api.py index 84184695..c27ad0bf 100644 --- a/bot/src/bot_api/api.py +++ b/bot/src/bot_api/api.py @@ -16,8 +16,8 @@ from werkzeug.exceptions import NotFound from bot_api.configuration.api_settings import ApiSettings from bot_api.configuration.authentication_settings import AuthenticationSettings -from bot_api.exception.service_error_code_enum import ServiceErrorCode -from bot_api.exception.service_exception import ServiceException +from bot_core.exception.service_error_code_enum import ServiceErrorCode +from bot_core.exception.service_exception import ServiceException from bot_api.logging.api_logger import ApiLogger from bot_api.model.error_dto import ErrorDTO from bot_api.route.route import Route diff --git a/bot/src/bot_api/controller/auth_discord_controller.py b/bot/src/bot_api/controller/auth_discord_controller.py index 6c3b1d0f..24d37afe 100644 --- a/bot/src/bot_api/controller/auth_discord_controller.py +++ b/bot/src/bot_api/controller/auth_discord_controller.py @@ -16,7 +16,7 @@ from bot_api.api import Api from bot_api.configuration.discord_authentication_settings import ( DiscordAuthenticationSettings, ) -from bot_api.exception.service_exception import ServiceException +from bot_core.exception.service_exception import ServiceException from bot_api.logging.api_logger import ApiLogger from bot_api.model.auth_user_dto import AuthUserDTO from bot_api.route.route import Route diff --git a/bot/src/bot_api/exception/__init__.py b/bot/src/bot_api/exception/__init__.py deleted file mode 100644 index a528b24c..00000000 --- a/bot/src/bot_api/exception/__init__.py +++ /dev/null @@ -1,26 +0,0 @@ -# -*- coding: utf-8 -*- - -""" -bot sh-edraft.de Discord bot -~~~~~~~~~~~~~~~~~~~ - -Discord bot for customers of sh-edraft.de - -:copyright: (c) 2022 - 2023 sh-edraft.de -:license: MIT, see LICENSE for more details. - -""" - -__title__ = "bot_api.exception" -__author__ = "Sven Heidemann" -__license__ = "MIT" -__copyright__ = "Copyright (c) 2022 - 2023 sh-edraft.de" -__version__ = "1.2.6" - -from collections import namedtuple - - -# imports: - -VersionInfo = namedtuple("VersionInfo", "major minor micro") -version_info = VersionInfo(major="1", minor="2", micro="6") diff --git a/bot/src/bot_api/model/error_dto.py b/bot/src/bot_api/model/error_dto.py index 867e581c..d24d7fdb 100644 --- a/bot/src/bot_api/model/error_dto.py +++ b/bot/src/bot_api/model/error_dto.py @@ -1,10 +1,7 @@ -import traceback from typing import Optional -from cpl_core.console import Console - from bot_api.abc.dto_abc import DtoABC -from bot_api.exception.service_error_code_enum import ServiceErrorCode +from bot_core.exception.service_error_code_enum import ServiceErrorCode class ErrorDTO(DtoABC): diff --git a/bot/src/bot_api/route/route.py b/bot/src/bot_api/route/route.py index a72b28e3..2b283b14 100644 --- a/bot/src/bot_api/route/route.py +++ b/bot/src/bot_api/route/route.py @@ -8,8 +8,8 @@ from flask import request, jsonify from flask_cors import cross_origin from bot_api.abc.auth_service_abc import AuthServiceABC -from bot_api.exception.service_error_code_enum import ServiceErrorCode -from bot_api.exception.service_exception import ServiceException +from bot_core.exception.service_error_code_enum import ServiceErrorCode +from bot_core.exception.service_exception import ServiceException from bot_api.model.error_dto import ErrorDTO from bot_data.abc.auth_user_repository_abc import AuthUserRepositoryABC from bot_data.model.auth_role_enum import AuthRoleEnum diff --git a/bot/src/bot_api/service/auth_service.py b/bot/src/bot_api/service/auth_service.py index a38fb903..661ca15d 100644 --- a/bot/src/bot_api/service/auth_service.py +++ b/bot/src/bot_api/service/auth_service.py @@ -18,8 +18,8 @@ from flask import request from bot_api.abc.auth_service_abc import AuthServiceABC from bot_api.configuration.authentication_settings import AuthenticationSettings from bot_api.configuration.frontend_settings import FrontendSettings -from bot_api.exception.service_error_code_enum import ServiceErrorCode -from bot_api.exception.service_exception import ServiceException +from bot_core.exception.service_error_code_enum import ServiceErrorCode +from bot_core.exception.service_exception import ServiceException from bot_api.filter.auth_user_select_criteria import AuthUserSelectCriteria from bot_api.logging.api_logger import ApiLogger from bot_api.model.auth_user_dto import AuthUserDTO diff --git a/bot/src/bot_api/service/discord_service.py b/bot/src/bot_api/service/discord_service.py index 386b3046..0e05cc6c 100644 --- a/bot/src/bot_api/service/discord_service.py +++ b/bot/src/bot_api/service/discord_service.py @@ -4,8 +4,8 @@ from cpl_discord.service import DiscordBotServiceABC from cpl_query.extension import List from bot_api.abc.auth_service_abc import AuthServiceABC -from bot_api.exception.service_error_code_enum import ServiceErrorCode -from bot_api.exception.service_exception import ServiceException +from bot_core.exception.service_error_code_enum import ServiceErrorCode +from bot_core.exception.service_exception import ServiceException from bot_api.filter.discord.server_select_criteria import ServerSelectCriteria from bot_api.model.discord.server_dto import ServerDTO from bot_api.model.discord.server_filtered_result_dto import ServerFilteredResultDTO diff --git a/bot/src/bot_api/exception/service_error_code_enum.py b/bot/src/bot_core/exception/service_error_code_enum.py similarity index 100% rename from bot/src/bot_api/exception/service_error_code_enum.py rename to bot/src/bot_core/exception/service_error_code_enum.py diff --git a/bot/src/bot_api/exception/service_exception.py b/bot/src/bot_core/exception/service_exception.py similarity index 83% rename from bot/src/bot_api/exception/service_exception.py rename to bot/src/bot_core/exception/service_exception.py index 0b0ed95d..2292afe9 100644 --- a/bot/src/bot_api/exception/service_exception.py +++ b/bot/src/bot_core/exception/service_exception.py @@ -1,4 +1,4 @@ -from bot_api.exception.service_error_code_enum import ServiceErrorCode +from bot_core.exception.service_error_code_enum import ServiceErrorCode class ServiceException(Exception): diff --git a/bot/src/bot_data/db_context.py b/bot/src/bot_data/db_context.py index cd5cf343..b8f1a676 100644 --- a/bot/src/bot_data/db_context.py +++ b/bot/src/bot_data/db_context.py @@ -1,8 +1,11 @@ import time +import uuid from cpl_core.database import DatabaseSettings from cpl_core.database.context import DatabaseContext +from bot_core.exception.service_error_code_enum import ServiceErrorCode +from bot_core.exception.service_exception import ServiceException from bot_core.logging.database_logger import DatabaseLogger @@ -34,7 +37,12 @@ class DBContext(DatabaseContext): return super(DBContext, self).select(statement) except Exception as e: if self._fails >= 3: - self._logger.fatal(__name__, f"Database error caused by {statement}", e) + self._logger.error(__name__, f"Database error caused by {statement}", e) + uid = uuid.uuid4() + raise ServiceException( + ServiceErrorCode.Unknown, + f"Query failed three times with {type(e).__name__}. Contact an admin and give them the UID: {uid}", + ) self._logger.error(__name__, f"Database error caused by {statement}", e) self._fails += 1 diff --git a/bot/src/bot_graphql/abc/query_abc.py b/bot/src/bot_graphql/abc/query_abc.py index 71d9b267..18808265 100644 --- a/bot/src/bot_graphql/abc/query_abc.py +++ b/bot/src/bot_graphql/abc/query_abc.py @@ -7,8 +7,8 @@ from cpl_core.type import T from cpl_discord.service import DiscordBotServiceABC from cpl_query.extension import List -from bot_api.exception.service_error_code_enum import ServiceErrorCode -from bot_api.exception.service_exception import ServiceException +from bot_core.exception.service_error_code_enum import ServiceErrorCode +from bot_core.exception.service_exception import ServiceException from bot_api.route.route import Route from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from bot_core.environment_variables import MAINTENANCE diff --git a/web/src/app/modules/view/dashboard/components/dashboard/dashboard.component.ts b/web/src/app/modules/view/dashboard/components/dashboard/dashboard.component.ts index bf1e7a72..89259635 100644 --- a/web/src/app/modules/view/dashboard/components/dashboard/dashboard.component.ts +++ b/web/src/app/modules/view/dashboard/components/dashboard/dashboard.component.ts @@ -1,19 +1,15 @@ import { Component, OnDestroy, OnInit } from "@angular/core"; import { FormBuilder, FormControl, FormGroup } from "@angular/forms"; import { Router } from "@angular/router"; -import { TranslateService } from "@ngx-translate/core"; import { debounceTime, Subject, throwError } from "rxjs"; -import { ConfirmationDialogService } from "src/app/services/confirmation-dialog/confirmation-dialog.service"; import { DataService } from "src/app/services/data/data.service"; import { SpinnerService } from "src/app/services/spinner/spinner.service"; -import { ToastService } from "src/app/services/toast/toast.service"; import { Server, ServerFilter } from "../../../../../models/data/server.model"; import { catchError, takeUntil } from "rxjs/operators"; import { Queries } from "../../../../../models/graphql/queries.model"; import { Page } from "../../../../../models/graphql/filter/page.model"; import { Sort } from "../../../../../models/graphql/filter/sort.model"; import { Query } from "../../../../../models/graphql/query.model"; -import { SidebarService } from "../../../../../services/sidebar/sidebar.service"; import { ServerService } from "../../../../../services/server.service"; @Component({ @@ -47,7 +43,7 @@ export class DashboardComponent implements OnInit, OnDestroy { private spinnerService: SpinnerService, private fb: FormBuilder, private router: Router, - private serverService: ServerService, + private serverService: ServerService ) { } diff --git a/web/src/app/services/error-handler/error-handler.service.ts b/web/src/app/services/error-handler/error-handler.service.ts index 89786ef2..12803e86 100644 --- a/web/src/app/services/error-handler/error-handler.service.ts +++ b/web/src/app/services/error-handler/error-handler.service.ts @@ -11,13 +11,14 @@ export class ErrorHandlerService implements ErrorHandler { constructor( private auth: AuthService, - private toast: ToastService, - ) { } + private toast: ToastService + ) { + } handleError(error: HttpErrorResponse): Observable { if (error && error.error) { - let message = 'Unbekannter Fehler'; - let header = 'Fehler'; + let message = "Unbekannter Fehler"; + let header = "Fehler"; const errorDto: ErrorDTO = error.error; if (errorDto.errorCode === ServiceErrorCode.Unauthorized) { @@ -26,7 +27,7 @@ export class ErrorHandlerService implements ErrorHandler { } if (errorDto.errorCode !== undefined) { - header = 'Fehlercode: ' + errorDto.errorCode; + header = "Fehlercode: " + errorDto.errorCode; } if (errorDto.message) { @@ -37,6 +38,7 @@ export class ErrorHandlerService implements ErrorHandler { this.toast.error(header, message); } else { + this.toast.error("ERROR", error.message); console.error(error.message); }