19
19
20
20
import sqlite3
21
21
22
- from .exceptions import NumbatException
22
+ from .exceptions import DBException , NumbatException
23
23
from .types import (
24
24
ComponentAccess ,
25
25
ComponentAccessType ,
@@ -60,7 +60,11 @@ def connect(path: str) -> sqlite3.Connection:
60
60
:return: A connection handle that can be used for future
61
61
operation on the database
62
62
"""
63
- return sqlite3 .connect (path )
63
+ try :
64
+ res = sqlite3 .connect (path )
65
+ except sqlite3 .Error as e :
66
+ raise DBException (f"Error while connecting to DB: { e } " ) from e
67
+ return res
64
68
65
69
@staticmethod
66
70
def exec (
@@ -75,11 +79,14 @@ def exec(
75
79
:return: The id of the last modified row (useful in case insertion)
76
80
"""
77
81
if not database :
78
- raise Exception ("Invalid database handle" )
79
-
80
- cur = database .cursor ()
81
- cur .execute (request , parameters )
82
- cur .close ()
82
+ raise DBException ("Invalid database handle" )
83
+
84
+ try :
85
+ cur = database .cursor ()
86
+ cur .execute (request , parameters )
87
+ cur .close ()
88
+ except sqlite3 .Error as e :
89
+ raise DBException (f"Error while excuting request to DB: { e } " ) from e
83
90
return cur .lastrowid
84
91
85
92
@staticmethod
@@ -97,10 +104,15 @@ def fetch(
97
104
if not database :
98
105
raise Exception ("Invalid database handle" )
99
106
100
- cur = database .cursor ()
101
- cur .execute (request , parameters )
102
- result = cur .fetchall ()
103
- cur .close ()
107
+ try :
108
+ cur = database .cursor ()
109
+ cur .execute (request , parameters )
110
+ result = cur .fetchall ()
111
+ cur .close ()
112
+ except sqlite3 .Error as e :
113
+ raise DBException (
114
+ f"Error while excuting a request to DB and fetching result: { e } "
115
+ ) from e
104
116
105
117
return result
106
118
0 commit comments