Skip to content

Commit 4e419ef

Browse files
fix(workspace): prevent invalid exception when opening prefix in RW mode fails (#587)
Fixed issue #334 where opening a locked prefix in read-write mode would throw exception and Workspace open silences exception and returns null. Right now null is returned only when PrefixNotFoundException is thrown. Changes: - Added new exception type PrefixNotFoundException for clearer error handling - Modified PrefixCatalog, Workspace, and PyEnumType to throw PrefixNotFoundException instead of InputException when prefix does not exist - Improved error handling in Workspace::open() to properly clean up incomplete files for all exception types while preserving the original exception for non-prefix-not-found cases Fixes #334
1 parent d067aae commit 4e419ef

5 files changed

Lines changed: 25 additions & 7 deletions

File tree

src/dbzero/bindings/python/types/PyEnumType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ namespace db0::python
124124
auto prefix_name_ptr = m_enum_type_def->getPrefixNamePtr();
125125
if (prefix_name_ptr) {
126126
// make the error message more informative
127-
THROWF(db0::InputException) << "Prefix does not exist or unable to open: " << prefix_name_ptr;
127+
THROWF(db0::PrefixNotFoundException) << "Prefix does not exist or unable to open: " << prefix_name_ptr;
128128
}
129129
THROWF(db0::InputException) << "Unable to resolve the scope of: " << *m_enum_type_def;
130130
}

src/dbzero/core/exception/Exceptions.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,9 @@ namespace db0
7272
{
7373
}
7474

75+
PrefixNotFoundException::PrefixNotFoundException()
76+
: RecoverableException(exception_id)
77+
{
78+
}
79+
7580
}

src/dbzero/core/exception/Exceptions.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,11 @@ namespace db0
128128
CacheException();
129129
};
130130

131+
class PrefixNotFoundException: public RecoverableException
132+
{
133+
public:
134+
static constexpr int exception_id = EXCEPTION_ID_PREFIX::BASIC | 0x10;
135+
PrefixNotFoundException();
136+
};
137+
131138
}

src/dbzero/workspace/PrefixCatalog.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ namespace db0
5353
auto file_name = getFileName(prefix_name);
5454
bool file_exists = CFile::exists(file_name.string());
5555
if (!if_exists && !file_exists) {
56-
THROWF(db0::InputException) << "Prefix does not exist: " << prefix_name;
56+
THROWF(db0::PrefixNotFoundException) << "Prefix does not exist: " << prefix_name;
5757
}
5858
if (file_exists) {
5959
std::remove(file_name.string().c_str());
@@ -203,7 +203,7 @@ namespace db0
203203
fs::path FixtureCatalog::getPrefixFileName(const PrefixName &prefix_name) const
204204
{
205205
if (!m_prefix_catalog.exists(prefix_name)) {
206-
THROWF(db0::InputException) << "Prefix does not exist: " << prefix_name;
206+
THROWF(db0::PrefixNotFoundException) << "Prefix does not exist: " << prefix_name;
207207
}
208208
return m_prefix_catalog.getFileName(prefix_name);
209209
}

src/dbzero/workspace/Workspace.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ namespace db0
5151
if (!m_prefix_catalog.exists(prefix_name)) {
5252
// create new file if READ-WRITE access permitted
5353
if (access_type == AccessType::READ_ONLY) {
54-
THROWF(db0::InputException) << "Prefix does not exist: " << prefix_name;
54+
THROWF(db0::PrefixNotFoundException) << "Prefix does not exist: " << prefix_name;
5555
}
5656

5757
BDevStorage::create(file_name, *page_size, *sparse_index_node_size, page_io_step_size);
@@ -390,13 +390,19 @@ namespace db0
390390
m_on_open_callback(it->second, file_created);
391391
}
392392
}
393-
} catch (std::exception &ex) {
393+
} catch (db0::PrefixNotFoundException &ex) {
394394
if (file_created) {
395395
// remove incomplete file
396396
m_fixture_catalog.drop(prefix_name);
397397
}
398-
std::cerr << "Error opening prefix '" << prefix_name << "': " << ex.what() << std::endl;
399-
return nullptr;
398+
return nullptr;
399+
}
400+
catch (std::exception &ex) {
401+
if (file_created) {
402+
// remove incomplete file
403+
m_fixture_catalog.drop(prefix_name);
404+
}
405+
throw;
400406
}
401407

402408
// Validate access type

0 commit comments

Comments
 (0)