Skip to content

Commit 2eea71e

Browse files
committed
Pymssql backend: Nicer error msg
1 parent ec6df99 commit 2eea71e

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

Orange/data/sql/backend/mssql.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
from Orange.data.sql.backend.base import ToSql, BackendError
1010

1111

12+
def parse_ex(ex: Exception) -> str:
13+
try:
14+
return ex.args[0][1].decode().splitlines()[-1]
15+
except: # pylint: disable=bare-except
16+
return str(ex)
17+
18+
1219
class PymssqlBackend(Backend):
1320
display_name = "SQL Server"
1421

@@ -23,7 +30,7 @@ def __init__(self, connection_params):
2330
try:
2431
self.connection = pymssql.connect(login_timeout=5, **connection_params)
2532
except pymssql.Error as ex:
26-
raise BackendError(str(ex)) from ex
33+
raise BackendError(parse_ex(ex)) from ex
2734
except ValueError:
2835
# ValueError is raised when 'server' contains "\\"
2936
raise BackendError("Incorrect format of connection details")
@@ -76,7 +83,7 @@ def execute_sql_query(self, query, params=()):
7683
cur.execute(query, *params)
7784
yield cur
7885
except pymssql.Error as ex:
79-
raise BackendError(str(ex)) from ex
86+
raise BackendError(parse_ex(ex)) from ex
8087

8188
def create_variable(self, field_name, field_metadata, type_hints, inspect_table=None):
8289
if field_name in type_hints:
@@ -144,4 +151,4 @@ def count_approx(self, query):
144151
if "SHOWPLAN permission denied" in str(ex):
145152
warnings.warn("SHOWPLAN permission denied, count approximates will not be used")
146153
return None
147-
raise BackendError(str(ex)) from ex
154+
raise BackendError(parse_ex(ex)) from ex
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import unittest
2+
3+
from Orange.data.sql.backend.base import BackendError
4+
from Orange.data.sql.backend.mssql import PymssqlBackend, parse_ex
5+
6+
7+
class TestPymssqlBackend(unittest.TestCase):
8+
def test_connection_error(self):
9+
connection_params = {"host": "host", "port": "", "database": "DB"}
10+
self.assertRaises(BackendError, PymssqlBackend, connection_params)
11+
12+
def test_parse_ex(self):
13+
err_msg = "Foo"
14+
self.assertEqual(parse_ex(ValueError(err_msg)), err_msg)
15+
16+
17+
if __name__ == '__main__':
18+
unittest.main()

0 commit comments

Comments
 (0)