Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 266e1b4d

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

Moved global error handlers out of the CertificateController to a separate file.
Renamed generic exception handlers.

Zobrazit rozdíly:

app.py
10 10
from src.config.connection_provider import ConnectionProvider
11 11
from src.controllers.certificates_controller import CertController
12 12
from src.controllers.crl_ocsp_controller import CrlOcspController
13
from src.controllers.exception_handlers import handle_cryptography_exception, handle_database_exception, \
14
    handle_generic_exception
13 15
from src.exceptions.database_exception import DatabaseException
14 16
from src.services.cryptography import CryptographyService, CryptographyException
15 17
from src.utils.logger import Logger
......
93 95

  
94 96

  
95 97
@app.errorhandler(CryptographyException)
96
def cryptography_error(e, certificate_controller: CertController):
97
    return certificate_controller.handle_cryptography_error(e)
98
def cryptography_error(e):
99
    return handle_cryptography_exception(e)
98 100

  
99 101

  
100 102
@app.errorhandler(DatabaseException)
101
def database_error(e, certificate_controller: CertController):
102
    return certificate_controller.handle_database_error(e)
103
def database_error(e):
104
    return handle_database_exception(e)
103 105

  
104 106

  
105 107
@app.errorhandler(Exception)
106
def generic_exception(e, certificate_controller: CertController):
108
def generic_exception(e):
107 109
    if isinstance(e, HTTPException):
110
        # handle HTTPException exceptions here (MethodNotAllowed for example)
108 111
        Logger.warning(f""" HTTPException occurred: "{str(e)}" """)
109 112
        return str(e), e.code
110
    return certificate_controller.handle_generic_exception(e)
113
    return handle_generic_exception(e)
111 114

  
112 115

  
113 116
def initialize_app(application) -> bool:
src/controllers/certificates_controller.py
65 65
E_INVALID_EXTENSIONS = {"success": False, "data": "Error occurred while creating a certificate. "
66 66
                                                  "It may be caused by wrong format of extensions."}
67 67

  
68
E_UNHANDLED_CRYPTOGRAPHY_ERROR = {"success": False, "data": "An unknown error has happened in the cryptography library."}
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."}
71

  
72 68

  
73 69
class CertController:
74 70
    USAGE_KEY_MAP = {'CA': CA_ID, 'SSL': SSL_ID, 'digitalSignature': SIGNATURE_ID, 'authentication': AUTHENTICATION_ID}
......
772 768
                                                                                          identity_name,
773 769
                                                                                          identity_password)
774 770
                    return Response(identity_byte_array, mimetype='application/x-pkcs12')
775

  
776
    @staticmethod
777
    def handle_cryptography_error(e):
778
        Logger.error(f"An unhandled CryptographyException has been raised: {str(e)}")
779
        return E_UNHANDLED_CRYPTOGRAPHY_ERROR, C_INTERNAL_SERVER_ERROR
780

  
781
    @staticmethod
782
    def handle_database_error(e):
783
        Logger.error(f"An unhandled DatabaseException has been raised: {str(e)}")
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
src/controllers/exception_handlers.py
1
from src.controllers.return_codes import C_INTERNAL_SERVER_ERROR
2
from src.utils.logger import Logger
3

  
4
E_UNHANDLED_CRYPTOGRAPHY_ERROR = {"success": False,
5
                                  "data": "An unknown error has happened in the cryptography library."}
6
E_UNHANDLED_DATABASE_ERROR = {"success": False, "data": "An unknown database error has happened."}
7
E_UNHANDLED_ERROR = {"success": False, "data": "An unknown error has happened."}
8

  
9

  
10
def handle_cryptography_exception(e):
11
    Logger.error(f"An unhandled CryptographyException has been raised: {str(e)}")
12
    return E_UNHANDLED_CRYPTOGRAPHY_ERROR, C_INTERNAL_SERVER_ERROR
13

  
14

  
15
def handle_database_exception(e):
16
    Logger.error(f"An unhandled DatabaseException has been raised: {str(e)}")
17
    return E_UNHANDLED_DATABASE_ERROR, C_INTERNAL_SERVER_ERROR
18

  
19

  
20
def handle_generic_exception(e):
21
    Logger.error(f"An unknown Exception ({type(e)}) has been raised: {str(e)}")
22
    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, E_UNHANDLED_ERROR
6
    E_NO_CERTIFICATES_FOUND
7
from src.controllers.exception_handlers import E_UNHANDLED_CRYPTOGRAPHY_ERROR, E_UNHANDLED_DATABASE_ERROR, \
8
    E_UNHANDLED_ERROR
7 9
from src.exceptions.database_exception import DatabaseException
8 10
from src.model.certificate import Certificate
9 11
from src.services.cryptography import CryptographyException

Také k dispozici: Unified diff