From 817fe486d6c53bc6e4e07d433df020d37f6ca971 Mon Sep 17 00:00:00 2001 From: jaime Date: Wed, 18 Sep 2024 17:29:11 +0800 Subject: [PATCH] enhance: catch std::stoi exception and improve error msg (#36296) issue: #36255 pr: #36267 Signed-off-by: jaime --- internal/core/src/index/Utils.cpp | 20 +++++++++++-- .../core/src/storage/DiskFileManagerImpl.cpp | 28 +++++++++++++++---- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/internal/core/src/index/Utils.cpp b/internal/core/src/index/Utils.cpp index b225e57a8d..53d24e408d 100644 --- a/internal/core/src/index/Utils.cpp +++ b/internal/core/src/index/Utils.cpp @@ -128,7 +128,14 @@ int64_t GetDimFromConfig(const Config& config) { auto dimension = GetValueFromConfig(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); + } } std::string @@ -151,7 +158,16 @@ GetIndexEngineVersionFromConfig(const Config& config) { GetValueFromConfig(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 = + fmt::format("invalided index engine version:{}, error:{}", + index_engine_version.value(), + e.what()); + LOG_ERROR(err_message); + throw std::logic_error(err_message); + } } // TODO :: too ugly diff --git a/internal/core/src/storage/DiskFileManagerImpl.cpp b/internal/core/src/storage/DiskFileManagerImpl.cpp index 4865a72c34..b20f8daa5c 100644 --- a/internal/core/src/storage/DiskFileManagerImpl.cpp +++ b/internal/core/src/storage/DiskFileManagerImpl.cpp @@ -236,9 +236,17 @@ DiskFileManagerImpl::CacheIndexToDisk() { std::map> 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); + } } for (auto& slices : index_slices) { @@ -291,9 +299,17 @@ DiskFileManagerImpl::CacheIndexToDisk( std::map> 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); + } } for (auto& slices : index_slices) {