diff --git a/src/common/file_utils.cpp b/src/common/file_utils.cpp index 7034891ba3..64a796a161 100644 --- a/src/common/file_utils.cpp +++ b/src/common/file_utils.cpp @@ -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())); } } diff --git a/test/main/config_test.cpp b/test/main/config_test.cpp index 8270e4c544..b7a46a2abf 100644 --- a/test/main/config_test.cpp +++ b/test/main/config_test.cpp @@ -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(DatabaseConfig("/\\0:* /? \" < > |")); + FAIL(); + } catch (Exception& e) { + ASSERT_TRUE(string(e.what()).find("Failed to create directory") != string::npos); + } +} diff --git a/tools/python_api/test/test_exception.py b/tools/python_api/test/test_exception.py index c74d731704..298e6ec62d 100644 --- a/tools/python_api/test/test_exception.py +++ b/tools/python_api/test/test_exception.py @@ -1,4 +1,7 @@ import pytest +import sys +sys.path.append('../build/') +import kuzu def test_exception(establish_connection): @@ -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)) diff --git a/tools/shell/shell_runner.cpp b/tools/shell/shell_runner.cpp index dcf4a0685f..df7f89b695 100644 --- a/tools/shell/shell_runner.cpp +++ b/tools/shell/shell_runner.cpp @@ -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; }