Projekt

Obecné

Profil

« Předchozí | Další » 

Revize d55b3aff

Přidáno uživatelem Stanislav Král před téměř 4 roky(ů)

Added a global error handler for generic exceptions.
Covered the added global exception error handler in a test.

Zobrazit rozdíly:

app.py
1 1
import os
2 2

  
3 3
from flask import Flask, redirect, request
4
from injector import Injector
5 4
from flask_injector import FlaskInjector
5
from injector import Injector
6
from werkzeug.exceptions import HTTPException
6 7

  
7 8
from src.config import configuration
8 9
from src.config.configuration import Configuration
......
101 102
    return certificate_controller.handle_database_error(e)
102 103

  
103 104

  
105
@app.errorhandler(Exception)
106
def generic_exception(e, certificate_controller: CertController):
107
    if isinstance(e, HTTPException):
108
        Logger.warning(f""" HTTPException occurred: "{str(e)}" """)
109
        return str(e), e.code
110
    return certificate_controller.handle_generic_exception(e)
111

  
112

  
104 113
def initialize_app(application) -> bool:
105 114
    """
106 115
    Initializes the application
src/controllers/certificates_controller.py
67 67

  
68 68
E_UNHANDLED_CRYPTOGRAPHY_ERROR = {"success": False, "data": "An unknown error has happened in the cryptography library."}
69 69
E_UNHANDLED_DATABASE_ERROR = {"success": False, "data": "An unknown database error has happened."}
70
E_UNHANDLED_ERROR = {"success": False, "data": "An unknown error has happened."}
70 71

  
71 72

  
72 73
class CertController:
......
781 782
    def handle_database_error(e):
782 783
        Logger.error(f"An unhandled DatabaseException has been raised: {str(e)}")
783 784
        return E_UNHANDLED_DATABASE_ERROR, C_INTERNAL_SERVER_ERROR
785

  
786
    @staticmethod
787
    def handle_generic_exception(e):
788
        Logger.error(f"An unknown Exception ({type(e)}) has been raised: {str(e)}")
789
        return E_UNHANDLED_ERROR, C_INTERNAL_SERVER_ERROR
tests/integration_tests/rest_api/certificates_test.py
3 3
# 2->3->4->5->6->7
4 4
# 2->8->9
5 5
from src.controllers.certificates_controller import E_IDENTITY_NAME_NOT_SPECIFIED, E_IDENTITY_PASSWORD_NOT_SPECIFIED, \
6
    E_NO_CERTIFICATES_FOUND, E_UNHANDLED_CRYPTOGRAPHY_ERROR, E_UNHANDLED_DATABASE_ERROR
6
    E_NO_CERTIFICATES_FOUND, E_UNHANDLED_CRYPTOGRAPHY_ERROR, E_UNHANDLED_DATABASE_ERROR, E_UNHANDLED_ERROR
7 7
from src.exceptions.database_exception import DatabaseException
8 8
from src.model.certificate import Certificate
9 9
from src.services.cryptography import CryptographyException
......
1328 1328
    ret = make_root_ca(server)
1329 1329
    assert ret.status_code == 500
1330 1330
    assert ret.json == E_UNHANDLED_DATABASE_ERROR
1331

  
1332

  
1333
def test_generic_exception_handler(server, mocker):
1334
    def mock_raises_zero_division_exception(self, certificate_id: int):
1335
        raise ZeroDivisionError("Failure is the only option")
1336

  
1337
    mocker.patch(
1338
        # patch create method of the CertificateRepository in such way that a ZeroDivisionError is raised
1339
        'src.dao.certificate_repository.CertificateRepository.read_all',
1340
        mock_raises_zero_division_exception
1341
    )
1342

  
1343
    ret = server.get("/api/certificates")
1344

  
1345
    assert ret.status_code == 500
1346
    assert ret.json == E_UNHANDLED_ERROR

Také k dispozici: Unified diff