mssql-python 0.1.6__cp313-cp313-win_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of mssql-python might be problematic. Click here for more details.

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