Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: catch std::stoi exception and improve error msg #36296

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions internal/core/src/index/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,14 @@
GetDimFromConfig(const Config& config) {
auto dimension = GetValueFromConfig<std::string>(config, "dim");
AssertInfo(dimension.has_value(), "dimension not exist in config");
return (std::stoi(dimension.value()));
try {
return (std::stoi(dimension.value()));
} catch (const std::logic_error& e) {
auto err_message = fmt::format(
"invalided dimension:{}, error:{}", dimension.value(), e.what());
LOG_ERROR(err_message);
throw std::logic_error(err_message);

Check warning on line 137 in internal/core/src/index/Utils.cpp

View check run for this annotation

Codecov / codecov/patch

internal/core/src/index/Utils.cpp#L133-L137

Added lines #L133 - L137 were not covered by tests
}
}

std::string
Expand All @@ -151,7 +158,16 @@
GetValueFromConfig<std::string>(config, INDEX_ENGINE_VERSION);
AssertInfo(index_engine_version.has_value(),
"index_engine not exist in config");
return (std::stoi(index_engine_version.value()));
try {
return (std::stoi(index_engine_version.value()));
} catch (const std::logic_error& e) {
auto err_message =

Check warning on line 164 in internal/core/src/index/Utils.cpp

View check run for this annotation

Codecov / codecov/patch

internal/core/src/index/Utils.cpp#L163-L164

Added lines #L163 - L164 were not covered by tests
fmt::format("invalided index engine version:{}, error:{}",
index_engine_version.value(),
e.what());
LOG_ERROR(err_message);
throw std::logic_error(err_message);

Check warning on line 169 in internal/core/src/index/Utils.cpp

View check run for this annotation

Codecov / codecov/patch

internal/core/src/index/Utils.cpp#L167-L169

Added lines #L167 - L169 were not covered by tests
}
}

// TODO :: too ugly
Expand Down
28 changes: 22 additions & 6 deletions internal/core/src/storage/DiskFileManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,17 @@

std::map<std::string, std::vector<int>> index_slices;
for (auto& file_path : remote_files) {
auto pos = file_path.find_last_of("_");
index_slices[file_path.substr(0, pos)].emplace_back(
std::stoi(file_path.substr(pos + 1)));
auto pos = file_path.find_last_of('_');
AssertInfo(pos > 0, "invalided index file path:{}", file_path);
try {
auto idx = std::stoi(file_path.substr(pos + 1));
index_slices[file_path.substr(0, pos)].emplace_back(idx);
} catch (const std::logic_error& e) {
auto err_message = fmt::format(
"invalided index file path:{}, error:{}", file_path, e.what());
LOG_ERROR(err_message);
throw std::logic_error(err_message);

Check warning on line 248 in internal/core/src/storage/DiskFileManagerImpl.cpp

View check run for this annotation

Codecov / codecov/patch

internal/core/src/storage/DiskFileManagerImpl.cpp#L244-L248

Added lines #L244 - L248 were not covered by tests
}
}

for (auto& slices : index_slices) {
Expand Down Expand Up @@ -291,9 +299,17 @@

std::map<std::string, std::vector<int>> index_slices;
for (auto& file_path : remote_files) {
auto pos = file_path.find_last_of('_');
index_slices[file_path.substr(0, pos)].emplace_back(
std::stoi(file_path.substr(pos + 1)));
auto pos = file_path.find_last_of("_");
AssertInfo(pos > 0, "invalided index file path:{}", file_path);
try {
auto idx = std::stoi(file_path.substr(pos + 1));
index_slices[file_path.substr(0, pos)].emplace_back(idx);
} catch (const std::logic_error& e) {
auto err_message = fmt::format(
"invalided index file path:{}, error:{}", file_path, e.what());
LOG_ERROR(err_message);
throw std::logic_error(err_message);

Check warning on line 311 in internal/core/src/storage/DiskFileManagerImpl.cpp

View check run for this annotation

Codecov / codecov/patch

internal/core/src/storage/DiskFileManagerImpl.cpp#L307-L311

Added lines #L307 - L311 were not covered by tests
}
}

for (auto& slices : index_slices) {
Expand Down
Loading