Skip to content

Commit

Permalink
[CAPI] add ov_get_last_error_msg() API
Browse files Browse the repository at this point in the history
  • Loading branch information
riverlijunjie committed Oct 23, 2023
1 parent ec2ae00 commit f631566
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/bindings/c/include/openvino/c/ov_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,11 @@ ov_get_error_info(ov_status_e status);
*/
OPENVINO_C_API(void)
ov_free(const char* content);

/**
* @brief Get the last error msg.
* @ingroup ov_base_c_api
* @param none.
*/
OPENVINO_C_API(const char*)
ov_get_last_error_msg();
16 changes: 10 additions & 6 deletions src/bindings/c/src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
#include "openvino/core/except.hpp"
#include "openvino/openvino.hpp"

#define CATCH_IE_EXCEPTION(StatusCode, ExceptionType) \
catch (const InferenceEngine::ExceptionType&) { \
return ov_status_e::StatusCode; \
#define CATCH_IE_EXCEPTION(StatusCode, ExceptionType) \
catch (const InferenceEngine::ExceptionType& ex) { \
dup_last_err_msg(ex.what()); \
return ov_status_e::StatusCode; \
}

#define CATCH_OV_EXCEPTION(StatusCode, ExceptionType) \
catch (const ov::ExceptionType&) { \
return ov_status_e::StatusCode; \
#define CATCH_OV_EXCEPTION(StatusCode, ExceptionType) \
catch (const ov::ExceptionType& ex) { \
dup_last_err_msg(ex.what()); \
return ov_status_e::StatusCode; \
}

#define CATCH_OV_EXCEPTIONS \
Expand All @@ -41,6 +43,7 @@
CATCH_IE_EXCEPTION(NETWORK_NOT_READ, NetworkNotRead) \
CATCH_IE_EXCEPTION(INFER_CANCELLED, InferCancelled) \
catch (...) { \
dup_last_err_msg("An unknown exception occurred"); \
return ov_status_e::UNKNOW_EXCEPTION; \
}

Expand Down Expand Up @@ -224,3 +227,4 @@ struct mem_istream : virtual mem_stringbuf, std::istream {

char* str_to_char_array(const std::string& str);
ov::element::Type get_element_type(ov_element_type_e type);
void dup_last_err_msg(const std::string msg);
18 changes: 18 additions & 0 deletions src/bindings/c/src/ov_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ char* str_to_char_array(const std::string& str) {
return char_array;
}

static char* last_err_msg = nullptr;
static std::mutex last_msg_mutex;
inline void dup_last_err_msg(const std::string msg) {
std::lock_guard<std::mutex> lock(last_msg_mutex);
ov_free(last_err_msg);
last_err_msg = str_to_char_array(msg);
}

const char* ov_get_last_error_msg() {
std::lock_guard<std::mutex> lock(last_msg_mutex);
char* res = nullptr;
if (last_err_msg)
res = str_to_char_array(std::string(last_err_msg));
ov_free(last_err_msg);
last_err_msg = nullptr;
return res;
}

ov_status_e ov_get_openvino_version(ov_version_t* version) {
if (!version) {
return ov_status_e::INVALID_C_PARAM;
Expand Down
35 changes: 34 additions & 1 deletion src/bindings/c/tests/ov_core_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ov_core_test : public ov_capi_test_base {
ov_capi_test_base::TearDown();
}
};
INSTANTIATE_TEST_SUITE_P(device_name, ov_core_test, ::testing::Values("CPU"));
INSTANTIATE_TEST_SUITE_P(ov_capi_test, ov_core_test, ::testing::Values("CPU"));

TEST_P(ov_core_test, ov_core_create_with_config) {
std::string plugins_xml = TestDataHelpers::generate_test_xml_file();
Expand Down Expand Up @@ -699,4 +699,37 @@ TEST_P(ov_core_test, ov_core_compile_model_from_file_unicode) {
}
#endif

using ov_util_test = ov_core_test;
INSTANTIATE_TEST_SUITE_P(ov_capi_test, ov_util_test, ::testing::Values("CPU"));

TEST_P(ov_util_test, ov_get_last_error_msg_check) {
auto device_name = GetParam();
ov_core_t* core = nullptr;
OV_EXPECT_OK(ov_core_create(&core));
EXPECT_NE(nullptr, core);

const char* key = ov_property_key_inference_num_threads;
OV_EXPECT_OK(ov_core_set_property(core, device_name.c_str(), key, "abc"));

char* ret = nullptr;
OV_EXPECT_NOT_OK(ov_core_get_property(core, device_name.c_str(), key, &ret));

auto err_msg = ov_get_last_error_msg();
EXPECT_NE(nullptr, err_msg);
ov_free(err_msg);
ov_free(ret);
ov_core_free(core);
}

TEST_P(ov_util_test, ov_get_last_error_msg_check_empty_msg) {
auto device_name = GetParam();
ov_core_t* core = nullptr;
OV_EXPECT_OK(ov_core_create(&core));
EXPECT_NE(nullptr, core);

auto err_msg = ov_get_last_error_msg();
EXPECT_EQ(nullptr, err_msg);
ov_core_free(core);
}

} // namespace

0 comments on commit f631566

Please sign in to comment.