Skip to content

Commit 7b1b435

Browse files
committed
feat(errors): map SQLite errors in DBAPI2 exceptions
1 parent b25b140 commit 7b1b435

File tree

12 files changed

+252
-99
lines changed

12 files changed

+252
-99
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ main.dSYM/
1212
.DS_Store
1313

1414
.idea
15-
SqliteCloud.egg-info
15+
sqlitecloud.egg-info
1616

1717
playground.ipynb
1818

src/sqlitecloud/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
SQLiteCloudConfig,
99
SQLiteCloudConnect,
1010
SQLiteCloudDataTypes,
11-
SQLiteCloudException,
1211
)
1312
from sqlitecloud.driver import Driver
13+
from sqlitecloud.exceptions import SQLiteCloudException
1414
from sqlitecloud.resultset import SQLiteCloudResultSet
1515

1616

src/sqlitecloud/datatypes.py

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from typing import Any, Callable, Dict, Optional, Union
44
from urllib import parse
55

6+
from sqlitecloud.exceptions import SQLiteCloudException
7+
68
from .resultset import SQLiteCloudResultSet
79

810
# SQLite supported data types
@@ -248,34 +250,3 @@ def __init__(self) -> None:
248250
self.value: Optional[SQLiteCloudDataTypes] = None
249251
self.len: int = 0
250252
self.cellsize: int = 0
251-
252-
253-
class SQLiteCloudWarning(Exception):
254-
def __init__(self, message: str, code: int = -1, xerrcode: int = 0) -> None:
255-
super().__init__(message)
256-
self.errmsg = str(message)
257-
self.errcode = code
258-
self.xerrcode = xerrcode
259-
260-
261-
class SQLiteCloudError(Exception):
262-
def __init__(self, message: str, code: int = -1, xerrcode: int = 0) -> None:
263-
super().__init__(message)
264-
self.errmsg = str(message)
265-
self.errcode = code
266-
self.xerrcode = xerrcode
267-
268-
269-
# class SQLiteCloudInterfaceError(SQLiteCloudError):
270-
# def __init__(self, message: str, code: int = -1, xerrcode: int = 0) -> None:
271-
# super().__init__(message, code, xerrcode)
272-
273-
274-
# class SQLiteCloudDatabaseError(SQLiteCloudError):
275-
# def __init__(self, message: str, code: int = -1, xerrcode: int = 0) -> None:
276-
# super().__init__(message, code, xerrcode)
277-
278-
279-
class SQLiteCloudException(SQLiteCloudError):
280-
def __init__(self, message: str, code: int = -1, xerrcode: int = 0) -> None:
281-
super().__init__(message, code, xerrcode)

