Skip to content
Open
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
58 changes: 35 additions & 23 deletions src/core/include/openvino/core/except.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#pragma once

#include <filesystem>
#include <iostream>
#include <sstream>
#include <stdexcept>
Expand All @@ -13,6 +14,40 @@

namespace ov {

template <class T>
auto stringify(T&& arg) -> std::conditional_t<std::is_same_v<std::decay_t<T>, std::string>, T&, std::string> {
if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
return arg;
} else {
std::stringstream stream;
stream << arg;
return stream.str();
}
}

#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
OPENVINO_API std::string stringify(const std::filesystem::path& arg);
#endif

template <typename... TS>
std::ostream& write_all_to_stream(std::ostream& str, TS&&... args) {
if constexpr (std::is_same_v<typename std::filesystem::path::string_type, std::wstring>) {
constexpr auto fwd_or_str =
[](auto&& arg) -> std::conditional_t<std::is_same_v<std::filesystem::path, std::decay_t<decltype(arg)>>,
decltype(stringify(arg)),
decltype(arg)&> {
if constexpr (std::is_same_v<std::filesystem::path, std::decay_t<decltype(arg)>>) {
return stringify(arg);
} else {
return arg;
}
};
return (str << ... << (fwd_or_str(std::forward<TS>(args))));
} else {
return (str << ... << args);
}
}

/// Base error for ov runtime errors.
class OPENVINO_API Exception : public std::runtime_error {
public:
Expand All @@ -31,29 +66,6 @@ class OPENVINO_API Exception : public std::runtime_error {
const std::string& explanation);
};

static inline std::ostream& write_all_to_stream(std::ostream& str) {
return str;
}

template <typename T, typename... TS>
std::ostream& write_all_to_stream(std::ostream& str, T&& arg, TS&&... args) {
return write_all_to_stream(str << arg, std::forward<TS>(args)...);
}

template <class T,
typename std::enable_if<!std::is_same<typename std::decay<T>::type, std::string>::value>::type* = nullptr>
std::string stringify(T&& arg) {
std::stringstream stream;
stream << arg;
return stream.str();
}

template <class T,
typename std::enable_if<std::is_same<typename std::decay<T>::type, std::string>::value>::type* = nullptr>
T& stringify(T&& arg) {
return arg;
}

/// Base class for check failure exceptions.
class OPENVINO_API AssertFailure : public Exception {
public:
Expand Down
8 changes: 8 additions & 0 deletions src/core/src/except.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,11 @@ void ov::NotImplemented::create(const char* file, int line, const std::string& e
ov::NotImplemented::~NotImplemented() = default;

const std::string ov::NotImplemented::default_msg{"Not Implemented"};

namespace ov {
#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
std::string stringify(const std::filesystem::path& arg) {
return std::string("\"") + ov::util::path_to_string(arg) + '"';
}
#endif
} // namespace ov
16 changes: 16 additions & 0 deletions src/core/tests/check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <gtest/gtest.h>

#include "common_test_utils/file_utils.hpp"
#include "common_test_utils/test_assertions.hpp"
#include "openvino/core/except.hpp"
#include "openvino/util/file_util.hpp"
Expand Down Expand Up @@ -61,3 +62,18 @@ TEST(check, ov_throw_exception_check_relative_path_to_source) {
ov::Exception,
AnyOf(StartsWith(exp_native_slash), StartsWith(exp_fwd_slash)));
}

#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT)
TEST(check, error_message_with_fs_path_and_unicode) {
const auto path = ov::test::utils::to_fs_path("这是.folder") / ov::test::utils::to_fs_path(L"这.txt");
auto description = std::string("Error detail");
const auto exp_error_str = std::string("Test read file: \"这是.folder") +
ov::util::FileTraits<char>::file_separator +
std::string("这.txt\", because: Error detail");

std::stringstream error;
ov::write_all_to_stream(error, "Test read file: ", path, ", because: ", description);

EXPECT_EQ(error.str(), exp_error_str);
}
#endif
4 changes: 2 additions & 2 deletions src/inference/src/dev/core_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ ov::Plugin ov::CoreImpl::get_plugin(const std::string& plugin_name) const {
// Check that device plugin name is the same as requested for HW plugins
if (!plugin_name.empty() && !ov::is_virtual_device(plugin_name)) {
OPENVINO_ASSERT(device_name.find(plugin_name) != std::string::npos,
ov::util::path_to_string(desc.m_lib_location),
desc.m_lib_location,
" is used for ",
device_name,
" , while it contains implementation for ",
Expand Down Expand Up @@ -820,7 +820,7 @@ ov::Plugin ov::CoreImpl::get_plugin(const std::string& plugin_name) const {
return m_plugins.emplace(device_name, plugin).first->second;
} catch (const ov::Exception& ex) {
OPENVINO_THROW("Failed to create plugin ",
ov::util::path_to_string(desc.m_lib_location),
desc.m_lib_location,
" for device ",
device_name,
"\n",
Expand Down
Loading