Skip to content

Commit 52e3edc

Browse files
api: exception rethrow in crud
Before this patch, all DatabaseError exception, except for ER_NO_SUCH_PROC and ER_ACCESS_DENIED ones, were silently ignored. It resulted in "UnboundLocalError: local variable 'crud_resp' referenced before assignment" instead of a proper error.
1 parent 223e16d commit 52e3edc

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## Unreleased
8+
9+
### Fixed
10+
- Exception rethrow in crud API (PR #310).
11+
712
## 1.1.1 - 2023-07-19
813

914
### Changed

tarantool/crud.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,6 @@ def call_crud(conn, *args):
6969
if exc.code in (ER_NO_SUCH_PROC, ER_ACCESS_DENIED):
7070
exc_msg = ". Ensure that you're calling crud.router and user has sufficient grants"
7171
raise DatabaseError(exc.code, exc.message + exc_msg, extra_info=exc.extra_info) from exc
72+
raise exc
7273

7374
return crud_resp

test/suites/crud_mock_server.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env tarantool
2+
3+
local admin_listen = os.getenv("ADMIN")
4+
local primary_listen = os.getenv("LISTEN")
5+
6+
require('console').listen(admin_listen)
7+
box.cfg{
8+
listen = primary_listen,
9+
memtx_memory = 0.1 * 1024^3, -- 0.1 GiB
10+
pid_file = "box.pid",
11+
}
12+
13+
box.schema.user.grant('guest', 'execute', 'universe', nil, {if_not_exists = true})
14+
15+
local function mock_replace()
16+
error('Unexpected connection error')
17+
end
18+
19+
rawset(_G, 'crud', {replace = mock_replace})
20+
21+
rawset(_G, 'ready', true)

test/suites/test_crud.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ def create_server():
2222
return srv
2323

2424

25+
def create_mock_server():
26+
srv = TarantoolServer()
27+
srv.script = 'test/suites/crud_mock_server.lua'
28+
srv.start()
29+
30+
return srv
31+
32+
2533
@unittest.skipIf(sys.platform.startswith("win"),
2634
"Crud tests on windows platform are not supported: "
2735
"complexity of the vshard replicaset configuration")
@@ -33,14 +41,19 @@ def setUpClass(cls):
3341
print('-' * 70, file=sys.stderr)
3442
# Create server and extract helpful fields for tests.
3543
cls.srv = create_server()
44+
cls.mock_srv = create_mock_server()
3645
cls.host = cls.srv.host
3746
cls.port = cls.srv.args['primary']
47+
cls.mock_host = cls.mock_srv.host
48+
cls.mock_port = cls.mock_srv.args['primary']
3849

3950
def setUp(self):
4051
time.sleep(1)
4152
# Open connections to instance.
4253
self.conn = tarantool.Connection(host=self.host, port=self.port,
4354
user='guest', password='', fetch_schema=False)
55+
self.mock_conn = tarantool.Connection(host=self.mock_host, port=self.mock_port,
56+
user='guest', password='', fetch_schema=False)
4457
self.conn_mesh = tarantool.MeshConnection(host=self.host, port=self.port,
4558
user='guest', password='', fetch_schema=False)
4659
self.conn_pool = tarantool.ConnectionPool([{'host': self.host, 'port': self.port}],
@@ -736,9 +749,15 @@ def test_crud_module_via_pool_connection(self):
736749
# Exception try testing.
737750
self._exception_operation_with_crud(testing_function, case, mode=tarantool.Mode.RW)
738751

752+
def test_error_rethrow(self):
753+
self.assertRaisesRegex(
754+
DatabaseError, "Unexpected connection error",
755+
lambda: self.mock_conn.crud_replace('tester', [2, 100, 'Alice'], {'timeout': 10}))
756+
739757
def tearDown(self):
740758
# Close connections to instance.
741759
self.conn.close()
760+
self.mock_conn.close()
742761
self.conn_mesh.close()
743762
self.conn_pool.close()
744763

@@ -747,3 +766,5 @@ def tearDownClass(cls):
747766
# Stop instance.
748767
cls.srv.stop()
749768
cls.srv.clean()
769+
cls.mock_srv.stop()
770+
cls.mock_srv.clean()

0 commit comments

Comments
 (0)