src/sqlitecloud/dbapi2.py

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,21 @@
2727
SQLiteCloudAccount,
2828
SQLiteCloudConfig,
2929
SQLiteCloudConnect,
30-
SQLiteCloudError,
31-
SQLiteCloudException,
32-
SQLiteCloudWarning,
3330
SQLiteDataTypes,
3431
)
3532
from sqlitecloud.driver import Driver
33+
from sqlitecloud.exceptions import (
34+
SQLiteCloudDatabaseError,
35+
SQLiteCloudDataError,
36+
SQLiteCloudError,
37+
SQLiteCloudIntegrityError,
38+
SQLiteCloudInterfaceError,
39+
SQLiteCloudInternalError,
40+
SQLiteCloudNotSupportedError,
41+
SQLiteCloudOperationalError,
42+
SQLiteCloudProgrammingError,
43+
SQLiteCloudWarning,
44+
)
3645
from sqlitecloud.resultset import (
3746
SQLITECLOUD_RESULT_TYPE,
3847
SQLITECLOUD_VALUE_TYPE,
@@ -54,14 +63,14 @@
5463

5564
Warning = SQLiteCloudWarning
5665
Error = SQLiteCloudError
57-
InterfaceError = SQLiteCloudException
58-
DatabaseError = SQLiteCloudException
59-
DataError = SQLiteCloudException
60-
OperationalError = SQLiteCloudException
61-
IntegrityError = SQLiteCloudException
62-
InternalError = SQLiteCloudException
63-
ProgrammingError = SQLiteCloudException
64-
NotSupportedError = SQLiteCloudException
66+
InterfaceError = SQLiteCloudInterfaceError
67+
DatabaseError = SQLiteCloudDatabaseError
68+
DataError = SQLiteCloudDataError
69+
OperationalError = SQLiteCloudOperationalError
70+
IntegrityError = SQLiteCloudIntegrityError
71+
InternalError = SQLiteCloudInternalError
72+
ProgrammingError = SQLiteCloudProgrammingError
73+
NotSupportedError = SQLiteCloudNotSupportedError
6574

6675
# Map for types for SQLite
6776
STRING = "TEXT"
@@ -254,9 +263,8 @@ def autocommit(self) -> bool:
254263

255264
@autocommit.setter
256265
def autocommit(self, value: bool) -> None:
257-
# TODO: raise NotSupportedError
258266
if not value:
259-
raise SQLiteCloudException("Disable Autocommit is not supported.")
267+
raise SQLiteCloudNotSupportedError("Disable Autocommit is not supported.")
260268

261269
def execute(
262270
self,
@@ -300,48 +308,37 @@ def executemany(
300308
return cursor.executemany(sql, seq_of_parameters)
301309

302310
def executescript(self, sql_script: str):
303-
# TODO: raise NotSupportedError
304-
raise SQLiteCloudException("executescript() is not supported.")
311+
raise SQLiteCloudNotSupportedError("executescript() is not supported.")
305312

306313
def create_function(self, name, num_params, func):
307-
# TODO: raise NotSupportedError
308-
raise SQLiteCloudException("create_function() is not supported.")
314+
raise SQLiteCloudNotSupportedError("create_function() is not supported.")
309315

310316
def create_aggregate(self, name, num_params, aggregate_class):
311-
# TODO: raise NotSupportedError
312-
raise SQLiteCloudException("create_aggregate() is not supported.")
317+
raise SQLiteCloudNotSupportedError("create_aggregate() is not supported.")
313318

314319
def create_collation(self, name, func):
315-
# TODO: raise NotSupportedError
316-
raise SQLiteCloudException("create_collation() is not supported.")
320+
raise SQLiteCloudNotSupportedError("create_collation() is not supported.")
317321

318322
def interrupt(self):
319-
# TODO: raise NotSupportedError
320-
raise SQLiteCloudException("interrupt() is not supported.")
323+
raise SQLiteCloudNotSupportedError("interrupt() is not supported.")
321324

322325
def set_authorizer(self, authorizer):
323-
# TODO: raise NotSupportedError
324-
raise SQLiteCloudException("set_authorizer() is not supported.")
326+
raise SQLiteCloudNotSupportedError("set_authorizer() is not supported.")
325327

326328
def set_progress_handler(self, handler, n):
327-
# TODO: raise NotSupportedError
328-
raise SQLiteCloudException("set_progress_handler() is not supported.")
329+
raise SQLiteCloudNotSupportedError("set_progress_handler() is not supported.")
329330

330331
def set_trace_callback(self, trace_callback):
331-
# TODO: raise NotSupportedError
332-
raise SQLiteCloudException("set_trace_callback() is not supported.")
332+
raise SQLiteCloudNotSupportedError("set_trace_callback() is not supported.")
333333

334334
def enable_load_extension(self, enable):
335-
# TODO: raise NotSupportedError
336-
raise SQLiteCloudException("enable_load_extension() is not supported.")
335+
raise SQLiteCloudNotSupportedError("enable_load_extension() is not supported.")
337336

338337
def load_extension(path):
339-
# TODO: raise NotSupportedError
340-
raise SQLiteCloudException("load_extension() is not supported.")
338+
raise SQLiteCloudNotSupportedError("load_extension() is not supported.")
341339

342340
def iterdump(self):
343-
# TODO: raise NotSupportedError
344-
raise SQLiteCloudException("iterdump() is not supported.")
341+
raise SQLiteCloudNotSupportedError("iterdump() is not supported.")
345342

346343
def close(self):
347344
"""
@@ -360,7 +357,7 @@ def commit(self):
360357
"""
361358
try:
362359
self._driver.execute("COMMIT;", self.sqlitecloud_connection)
363-
except SQLiteCloudException as e:
360+
except SQLiteCloudOperationalError as e:
364361
if (
365362
e.errcode == 1
366363
and e.xerrcode == 1
@@ -380,7 +377,7 @@ def rollback(self):
380377
"""
381378
try:
382379
self._driver.execute("ROLLBACK;", self.sqlitecloud_connection)
383-
except SQLiteCloudException as e:
380+
except SQLiteCloudOperationalError as e:
384381
if (
385382
e.errcode == 1
386383
and e.xerrcode == 1
@@ -696,20 +693,16 @@ def fetchall(self) -> List[Any]:
696693
return self.fetchmany(self.rowcount)
697694

698695
def setinputsizes(self, sizes) -> None:
699-
# TODO: raise NotSupportedError
700-
raise SQLiteCloudException("setinputsizes() is not supported.")
696+
raise SQLiteCloudNotSupportedError("setinputsizes() is not supported.")
701697

702698
def setoutputsize(self, size, column=None) -> None:
703-
# TODO: raise NotSupportedError
704-
raise SQLiteCloudException("setoutputsize() is not supported.")
699+
raise SQLiteCloudNotSupportedError("setoutputsize() is not supported.")
705700

706701
def scroll(value, mode="relative"):
707-
# TODO: raise NotSupportedError
708-
raise SQLiteCloudException("scroll() is not supported.")
702+
raise SQLiteCloudNotSupportedError("scroll() is not supported.")
709703

710704
def messages(self):
711-
# TODO: raise NotSupportedError
712-
raise SQLiteCloudException("messages() is not supported.")
705+
raise SQLiteCloudNotSupportedError("messages() is not supported.")
713706

714707
def _call_row_factory(self, row: Tuple) -> object:
715708
if self.row_factory is None:
@@ -737,7 +730,7 @@ def _ensure_connection(self):
737730
SQLiteCloudException: If the cursor is closed.
738731
"""
739732
if not self._connection:
740-
raise SQLiteCloudException("The cursor is closed.")
733+
raise SQLiteCloudOperationalError("The cursor is closed.")
741734

742735
def _adapt_parameters(self, parameters: Union[Dict, Tuple]) -> Union[Dict, Tuple]:
743736
if isinstance(parameters, dict):

src/sqlitecloud/driver.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
SQLITECLOUD_ROWSET,
1717
SQLiteCloudConfig,
1818
SQLiteCloudConnect,
19-
SQLiteCloudException,
2019
SQLiteCloudNumber,
2120
SQLiteCloudRowsetSignature,
2221
SQLiteCloudValue,
2322
SQLiteDataTypes,
2423
)
24+
from sqlitecloud.exceptions import (
25+
SQLiteCloudException,
26+
raise_sqlitecloud_error_with_extended_code,
27+
)
2528
from sqlitecloud.resultset import (
2629
SQLITECLOUD_RESULT_TYPE,
2730
SQLiteCloudOperationResult,
@@ -485,6 +488,12 @@ def _internal_socket_write(
485488
command (bytes): The command to send.
486489
main_socket (bool): If True, write to the main socket, otherwise write to the pubsub socket.
487490
"""
491+
# try:
492+
# if "ATTACH DATABASE" in command.decode() or '"test_schema".table_info' in command.decode():
493+
# pdb.set_trace()
494+
# except:
495+
# pass
496+
488497
# write buffer
489498
if len(command) == 0:
490499
return
@@ -712,7 +721,9 @@ def _internal_parse_buffer(
712721
len_ -= cstart2
713722
errmsg = clone[cstart2:]
714723

715-
raise SQLiteCloudException(errmsg.decode(), errcode, xerrcode)
724+
raise raise_sqlitecloud_error_with_extended_code(
725+
errmsg.decode(), errcode, xerrcode
726+
)
716727

717728
elif cmd in [SQLITECLOUD_CMD.ROWSET.value, SQLITECLOUD_CMD.ROWSET_CHUNK.value]:
718729
# CMD_ROWSET: *LEN 0:VERSION ROWS COLS DATA

0 commit comments

Comments
 (0)