|
19 | 19 | # limitations under the License. |
20 | 20 |
|
21 | 21 |
|
| 22 | +""" |
| 23 | +This module contains the core driver exceptions. |
| 24 | +""" |
| 25 | + |
| 26 | + |
22 | 27 | class AddressError(Exception): |
23 | 28 | """ Raised when a network address is invalid. |
24 | 29 | """ |
25 | 30 |
|
26 | 31 |
|
| 32 | +class ProtocolError(Exception): |
| 33 | + """ Raised when an unexpected or unsupported protocol event occurs. |
| 34 | + """ |
| 35 | + |
| 36 | + |
| 37 | +class ServiceUnavailable(Exception): |
| 38 | + """ Raised when no database service is available. |
| 39 | + """ |
| 40 | + |
| 41 | + |
27 | 42 | class SecurityError(Exception): |
28 | 43 | """ Raised when an action is denied due to security settings. |
29 | 44 | """ |
30 | 45 |
|
31 | 46 |
|
32 | | -class AuthError(SecurityError): |
33 | | - """ Raised when authentication failure occurs. |
| 47 | +class CypherError(Exception): |
| 48 | + """ Raised when the Cypher engine returns an error to the client. |
34 | 49 | """ |
35 | 50 |
|
| 51 | + message = None |
| 52 | + code = None |
| 53 | + classification = None |
| 54 | + category = None |
| 55 | + title = None |
| 56 | + metadata = None |
36 | 57 |
|
37 | | -class ProtocolError(Exception): |
38 | | - """ Raised when an unexpected or unsupported protocol event occurs. |
| 58 | + @classmethod |
| 59 | + def hydrate(cls, message=None, code=None, **metadata): |
| 60 | + message = message or "An unknown error occurred." |
| 61 | + code = code or "Neo.DatabaseError.General.UnknownError" |
| 62 | + try: |
| 63 | + _, classification, category, title = code.split(".") |
| 64 | + except ValueError: |
| 65 | + classification = "DatabaseError" |
| 66 | + category = "General" |
| 67 | + title = "UnknownError" |
| 68 | + if classification == "ClientError": |
| 69 | + try: |
| 70 | + error_class = client_errors[code] |
| 71 | + except KeyError: |
| 72 | + error_class = ClientError |
| 73 | + elif classification == "DatabaseError": |
| 74 | + error_class = DatabaseError |
| 75 | + elif classification == "TransientError": |
| 76 | + error_class = TransientError |
| 77 | + else: |
| 78 | + error_class = cls |
| 79 | + inst = error_class(message) |
| 80 | + inst.message = message |
| 81 | + inst.code = code |
| 82 | + inst.classification = classification |
| 83 | + inst.category = category |
| 84 | + inst.title = title |
| 85 | + inst.metadata = metadata |
| 86 | + return inst |
| 87 | + |
| 88 | + |
| 89 | +class ClientError(CypherError): |
| 90 | + """ The Client sent a bad request - changing the request might yield a successful outcome. |
39 | 91 | """ |
40 | 92 |
|
41 | 93 |
|
42 | | -class ServiceUnavailable(Exception): |
43 | | - """ Raised when no database service is available. |
| 94 | +class DatabaseError(CypherError): |
| 95 | + """ The database failed to service the request. |
| 96 | + """ |
| 97 | + |
| 98 | + |
| 99 | +class TransientError(CypherError): |
| 100 | + """ The database cannot service the request right now, retrying later might yield a successful outcome. |
| 101 | + """ |
| 102 | + |
| 103 | + |
| 104 | +class ConstraintError(ClientError): |
44 | 105 | """ |
| 106 | + """ |
| 107 | + |
| 108 | + |
| 109 | +class CypherSyntaxError(ClientError): |
| 110 | + """ |
| 111 | + """ |
| 112 | + |
| 113 | + |
| 114 | +class CypherTypeError(ClientError): |
| 115 | + """ |
| 116 | + """ |
| 117 | + |
| 118 | + |
| 119 | +class Forbidden(ClientError, SecurityError): |
| 120 | + """ |
| 121 | + """ |
| 122 | + |
| 123 | + |
| 124 | +class AuthError(ClientError, SecurityError): |
| 125 | + """ Raised when authentication failure occurs. |
| 126 | + """ |
| 127 | + |
| 128 | + |
| 129 | +client_errors = { |
| 130 | + |
| 131 | + # ConstraintError |
| 132 | + "Neo.ClientError.Schema.ConstraintValidationFailed": ConstraintError, |
| 133 | + "Neo.ClientError.Schema.ConstraintViolation": ConstraintError, |
| 134 | + "Neo.ClientError.Statement.ConstraintVerificationFailed": ConstraintError, |
| 135 | + "Neo.ClientError.Statement.ConstraintViolation": ConstraintError, |
| 136 | + |
| 137 | + # CypherSyntaxError |
| 138 | + "Neo.ClientError.Statement.InvalidSyntax": CypherSyntaxError, |
| 139 | + "Neo.ClientError.Statement.SyntaxError": CypherSyntaxError, |
| 140 | + |
| 141 | + # CypherTypeError |
| 142 | + "Neo.ClientError.Procedure.TypeError": CypherTypeError, |
| 143 | + "Neo.ClientError.Statement.InvalidType": CypherTypeError, |
| 144 | + "Neo.ClientError.Statement.TypeError": CypherTypeError, |
| 145 | + |
| 146 | + # Forbidden |
| 147 | + "Neo.ClientError.General.ForbiddenOnReadOnlyDatabase": Forbidden, |
| 148 | + "Neo.ClientError.General.ReadOnly": Forbidden, |
| 149 | + "Neo.ClientError.Schema.ForbiddenOnConstraintIndex": Forbidden, |
| 150 | + "Neo.ClientError.Schema.IndexBelongsToConstraint": Forbidden, |
| 151 | + "Neo.ClientError.Security.Forbidden": Forbidden, |
| 152 | + "Neo.ClientError.Transaction.ForbiddenDueToTransactionType": Forbidden, |
| 153 | + |
| 154 | + # AuthError |
| 155 | + "Neo.ClientError.Security.AuthorizationFailed": AuthError, |
| 156 | + "Neo.ClientError.Security.Unauthorized": AuthError, |
| 157 | + |
| 158 | +} |
0 commit comments