Skip to content

Commit

Permalink
[1033] Throw exception if the database path directory is wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
anuchak committed Jan 4, 2023
1 parent 29967a4 commit 77e36b7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/common/file_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,18 @@ void FileUtils::readFromFile(
}

void FileUtils::createDir(const string& dir) {
if (filesystem::exists(dir)) {
throw Exception(StringUtils::string_format("Directory %s already exists.", dir.c_str()));
}
if (!filesystem::create_directory(dir)) {
try {
if (filesystem::exists(dir)) {
throw Exception(
StringUtils::string_format("Directory %s already exists.", dir.c_str()));
}
if (!filesystem::create_directory(dir)) {
throw Exception(StringUtils::string_format(
"Directory %s cannot be created. Check if it exists and remove it.", dir.c_str()));
}
} catch (exception& e) {
throw Exception(StringUtils::string_format(
"Directory %s cannot be created. Check if it exists and remove it.", dir.c_str()));
"Failed to create directory %s due to: %s", dir.c_str(), e.what()));
}
}

Expand Down
10 changes: 10 additions & 0 deletions test/main/config_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ TEST_F(ApiTest, ClientConfig) {
ASSERT_NO_THROW(conn->setMaxNumThreadForExec(2));
ASSERT_EQ(conn->getMaxNumThreadForExec(), 2);
}

TEST_F(ApiTest, DatabasePathIncorrect) {
spdlog::set_level(spdlog::level::debug);
try {
make_unique<Database>(DatabaseConfig("/\\0:* /? \" < > |"));
FAIL();
} catch (Exception& e) {
ASSERT_TRUE(string(e.what()).find("Failed to create directory") != string::npos);
}
}
9 changes: 9 additions & 0 deletions tools/python_api/test/test_exception.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import pytest
import sys
sys.path.append('../build/')
import kuzu


def test_exception(establish_connection):
Expand All @@ -13,3 +16,9 @@ def test_exception(establish_connection):
with pytest.raises(RuntimeError,
match="Buffer manager exception: Resizing to a smaller Buffer Pool Size is unsupported."):
db.resize_buffer_manager(1)


def test_db_path_exception():
path = '/:* /? " < > |'
with pytest.raises(RuntimeError, match='filesystem error'):
kuzu.Database(str(path))
9 changes: 7 additions & 2 deletions tools/shell/shell_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ int main(int argc, char* argv[]) {
cout << "Enter \":help\" for usage hints." << endl;
SystemConfig systemConfig(bpSizeInMB << 20);
DatabaseConfig databaseConfig(databasePath, inMemoryFlag);
auto shell = EmbeddedShell(databaseConfig, systemConfig);
shell.run();
try {
auto shell = EmbeddedShell(databaseConfig, systemConfig);
shell.run();
} catch (exception &e) {
cerr << e.what() << endl;
return 1;
}
return 0;
}

0 comments on commit 77e36b7

Please sign in to comment.