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