forked from microsoft/mssql-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.py
More file actions
655 lines (613 loc) · 25.3 KB
/
exceptions.py
File metadata and controls
655 lines (613 loc) · 25.3 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
"""
Copyright (c) Microsoft Corporation.
Licensed under the MIT license.
This module contains custom exception classes for the mssql_python package.
These classes are used to raise exceptions when an error occurs while executing a query.
"""
from mssql_python.logging_config import get_logger
logger = get_logger()
class Exception(Exception):
"""
Base class for all DB API 2.0 exceptions.
"""
def __init__(self, driver_error, ddbc_error) -> None:
self.driver_error = driver_error
self.ddbc_error = truncate_error_message(ddbc_error)
if self.ddbc_error:
# Both driver and DDBC errors are present
self.message = (
f"Driver Error: {self.driver_error}; DDBC Error: {self.ddbc_error}"
)
else:
# Errors raised by the driver itself should not have a DDBC error message
self.message = f"Driver Error: {self.driver_error}"
super().__init__(self.message)
class Warning(Exception):
"""
Exception raised for important warnings like data truncations while inserting, etc.
"""
def __init__(self, driver_error, ddbc_error) -> None:
super().__init__(driver_error, ddbc_error)
class Error(Exception):
"""
Base class for errors.
"""
def __init__(self, driver_error, ddbc_error) -> None:
super().__init__(driver_error, ddbc_error)
class InterfaceError(Error):
"""
Exception raised for errors that are related to the database
interface rather than the database itself.
"""
def __init__(self, driver_error, ddbc_error) -> None:
super().__init__(driver_error, ddbc_error)
class DatabaseError(Error):
"""
Exception raised for errors that are related to the database.
"""
def __init__(self, driver_error, ddbc_error) -> None:
super().__init__(driver_error, ddbc_error)
class DataError(DatabaseError):
"""
Exception raised for errors that are due to problems with the
processed data like division by zero, numeric value out of range, etc.
"""
def __init__(self, driver_error, ddbc_error) -> None:
super().__init__(driver_error, ddbc_error)
class OperationalError(DatabaseError):
"""
Exception raised for errors that are related to the database's operation
and not necessarily under the control of the programmer.
"""
def __init__(self, driver_error, ddbc_error) -> None:
super().__init__(driver_error, ddbc_error)
class IntegrityError(DatabaseError):
"""
Exception raised when the relational integrity of the database is affected,
e.g., a foreign key check fails.
"""
def __init__(self, driver_error, ddbc_error) -> None:
super().__init__(driver_error, ddbc_error)
class InternalError(DatabaseError):
"""
Exception raised when the database encounters an internal error,
e.g., the cursor is not valid anymore, the transaction is out of sync, etc.
"""
def __init__(self, driver_error, ddbc_error) -> None:
super().__init__(driver_error, ddbc_error)
class ProgrammingError(DatabaseError):
"""
Exception raised for programming errors,
e.g., table not found or already exists, syntax error in the SQL statement,
wrong number of parameters specified, etc.
"""
def __init__(self, driver_error, ddbc_error) -> None:
super().__init__(driver_error, ddbc_error)
class NotSupportedError(DatabaseError):
"""
Exception raised in case a method or database API was used which
is not supported by the database, e.g., requesting a .rollback()
on a connection that does not support transaction or has transactions turned off.
"""
def __init__(self, driver_error, ddbc_error) -> None:
super().__init__(driver_error, ddbc_error)
# Mapping SQLSTATE codes to custom exception classes
def sqlstate_to_exception(sqlstate: str, ddbc_error: str) -> Exception:
"""
Map an SQLSTATE code to a custom exception class.
This function maps an SQLSTATE code to a custom exception class based on the code.
If the code is not found in the mapping, a generic DatabaseError class is returned
to handle the error.
Args:
sqlstate (str): The SQLSTATE code to map to a custom exception class.
Returns:
mapping[str, Exception]: A mapping of SQLSTATE codes to custom exception classes.
"""
mapping = {
"01000": Warning(
driver_error="General warning",
ddbc_error=ddbc_error
), # General warning
"01001": OperationalError(
driver_error="Cursor operation conflict",
ddbc_error=ddbc_error
), # Cursor operation conflict
"01002": OperationalError(
driver_error="Disconnect error",
ddbc_error=ddbc_error
), # Disconnect error
"01003": DataError(
driver_error="NULL value eliminated in set function",
ddbc_error=ddbc_error
), # NULL value eliminated in set function
"01004": DataError(
driver_error="String data, right-truncated",
ddbc_error=ddbc_error
), # String data, right-truncated
"01006": OperationalError(
driver_error="Privilege not revoked",
ddbc_error=ddbc_error
), # Privilege not revoked
"01007": OperationalError(
driver_error="Privilege not granted",
ddbc_error=ddbc_error
), # Privilege not granted
"01S00": ProgrammingError(
driver_error="Invalid connection string attribute",
ddbc_error=ddbc_error
), # Invalid connection string attribute
"01S01": DataError(
driver_error="Error in row",
ddbc_error=ddbc_error
), # Error in row
"01S02": Warning(
driver_error="Option value changed",
ddbc_error=ddbc_error
), # Option value changed
"01S06": OperationalError(
driver_error="Attempt to fetch before the result set returned the first rowset",
ddbc_error=ddbc_error,
), # Attempt to fetch before the result set returned the first rowset
"01S07": DataError(
driver_error="Fractional truncation",
ddbc_error=ddbc_error
), # Fractional truncation
"01S08": OperationalError(
driver_error="Error saving File DSN",
ddbc_error=ddbc_error
), # Error saving File DSN
"01S09": ProgrammingError(
driver_error="Invalid keyword",
ddbc_error=ddbc_error
), # Invalid keyword
"07001": ProgrammingError(
driver_error="Wrong number of parameters",
ddbc_error=ddbc_error
), # Wrong number of parameters
"07002": ProgrammingError(
driver_error="COUNT field incorrect",
ddbc_error=ddbc_error
), # COUNT field incorrect
"07005": ProgrammingError(
driver_error="Prepared statement not a cursor-specification",
ddbc_error=ddbc_error,
), # Prepared statement not a cursor-specification
"07006": ProgrammingError(
driver_error="Restricted data type attribute violation",
ddbc_error=ddbc_error,
), # Restricted data type attribute violation
"07009": ProgrammingError(
driver_error="Invalid descriptor index",
ddbc_error=ddbc_error
), # Invalid descriptor index
"07S01": ProgrammingError(
driver_error="Invalid use of default parameter",
ddbc_error=ddbc_error
), # Invalid use of default parameter
"08001": OperationalError(
driver_error="Client unable to establish connection",
ddbc_error=ddbc_error
), # Client unable to establish connection
"08002": OperationalError(
driver_error="Connection name in use",
ddbc_error=ddbc_error
), # Connection name in use
"08003": OperationalError(
driver_error="Connection not open",
ddbc_error=ddbc_error
), # Connection not open
"08004": OperationalError(
driver_error="Server rejected the connection",
ddbc_error=ddbc_error
), # Server rejected the connection
"08007": OperationalError(
driver_error="Connection failure during transaction",
ddbc_error=ddbc_error
), # Connection failure during transaction
"08S01": OperationalError(
driver_error="Communication link failure",
ddbc_error=ddbc_error
), # Communication link failure
"21S01": ProgrammingError(
driver_error="Insert value list does not match column list",
ddbc_error=ddbc_error,
), # Insert value list does not match column list
"21S02": ProgrammingError(
driver_error="Degree of derived table does not match column list",
ddbc_error=ddbc_error,
), # Degree of derived table does not match column list
"22001": DataError(
driver_error="String data, right-truncated",
ddbc_error=ddbc_error
), # String data, right-truncated
"22002": DataError(
driver_error="Indicator variable required but not supplied",
ddbc_error=ddbc_error,
), # Indicator variable required but not supplied
"22003": DataError(
driver_error="Numeric value out of range",
ddbc_error=ddbc_error
), # Numeric value out of range
"22007": DataError(
driver_error="Invalid datetime format",
ddbc_error=ddbc_error
), # Invalid datetime format
"22008": DataError(
driver_error="Datetime field overflow",
ddbc_error=ddbc_error
), # Datetime field overflow
"22012": DataError(
driver_error="Division by zero",
ddbc_error=ddbc_error
), # Division by zero
"22015": DataError(
driver_error="Interval field overflow",
ddbc_error=ddbc_error
), # Interval field overflow
"22018": DataError(
driver_error="Invalid character value for cast specification",
ddbc_error=ddbc_error,
), # Invalid character value for cast specification
"22019": ProgrammingError(
driver_error="Invalid escape character",
ddbc_error=ddbc_error
), # Invalid escape character
"22025": ProgrammingError(
driver_error="Invalid escape sequence",
ddbc_error=ddbc_error
), # Invalid escape sequence
"22026": DataError(
driver_error="String data, length mismatch",
ddbc_error=ddbc_error
), # String data, length mismatch
"23000": IntegrityError(
driver_error="Integrity constraint violation",
ddbc_error=ddbc_error
), # Integrity constraint violation
"24000": ProgrammingError(
driver_error="Invalid cursor state",
ddbc_error=ddbc_error
), # Invalid cursor state
"25000": OperationalError(
driver_error="Invalid transaction state",
ddbc_error=ddbc_error
), # Invalid transaction state
"25S01": OperationalError(
driver_error="Transaction state",
ddbc_error=ddbc_error
), # Transaction state
"25S02": OperationalError(
driver_error="Transaction is still active",
ddbc_error=ddbc_error
), # Transaction is still active
"25S03": OperationalError(
driver_error="Transaction is rolled back",
ddbc_error=ddbc_error
), # Transaction is rolled back
"28000": OperationalError(
driver_error="Invalid authorization specification",
ddbc_error=ddbc_error
), # Invalid authorization specification
"34000": ProgrammingError(
driver_error="Invalid cursor name",
ddbc_error=ddbc_error
), # Invalid cursor name
"3C000": ProgrammingError(
driver_error="Duplicate cursor name",
ddbc_error=ddbc_error
), # Duplicate cursor name
"3D000": ProgrammingError(
driver_error="Invalid catalog name",
ddbc_error=ddbc_error
), # Invalid catalog name
"3F000": ProgrammingError(
driver_error="Invalid schema name",
ddbc_error=ddbc_error
), # Invalid schema name
"40001": OperationalError(
driver_error="Serialization failure",
ddbc_error=ddbc_error
), # Serialization failure
"40002": IntegrityError(
driver_error="Integrity constraint violation",
ddbc_error=ddbc_error
), # Integrity constraint violation
"40003": OperationalError(
driver_error="Statement completion unknown",
ddbc_error=ddbc_error
), # Statement completion unknown
"42000": ProgrammingError(
driver_error="Syntax error or access violation",
ddbc_error=ddbc_error
), # Syntax error or access violation
"42S01": ProgrammingError(
driver_error="Base table or view already exists",
ddbc_error=ddbc_error
), # Base table or view already exists
"42S02": ProgrammingError(
driver_error="Base table or view not found",
ddbc_error=ddbc_error
), # Base table or view not found
"42S11": ProgrammingError(
driver_error="Index already exists",
ddbc_error=ddbc_error
), # Index already exists
"42S12": ProgrammingError(
driver_error="Index not found",
ddbc_error=ddbc_error
), # Index not found
"42S21": ProgrammingError(
driver_error="Column already exists",
ddbc_error=ddbc_error
), # Column already exists
"42S22": ProgrammingError(
driver_error="Column not found",
ddbc_error=ddbc_error
), # Column not found
"44000": IntegrityError(
driver_error="WITH CHECK OPTION violation",
ddbc_error=ddbc_error
), # WITH CHECK OPTION violation
"HY000": OperationalError(
driver_error="General error",
ddbc_error=ddbc_error
), # General error
"HY001": OperationalError(
driver_error="Memory allocation error",
ddbc_error=ddbc_error
), # Memory allocation error
"HY003": ProgrammingError(
driver_error="Invalid application buffer type",
ddbc_error=ddbc_error
), # Invalid application buffer type
"HY004": ProgrammingError(
driver_error="Invalid SQL data type",
ddbc_error=ddbc_error
), # Invalid SQL data type
"HY007": ProgrammingError(
driver_error="Associated statement is not prepared",
ddbc_error=ddbc_error
), # Associated statement is not prepared
"HY008": OperationalError(
driver_error="Operation canceled",
ddbc_error=ddbc_error
), # Operation canceled
"HY009": ProgrammingError(
driver_error="Invalid use of null pointer",
ddbc_error=ddbc_error
), # Invalid use of null pointer
"HY010": ProgrammingError(
driver_error="Function sequence error",
ddbc_error=ddbc_error
), # Function sequence error
"HY011": ProgrammingError(
driver_error="Attribute cannot be set now",
ddbc_error=ddbc_error
), # Attribute cannot be set now
"HY012": ProgrammingError(
driver_error="Invalid transaction operation code",
ddbc_error=ddbc_error
), # Invalid transaction operation code
"HY013": OperationalError(
driver_error="Memory management error",
ddbc_error=ddbc_error
), # Memory management error
"HY014": OperationalError(
driver_error="Limit on the number of handles exceeded",
ddbc_error=ddbc_error,
), # Limit on the number of handles exceeded
"HY015": ProgrammingError(
driver_error="No cursor name available",
ddbc_error=ddbc_error
), # No cursor name available
"HY016": ProgrammingError(
driver_error="Cannot modify an implementation row descriptor",
ddbc_error=ddbc_error,
), # Cannot modify an implementation row descriptor
"HY017": ProgrammingError(
driver_error="Invalid use of an automatically allocated descriptor handle",
ddbc_error=ddbc_error,
), # Invalid use of an automatically allocated descriptor handle
"HY018": OperationalError(
driver_error="Server declined cancel request",
ddbc_error=ddbc_error
), # Server declined cancel request
"HY019": DataError(
driver_error="Non-character and non-binary data sent in pieces",
ddbc_error=ddbc_error,
), # Non-character and non-binary data sent in pieces
"HY020": DataError(
driver_error="Attempt to concatenate a null value",
ddbc_error=ddbc_error
), # Attempt to concatenate a null value
"HY021": ProgrammingError(
driver_error="Inconsistent descriptor information",
ddbc_error=ddbc_error
), # Inconsistent descriptor information
"HY024": ProgrammingError(
driver_error="Invalid attribute value",
ddbc_error=ddbc_error
), # Invalid attribute value
"HY090": ProgrammingError(
driver_error="Invalid string or buffer length",
ddbc_error=ddbc_error
), # Invalid string or buffer length
"HY091": ProgrammingError(
driver_error="Invalid descriptor field identifier",
ddbc_error=ddbc_error
), # Invalid descriptor field identifier
"HY092": ProgrammingError(
driver_error="Invalid attribute/option identifier",
ddbc_error=ddbc_error
), # Invalid attribute/option identifier
"HY095": ProgrammingError(
driver_error="Function type out of range",
ddbc_error=ddbc_error
), # Function type out of range
"HY096": ProgrammingError(
driver_error="Invalid information type",
ddbc_error=ddbc_error
), # Invalid information type
"HY097": ProgrammingError(
driver_error="Column type out of range",
ddbc_error=ddbc_error
), # Column type out of range
"HY098": ProgrammingError(
driver_error="Scope type out of range",
ddbc_error=ddbc_error
), # Scope type out of range
"HY099": ProgrammingError(
driver_error="Nullable type out of range",
ddbc_error=ddbc_error
), # Nullable type out of range
"HY100": ProgrammingError(
driver_error="Uniqueness option type out of range",
ddbc_error=ddbc_error
), # Uniqueness option type out of range
"HY101": ProgrammingError(
driver_error="Accuracy option type out of range",
ddbc_error=ddbc_error
), # Accuracy option type out of range
"HY103": ProgrammingError(
driver_error="Invalid retrieval code",
ddbc_error=ddbc_error
), # Invalid retrieval code
"HY104": ProgrammingError(
driver_error="Invalid precision or scale value",
ddbc_error=ddbc_error
), # Invalid precision or scale value
"HY105": ProgrammingError(
driver_error="Invalid parameter type",
ddbc_error=ddbc_error
), # Invalid parameter type
"HY106": ProgrammingError(
driver_error="Fetch type out of range",
ddbc_error=ddbc_error
), # Fetch type out of range
"HY107": ProgrammingError(
driver_error="Row value out of range",
ddbc_error=ddbc_error
), # Row value out of range
"HY109": ProgrammingError(
driver_error="Invalid cursor position",
ddbc_error=ddbc_error
), # Invalid cursor position
"HY110": ProgrammingError(
driver_error="Invalid driver completion",
ddbc_error=ddbc_error
), # Invalid driver completion
"HY111": ProgrammingError(
driver_error="Invalid bookmark value",
ddbc_error=ddbc_error
), # Invalid bookmark value
"HYC00": NotSupportedError(
driver_error="Optional feature not implemented",
ddbc_error=ddbc_error
), # Optional feature not implemented
"HYT00": OperationalError(
driver_error="Timeout expired",
ddbc_error=ddbc_error
), # Timeout expired
"HYT01": OperationalError(
driver_error="Connection timeout expired",
ddbc_error=ddbc_error
), # Connection timeout expired
"IM001": NotSupportedError(
driver_error="Driver does not support this function",
ddbc_error=ddbc_error
), # Driver does not support this function
"IM002": OperationalError(
driver_error="Data source name not found and no default driver specified",
ddbc_error=ddbc_error,
), # Data source name not found and no default driver specified
"IM003": OperationalError(
driver_error="Specified driver could not be loaded",
ddbc_error=ddbc_error
), # Specified driver could not be loaded
"IM004": OperationalError(
driver_error="Driver's SQLAllocHandle on SQL_HANDLE_ENV failed",
ddbc_error=ddbc_error,
), # Driver's SQLAllocHandle on SQL_HANDLE_ENV failed
"IM005": OperationalError(
driver_error="Driver's SQLAllocHandle on SQL_HANDLE_DBC failed",
ddbc_error=ddbc_error,
), # Driver's SQLAllocHandle on SQL_HANDLE_DBC failed
"IM006": OperationalError(
driver_error="Driver's SQLSetConnectAttr failed",
ddbc_error=ddbc_error
), # Driver's SQLSetConnectAttr failed
"IM007": OperationalError(
driver_error="No data source or driver specified; dialog prohibited",
ddbc_error=ddbc_error,
), # No data source or driver specified; dialog prohibited
"IM008": OperationalError(
driver_error="Dialog failed",
ddbc_error=ddbc_error
), # Dialog failed
"IM009": OperationalError(
driver_error="Unable to load translation DLL",
ddbc_error=ddbc_error
), # Unable to load translation DLL
"IM010": OperationalError(
driver_error="Data source name too long",
ddbc_error=ddbc_error
), # Data source name too long
"IM011": OperationalError(
driver_error="Driver name too long",
ddbc_error=ddbc_error
), # Driver name too long
"IM012": OperationalError(
driver_error="DRIVER keyword syntax error",
ddbc_error=ddbc_error
), # DRIVER keyword syntax error
"IM013": OperationalError(
driver_error="Trace file error",
ddbc_error=ddbc_error
), # Trace file error
"IM014": OperationalError(
driver_error="Invalid name of File DSN",
ddbc_error=ddbc_error
), # Invalid name of File DSN
"IM015": OperationalError(
driver_error="Corrupt file data source",
ddbc_error=ddbc_error
), # Corrupt file data source
}
return mapping.get(sqlstate, None)
def truncate_error_message(error_message: str) -> str:
"""
- The Driver Error message is the message that is returned by the Internal driver.
- This section will always be at the start of the message.
"""
try:
if not error_message.startswith("[Microsoft]"):
# The message is not from the driver, so no need to truncate
return error_message
string_first = error_message[: error_message.index("]") + 1]
string_second = error_message[error_message.index("]") + 1 :]
string_third = string_second[string_second.index("]") + 1 :]
return string_first + string_third
except Exception as e:
if logger:
logger.error("Error while truncating error message: %s",e)
return error_message
def raise_exception(sqlstate: str, ddbc_error: str) -> None:
"""
Raise a custom exception based on the given SQLSTATE code.
This function raises a custom exception based on the provided SQLSTATE code.
If the code is not found in the mapping, a generic DatabaseError is raised.
Args:
sqlstate (str): The SQLSTATE code to map to a custom exception.
ddbc_error (str): The DDBC error message.
Raises:
DatabaseError: If the SQLSTATE code is not found in the mapping.
"""
exception_class = sqlstate_to_exception(sqlstate, ddbc_error)
if exception_class:
if logger:
logger.error(exception_class)
raise exception_class
raise DatabaseError(
driver_error=f"An error occurred with SQLSTATE code: {sqlstate}",
ddbc_error=f"{ddbc_error}" if ddbc_error else f"Unknown DDBC error",
)