|
| 1 | +__all__ = [ |
| 2 | + 'G2BadInputException', |
| 3 | + 'G2ConfigurationException', |
| 4 | + 'G2Exception', |
| 5 | + 'G2IncompleteRecordException', |
| 6 | + 'G2MalformedJsonException', |
| 7 | + 'G2MessageBufferException', |
| 8 | + 'G2MissingConfigurationException', |
| 9 | + 'G2MissingDataSourceException', |
| 10 | + 'G2ModuleEmptyMessage', |
| 11 | + 'G2ModuleException', |
| 12 | + 'G2ModuleGenericException', |
| 13 | + 'G2ModuleInvalidXML', |
| 14 | + 'G2ModuleLicenseException', |
| 15 | + 'G2ModuleNotInitialized', |
| 16 | + 'G2ModuleResolveMissingResEnt', |
| 17 | + 'G2RepositoryPurgedException', |
| 18 | + 'G2RetryableException', |
| 19 | + 'G2UnacceptableJsonKeyValueException', |
| 20 | + 'G2UnrecoverableException', |
| 21 | + 'TranslateG2ModuleException', |
| 22 | +] |
| 23 | + |
| 24 | +# ----------------------------------------------------------------------------- |
| 25 | +# Base G2Exception |
| 26 | +# ----------------------------------------------------------------------------- |
1 | 27 |
|
2 | 28 |
|
3 | 29 | class G2Exception(Exception): |
4 | 30 | '''Base exception for G2 related python code''' |
| 31 | + |
5 | 32 | def __init__(self, *args, **kwargs): |
6 | | - Exception.__init__(self, *args, **kwargs) |
| 33 | + super().__init__(self, *args, **kwargs) |
| 34 | + |
| 35 | +# ----------------------------------------------------------------------------- |
| 36 | +# Category exceptions |
| 37 | +# - These exceptions represent categories of actions that can be taken by |
| 38 | +# the calling program. |
| 39 | +# - G2BadInputException - The user-supplied input contained an error. |
| 40 | +# - G2RetryableException - The program can provide a remedy and continue. |
| 41 | +# - G2UnrecoverableException - System failure; can't continue. |
| 42 | +# ----------------------------------------------------------------------------- |
| 43 | + |
| 44 | + |
| 45 | +class G2BadInputException(G2Exception): |
7 | 46 |
|
8 | | -class G2UnsupportedFileTypeException(G2Exception): |
9 | 47 | def __init__(self, *args, **kwargs): |
10 | | - G2Exception.__init__(self, *args, **kwargs) |
| 48 | + super().__init__(self, *args, **kwargs) |
| 49 | + |
| 50 | + |
| 51 | +class G2RetryableException(G2Exception): |
11 | 52 |
|
12 | | -class G2InvalidFileTypeContentsException(G2Exception): |
13 | 53 | def __init__(self, *args, **kwargs): |
14 | | - G2Exception.__init__(self, *args, **kwargs) |
| 54 | + super().__init__(self, *args, **kwargs) |
| 55 | + |
| 56 | + |
| 57 | +class G2UnrecoverableException(G2Exception): |
15 | 58 |
|
16 | | -class G2DBException(G2Exception): |
17 | | - '''Base exception for G2 DB related python code''' |
18 | 59 | def __init__(self, *args, **kwargs): |
19 | | - G2Exception.__init__(self, *args, **kwargs) |
| 60 | + super().__init__(self, *args, **kwargs) |
20 | 61 |
|
21 | | -class UnconfiguredDataSourceException(G2Exception): |
22 | | - def __init__(self, DataSourceName): |
23 | | - G2Exception.__init__(self,("Datasource %s not configured. See https://senzing.zendesk.com/hc/en-us/articles/360010784333 on how to configure datasources in the config file." % DataSourceName )) |
| 62 | +# ----------------------------------------------------------------------------- |
| 63 | +# Detail exceptions for G2BadInputException |
| 64 | +# - Processing did not complete. |
| 65 | +# - These exceptions are "per record" exceptions. |
| 66 | +# - The record should be recorded as "bad". (logged, queued as failure) |
| 67 | +# - Processing may continue. |
| 68 | +# ----------------------------------------------------------------------------- |
| 69 | + |
| 70 | + |
| 71 | +class G2IncompleteRecordException(G2BadInputException): |
24 | 72 |
|
25 | | -class G2DBUnknownException(G2DBException): |
26 | 73 | def __init__(self, *args, **kwargs): |
27 | | - G2DBException.__init__(self, *args, **kwargs) |
| 74 | + super().__init__(self, *args, **kwargs) |
| 75 | + |
| 76 | + |
| 77 | +class G2MalformedJsonException(G2BadInputException): |
28 | 78 |
|
29 | | -class G2UnsupportedDatabaseType(G2DBException): |
30 | 79 | def __init__(self, *args, **kwargs): |
31 | | - G2DBException.__init__(self, *args, **kwargs) |
| 80 | + super().__init__(self, *args, **kwargs) |
| 81 | + |
| 82 | + |
| 83 | +class G2MissingConfigurationException(G2BadInputException): |
32 | 84 |
|
33 | | -class G2TableNoExist(G2DBException): |
34 | 85 | def __init__(self, *args, **kwargs): |
35 | | - G2DBException.__init__(self, *args, **kwargs) |
| 86 | + super().__init__(self, *args, **kwargs) |
| 87 | + |
| 88 | + |
| 89 | +class G2MissingDataSourceException(G2BadInputException): |
| 90 | + |
| 91 | + def __init__(self, *args, **kwargs): |
| 92 | + super().__init__(self, *args, **kwargs) |
| 93 | + |
| 94 | + |
| 95 | +class G2UnacceptableJsonKeyValueException(G2BadInputException): |
| 96 | + |
| 97 | + def __init__(self, *args, **kwargs): |
| 98 | + super().__init__(self, *args, **kwargs) |
| 99 | + |
| 100 | +# ----------------------------------------------------------------------------- |
| 101 | +# Detail exceptions for G2RetryableException |
| 102 | +# - Processing did not complete. |
| 103 | +# - These exceptions may be remedied programmatically. |
| 104 | +# - The call to the Senzing method should be retried. |
| 105 | +# - Processing may continue. |
| 106 | +# ----------------------------------------------------------------------------- |
| 107 | + |
| 108 | + |
| 109 | +class G2ConfigurationException(G2RetryableException): |
| 110 | + |
| 111 | + def __init__(self, *args, **kwargs): |
| 112 | + super().__init__(self, *args, **kwargs) |
| 113 | + |
| 114 | + |
| 115 | +class G2DatabaseConnectionLost(G2RetryableException): |
36 | 116 |
|
37 | | -class G2DBMNotStarted(G2DBException): |
38 | 117 | def __init__(self, *args, **kwargs): |
39 | | - G2DBException.__init__(self, *args, **kwargs) |
| 118 | + super().__init__(self, *args, **kwargs) |
| 119 | + |
| 120 | + |
| 121 | +class G2MessageBufferException(G2RetryableException): |
40 | 122 |
|
41 | | -class G2DBNotFound(G2DBException): |
42 | 123 | def __init__(self, *args, **kwargs): |
43 | | - G2DBException.__init__(self, *args, **kwargs) |
| 124 | + super().__init__(self, *args, **kwargs) |
| 125 | + |
| 126 | + |
| 127 | +class G2RepositoryPurgedException(G2RetryableException): |
44 | 128 |
|
45 | | -class G2DBUniqueConstraintViolation(G2DBException): |
46 | 129 | def __init__(self, *args, **kwargs): |
47 | | - G2DBException.__init__(self, *args, **kwargs) |
| 130 | + super().__init__(self, *args, **kwargs) |
| 131 | + |
| 132 | +# ----------------------------------------------------------------------------- |
| 133 | +# Detail exceptions for G2UnrecoverableException |
| 134 | +# - Processing did not complete. |
| 135 | +# - These exceptions cannot be remedied programmatically. |
| 136 | +# - Processing cannot continue. |
| 137 | +# ----------------------------------------------------------------------------- |
| 138 | + |
48 | 139 |
|
49 | 140 | class G2ModuleException(G2Exception): |
50 | 141 | '''Base exception for G2 Module related python code''' |
| 142 | + |
51 | 143 | def __init__(self, *args, **kwargs): |
52 | | - G2Exception.__init__(self, *args, **kwargs) |
| 144 | + super().__init__(self, *args, **kwargs) |
| 145 | + |
| 146 | + |
| 147 | +class G2ModuleEmptyMessage(G2ModuleException): |
53 | 148 |
|
54 | | -class G2ModuleNotInitialized(G2ModuleException): |
55 | | - '''G2 Module called but has not been initialized ''' |
56 | 149 | def __init__(self, *args, **kwargs): |
57 | | - G2ModuleException.__init__(self, *args, **kwargs) |
| 150 | + super().__init__(self, *args, **kwargs) |
| 151 | + |
58 | 152 |
|
59 | 153 | class G2ModuleGenericException(G2ModuleException): |
60 | 154 | '''Generic exception for non-subclassed G2 Module exception ''' |
| 155 | + |
61 | 156 | def __init__(self, *args, **kwargs): |
62 | | - G2ModuleException.__init__(self, *args, **kwargs) |
| 157 | + super().__init__(self, *args, **kwargs) |
| 158 | + |
| 159 | + |
| 160 | +class G2ModuleInvalidXML(G2ModuleException): |
63 | 161 |
|
64 | | -class G2ModuleMySQLNoSchema(G2ModuleException): |
65 | 162 | def __init__(self, *args, **kwargs): |
66 | | - G2ModuleException.__init__(self, *args, **kwargs) |
| 163 | + super().__init__(self, *args, **kwargs) |
| 164 | + |
| 165 | + |
| 166 | +class G2ModuleLicenseException(G2ModuleException): |
67 | 167 |
|
68 | | -class G2ModuleEmptyMessage(G2ModuleException): |
69 | 168 | def __init__(self, *args, **kwargs): |
70 | | - G2ModuleException.__init__(self, *args, **kwargs) |
| 169 | + super().__init__(self, *args, **kwargs) |
| 170 | + |
| 171 | + |
| 172 | +class G2ModuleNotInitialized(G2ModuleException): |
| 173 | + '''G2 Module called but has not been initialized ''' |
71 | 174 |
|
72 | | -class G2ModuleInvalidXML(G2ModuleException): |
73 | 175 | def __init__(self, *args, **kwargs): |
74 | | - G2ModuleException.__init__(self, *args, **kwargs) |
| 176 | + super().__init__(self, *args, **kwargs) |
| 177 | + |
75 | 178 |
|
76 | 179 | class G2ModuleResolveMissingResEnt(G2ModuleException): |
77 | | - def __init__(self, *args, **kwargs): |
78 | | - G2ModuleException.__init__(self, *args, **kwargs) |
79 | 180 |
|
80 | | -class G2ModuleLicenseException(G2ModuleException): |
81 | 181 | def __init__(self, *args, **kwargs): |
82 | | - G2ModuleException.__init__(self, *args, **kwargs) |
83 | | - |
84 | | -def TranslateG2ModuleException(ex): |
85 | | - exInfo = ex.decode().split('|', 1) |
86 | | - if exInfo[0] == '7213E': |
87 | | - return G2ModuleMySQLNoSchema(ex) |
88 | | - elif exInfo[0] == '0002E': |
89 | | - return G2ModuleInvalidXML(ex.decode()) |
90 | | - elif exInfo[0] == '0007E': |
91 | | - return G2ModuleEmptyMessage(ex.decode()) |
92 | | - elif exInfo[0] == '2134E': |
93 | | - return G2ModuleResolveMissingResEnt(ex.decode()) |
94 | | - elif exInfo[0] == '9000E': |
95 | | - return G2ModuleLicenseException(ex.decode()) |
| 182 | + super().__init__(self, *args, **kwargs) |
| 183 | + |
| 184 | +# ----------------------------------------------------------------------------- |
| 185 | +# Determine Exception based on Senzing reason code. |
| 186 | +# Reference: https://senzing.zendesk.com/hc/en-us/articles/360026678133-Engine-Error-codes |
| 187 | +# ----------------------------------------------------------------------------- |
| 188 | + |
| 189 | + |
| 190 | +exceptions_map = { |
| 191 | + "0002E": G2ModuleInvalidXML, |
| 192 | + "0007E": G2ModuleEmptyMessage, |
| 193 | + "0023E": G2UnacceptableJsonKeyValueException, |
| 194 | + "0024E": G2UnacceptableJsonKeyValueException, |
| 195 | + "0025E": G2UnacceptableJsonKeyValueException, |
| 196 | + "0026E": G2UnacceptableJsonKeyValueException, |
| 197 | + "0027E": G2UnacceptableJsonKeyValueException, |
| 198 | + "0032E": G2UnacceptableJsonKeyValueException, |
| 199 | + "0034E": G2ConfigurationException, |
| 200 | + "0035E": G2ConfigurationException, |
| 201 | + "0036E": G2ConfigurationException, |
| 202 | + "0051E": G2UnacceptableJsonKeyValueException, |
| 203 | + "0054E": G2RepositoryPurgedException, |
| 204 | + "0061E": G2ConfigurationException, |
| 205 | + "0062E": G2ConfigurationException, |
| 206 | + "0064E": G2ConfigurationException, |
| 207 | + "1007E": G2DatabaseConnectionLost, |
| 208 | + "2134E": G2ModuleResolveMissingResEnt, |
| 209 | + "9000E": G2ModuleLicenseException, |
| 210 | + "30020": G2UnacceptableJsonKeyValueException, |
| 211 | + "30110E": G2MessageBufferException, |
| 212 | + "30111E": G2MessageBufferException, |
| 213 | + "30112E": G2MessageBufferException, |
| 214 | + "30121E": G2MalformedJsonException, |
| 215 | + "30122E": G2MalformedJsonException, |
| 216 | + "30123E": G2MalformedJsonException, |
| 217 | +} |
| 218 | + |
| 219 | + |
| 220 | +def TranslateG2ModuleException(exception_message): |
| 221 | + |
| 222 | + if exception_message is None: |
| 223 | + exception_message_string = '' |
| 224 | + elif isinstance(exception_message, bytearray): |
| 225 | + exception_message_string = exception_message.decode() |
| 226 | + elif isinstance(exception_message, bytes): |
| 227 | + exception_message_string = exception_message.decode() |
96 | 228 | else: |
97 | | - return G2ModuleGenericException(ex.decode()) |
| 229 | + exception_message_string = exception_message |
98 | 230 |
|
| 231 | + senzing_error_code = exception_message_string.split('|', 1)[0].strip() |
| 232 | + senzing_error_class = exceptions_map.get(senzing_error_code, G2Exception) |
| 233 | + return senzing_error_class(exception_message_string) |
0 commit comments