Skip to main content

openvino_genai/
error.rs

1use openvino_genai_sys::ov_status_e;
2use std::error::Error;
3use std::fmt;
4
5/// See
6/// [`ov_status_e`](https://docs.openvino.ai/2024/api/c_cpp_api/group__ov__base__c__api.html#_CPPv411ov_status_e);
7/// enumerates errors returned by the OpenVINO GenAI implementation.
8#[allow(missing_docs)]
9#[derive(Debug, PartialEq, Eq)]
10pub enum InferenceError {
11    GeneralError,
12    NotImplemented,
13    NetworkNotLoaded,
14    ParameterMismatch,
15    NotFound,
16    OutOfBounds,
17    Unexpected,
18    RequestBusy,
19    ResultNotReady,
20    NotAllocated,
21    InferNotStarted,
22    NetworkNotRead,
23    InferCancelled,
24    InvalidCParam,
25    UnknownCError,
26    NotImplementCMethod,
27    UnknownException,
28    Undefined(i32),
29}
30
31impl InferenceError {
32    /// Convert an `openvino_genai_sys` error to a [`Result`]:
33    /// - `0` becomes `Ok`
34    /// - anything else becomes `Err` containing an [`InferenceError`]
35    pub fn convert(status: ov_status_e) -> Result<(), InferenceError> {
36        match status {
37            ov_status_e::OK => Ok(()),
38            ov_status_e::GENERAL_ERROR => Err(Self::GeneralError),
39            ov_status_e::NOT_IMPLEMENTED => Err(Self::NotImplemented),
40            ov_status_e::NETWORK_NOT_LOADED => Err(Self::NetworkNotLoaded),
41            ov_status_e::PARAMETER_MISMATCH => Err(Self::ParameterMismatch),
42            ov_status_e::NOT_FOUND => Err(Self::NotFound),
43            ov_status_e::OUT_OF_BOUNDS => Err(Self::OutOfBounds),
44            ov_status_e::UNEXPECTED => Err(Self::Unexpected),
45            ov_status_e::REQUEST_BUSY => Err(Self::RequestBusy),
46            ov_status_e::RESULT_NOT_READY => Err(Self::ResultNotReady),
47            ov_status_e::NOT_ALLOCATED => Err(Self::NotAllocated),
48            ov_status_e::INFER_NOT_STARTED => Err(Self::InferNotStarted),
49            ov_status_e::NETWORK_NOT_READ => Err(Self::NetworkNotRead),
50            ov_status_e::INFER_CANCELLED => Err(Self::InferCancelled),
51            ov_status_e::INVALID_C_PARAM => Err(Self::InvalidCParam),
52            ov_status_e::UNKNOWN_C_ERROR => Err(Self::UnknownCError),
53            ov_status_e::NOT_IMPLEMENT_C_METHOD => Err(Self::NotImplementCMethod),
54            ov_status_e::UNKNOW_EXCEPTION => Err(Self::UnknownException),
55        }
56    }
57}
58
59impl Error for InferenceError {}
60
61impl fmt::Display for InferenceError {
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        match self {
64            Self::GeneralError => write!(f, "general error"),
65            Self::NotImplemented => write!(f, "not implemented"),
66            Self::NetworkNotLoaded => write!(f, "network not loaded"),
67            Self::ParameterMismatch => write!(f, "parameter mismatch"),
68            Self::NotFound => write!(f, "not found"),
69            Self::OutOfBounds => write!(f, "out of bounds"),
70            Self::Unexpected => write!(f, "unexpected"),
71            Self::RequestBusy => write!(f, "request busy"),
72            Self::ResultNotReady => write!(f, "result not ready"),
73            Self::NotAllocated => write!(f, "not allocated"),
74            Self::InferNotStarted => write!(f, "infer not started"),
75            Self::NetworkNotRead => write!(f, "network not read"),
76            Self::InferCancelled => write!(f, "infer cancelled"),
77            Self::InvalidCParam => write!(f, "invalid C parameter"),
78            Self::UnknownCError => write!(f, "unknown C error"),
79            Self::NotImplementCMethod => write!(f, "not implemented C method"),
80            Self::UnknownException => write!(f, "unknown exception"),
81            Self::Undefined(code) => write!(f, "undefined error code: {code}"),
82        }
83    }
84}
85
86/// Enumerate the ways that library loading can fail.
87#[allow(missing_docs)]
88#[derive(Debug)]
89pub enum LoadingError {
90    SystemFailure(String),
91    CannotFindLibraryPath,
92}
93
94impl Error for LoadingError {}
95
96impl fmt::Display for LoadingError {
97    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98        match self {
99            Self::SystemFailure(msg) => write!(f, "system failed to load shared libraries: {msg}"),
100            Self::CannotFindLibraryPath => write!(f, "cannot find path to shared libraries"),
101        }
102    }
103}
104
105/// Enumerate setup failures: in some cases, this library will call library-loading code that may
106/// fail in a different way (i.e., [`LoadingError`]) than the calls to the OpenVINO GenAI libraries
107/// (i.e., [`InferenceError`]).
108#[allow(missing_docs)]
109#[derive(Debug)]
110pub enum SetupError {
111    Inference(InferenceError),
112    Loading(LoadingError),
113}
114
115impl Error for SetupError {}
116
117impl fmt::Display for SetupError {
118    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
119        match self {
120            Self::Inference(error) => write!(f, "inference error: {error}"),
121            Self::Loading(error) => write!(f, "library loading error: {error}"),
122        }
123    }
124}
125
126impl From<InferenceError> for SetupError {
127    fn from(error: InferenceError) -> Self {
128        SetupError::Inference(error)
129    }
130}
131
132impl From<LoadingError> for SetupError {
133    fn from(error: LoadingError) -> Self {
134        SetupError::Loading(error)
135    }
136}