Skip to content

Commit 4bb1b25

Browse files
committed
Return error when loading fails instead of panicking
This refactoring changes the public API of the `openvino` crate to add a new error type, `LoadingError`, as well as a wrapper type, `SetupError`, to disambiguate between that and `InferenceError`. The `Core::new` function changes to return the wrapper, `SetupError`, since it could fail while loading the OpenVINO shared libraries.
1 parent f9c34ad commit 4bb1b25

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

crates/openvino/src/core.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
//! [API](https://docs.openvinotoolkit.org/latest/ie_c_api/modules.html).
33
44
use crate::blob::Blob;
5-
use crate::network::{CNNNetwork, ExecutableNetwork};
65
use crate::tensor_desc::TensorDesc;
76
use crate::{cstr, drop_using_function, try_unsafe, util::Result};
7+
use crate::{
8+
error::{LoadingError, SetupError},
9+
network::{CNNNetwork, ExecutableNetwork},
10+
};
811
use crate::{Layout, Precision};
912
use openvino_sys::{
1013
self, ie_config_t, ie_core_create, ie_core_free, ie_core_load_network, ie_core_read_network,
@@ -19,17 +22,18 @@ drop_using_function!(Core, ie_core_free);
1922

2023
impl Core {
2124
/// Construct a new OpenVINO [Core]--this is the primary entrypoint for constructing and using
22-
/// inference networks.
23-
pub fn new(xml_config_file: Option<&str>) -> Result<Core> {
24-
openvino_sys::library::load().expect("unable to load shared library");
25+
/// inference networks. Because this function may load OpenVINO's shared libraries at runtime,
26+
/// there are more ways than usual that this function can fail (e.g., [LoadingError]s).
27+
pub fn new(xml_config_file: Option<&str>) -> std::result::Result<Core, SetupError> {
28+
openvino_sys::library::load().or_else(|e| Err(LoadingError::SystemFailure(e)))?;
2529

2630
let file = match xml_config_file {
2731
None => format!(
2832
"{}/plugins.xml",
2933
openvino_sys::library::find()
30-
.expect("unable to find path to OpenVINO libraries")
34+
.ok_or(LoadingError::CannotFindPath)?
3135
.parent()
32-
.expect("unable to get the parent of the linked OpenVINO library")
36+
.ok_or(LoadingError::NoParentDirectory)?
3337
.display()
3438
),
3539
Some(f) => f.to_string(),

crates/openvino/src/error.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
use thiserror::Error;
22

3-
/// See
3+
/// Enumerate errors returned by the OpenVINO implementation. See
44
/// [IEStatusCode](https://docs.openvinotoolkit.org/latest/ie_c_api/ie__c__api_8h.html#a391683b1e8e26df8b58d7033edd9ee83).
5-
///
6-
/// TODO Replace this in bindgen with
7-
/// [newtype_enum](https://docs.rs/bindgen/0.54.1/bindgen/struct.Builder.html#method.newtype_enum)
8-
/// or
9-
/// [rustified_enum](https://docs.rs/bindgen/0.54.1/bindgen/struct.Builder.html#method.rustified_enum).
5+
/// TODO This could be auto-generated (https://github.com/intel/openvino-rs/issues/20).
106
#[derive(Debug, Error)]
117
pub enum InferenceError {
128
#[error("general error")]
@@ -58,3 +54,25 @@ impl InferenceError {
5854
}
5955
}
6056
}
57+
58+
/// Enumberate setup failures: in some cases, this library calls library loading code that may fail
59+
/// in a different way (i.e., [LoadingError]) than the calls in to the OpenVINO libraries (i.e.,
60+
/// [InferenceError]).
61+
#[derive(Debug, Error)]
62+
pub enum SetupError {
63+
#[error("inference error")]
64+
Inference(#[from] InferenceError),
65+
#[error("library loading error")]
66+
Loading(#[from] LoadingError),
67+
}
68+
69+
/// Enumerate the ways that library loading can fail.
70+
#[derive(Debug, Error)]
71+
pub enum LoadingError {
72+
#[error("system failed to load shared libraries (see https://github.com/intel/openvino-rs/blob/main/crates/openvino-finder): {0}")]
73+
SystemFailure(String),
74+
#[error("cannot find path to shared libraries (see https://github.com/intel/openvino-rs/blob/main/crates/openvino-finder)")]
75+
CannotFindPath,
76+
#[error("no parent directory found for shared library path")]
77+
NoParentDirectory,
78+
}

crates/openvino/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod util;
88

99
pub use crate::core::Core;
1010
pub use blob::Blob;
11-
pub use error::InferenceError;
11+
pub use error::{InferenceError, LoadingError, SetupError};
1212
pub use network::{CNNNetwork, ExecutableNetwork};
1313
// Re-publish some OpenVINO enums with a conventional Rust naming (see
1414
// `crates/openvino-sys/build.rs`).

0 commit comments

Comments
 (0)