File size: 4,161 Bytes
ae01f49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
##########################################################################
#
# pgAdmin 4 - PostgreSQL Tools
#
# Copyright (C) 2013 - 2024, The pgAdmin Development Team
# This software is released under the PostgreSQL Licence
#
##########################################################################

from werkzeug.exceptions import HTTPException
from werkzeug.http import HTTP_STATUS_CODES
from flask_babel import gettext as _
from flask import request

from pgadmin.utils.ajax import service_unavailable, gone, internal_server_error

SERVICE_UNAVAILABLE = 'Service Unavailable'


class ConnectionLost(HTTPException):
    """
    Exception
    """

    def __init__(self, _server_id, _database_name, _conn_id):
        self.sid = _server_id
        self.db = _database_name
        self.conn_id = _conn_id
        HTTPException.__init__(self)

    @property
    def name(self):
        return HTTP_STATUS_CODES.get(503, SERVICE_UNAVAILABLE)

    def get_response(self, environ=None):
        return service_unavailable(
            _("Connection to the server has been lost."),
            info="CONNECTION_LOST",
            data={
                'sid': self.sid,
                'database': self.db,
                'conn_id': self.conn_id
            }
        )

    def __str__(self):
        return "Connection (id #{2}) lost for the server (#{0}) on " \
               "database ({1})".format(self.sid, self.db, self.conn_id)

    def __repr__(self):
        return "Connection (id #{2}) lost for the server (#{0}) on " \
               "database ({1})".format(self.sid, self.db, self.conn_id)


class SSHTunnelConnectionLost(HTTPException):
    """
    Exception when connection to SSH tunnel is lost
    """

    def __init__(self, _tunnel_host):
        self.tunnel_host = _tunnel_host
        HTTPException.__init__(self)

    @property
    def name(self):
        return HTTP_STATUS_CODES.get(503, SERVICE_UNAVAILABLE)

    def get_response(self, environ=None):
        return service_unavailable(
            _("Connection to the SSH Tunnel for host '{0}' has been lost. "
              "Reconnect to the database server.").format(self.tunnel_host),
            info="SSH_TUNNEL_CONNECTION_LOST",
            data={
                'tunnel_host': self.tunnel_host
            }
        )

    def __str__(self):
        return "Connection to the SSH Tunnel for host '{0}' has been lost. " \
               "Reconnect to the database server".format(self.tunnel_host)

    def __repr__(self):
        return "Connection to the SSH Tunnel for host '{0}' has been lost. " \
               "Reconnect to the database server".format(self.tunnel_host)


class CryptKeyMissing(HTTPException):
    """
    Exception
    """
    CRYPT_KEY_MISSING = "Crypt key is missing."

    def __init__(self):
        HTTPException.__init__(self)

    @property
    def name(self):
        return HTTP_STATUS_CODES.get(503, SERVICE_UNAVAILABLE)

    def get_response(self, environ=None):
        return service_unavailable(
            _(self.CRYPT_KEY_MISSING),
            info="CRYPTKEY_MISSING",
        )

    def __str__(self):
        return self.CRYPT_KEY_MISSING

    def __repr__(self):
        return self.CRYPT_KEY_MISSING


class ObjectGone(HTTPException):
    """
    Exception
    """

    def __init__(self, error_msg):
        self.error_msg = error_msg
        HTTPException.__init__(self)

    @property
    def name(self):
        return HTTP_STATUS_CODES.get(410, 'Gone')

    def get_response(self, environ=None):
        return gone(self.error_msg)

    def __str__(self):
        return self.error_msg

    def __repr__(self):
        return self.error_msg


class ExecuteError(HTTPException):
    """
    ExecuteError
    """

    def __init__(self, error_msg):
        self.error_msg = error_msg
        HTTPException.__init__(self)

    @property
    def name(self):
        return HTTP_STATUS_CODES.get(500, 'Internal server error')

    def get_response(self, environ=None):
        return internal_server_error(self.error_msg)

    def __str__(self):
        return self.error_msg

    def __repr__(self):
        return self.error_msg