orbbec_sdk/
error.rs

1//! Error module
2use crate::sys::OBError;
3use crate::sys::enums::OBExceptionType;
4
5use std::{error::Error, fmt};
6
7/// Detailed error information
8#[derive(Debug)]
9pub struct OrbbecErrorData {
10    /// Detailed error log
11    pub message: String,
12    /// Function where the error occurred
13    pub function: String,
14    /// Arguments passed to the function
15    pub args: String,
16}
17
18/// Orbbec errors
19#[derive(Debug)]
20pub enum OrbbecError {
21    /// Unknown error, an error not clearly defined by the SDK
22    Unknown(OrbbecErrorData),
23    /// Standard exception, an error caused by the standard library (cpp)
24    StdException(OrbbecErrorData),
25    /// Camera/Device has been disconnected, the camera/device is not available
26    CameraDisconnected(OrbbecErrorData),
27    /// An error in the SDK adaptation platform layer, which means an error in the implementation of a specific system platform
28    PlatformException(OrbbecErrorData),
29    /// Invalid parameter type exception, need to check input parameter
30    InvalidValue(OrbbecErrorData),
31    /// Wrong API call sequence, the API is called in the wrong order or the wrong parameter is passed
32    WrongAPICallSequence(OrbbecErrorData),
33    /// SDK and firmware have not yet implemented this function or feature
34    NotImplemented(OrbbecErrorData),
35    /// SDK access I/O exception error
36    IOException(OrbbecErrorData),
37    /// SDK access and use memory errors. For example, the frame fails to allocate memory
38    MemoryException(OrbbecErrorData),
39    /// Unsupported operation type error by SDK or device
40    UnsupportedOperation(OrbbecErrorData),
41}
42
43impl fmt::Display for OrbbecError {
44    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45        match self {
46            OrbbecError::Unknown(data) => write!(f, "Unknown Error: {}", data.message),
47            OrbbecError::StdException(data) => write!(f, "Standard Exception: {}", data.message),
48            OrbbecError::CameraDisconnected(data) => {
49                write!(f, "Camera Disconnected: {}", data.message)
50            }
51            OrbbecError::PlatformException(data) => {
52                write!(f, "Platform Exception: {}", data.message)
53            }
54            OrbbecError::InvalidValue(data) => write!(f, "Invalid Value: {}", data.message),
55            OrbbecError::WrongAPICallSequence(data) => {
56                write!(f, "Wrong API Call Sequence: {}", data.message)
57            }
58            OrbbecError::NotImplemented(data) => write!(f, "Not Implemented: {}", data.message),
59            OrbbecError::IOException(data) => write!(f, "I/O Exception: {}", data.message),
60            OrbbecError::MemoryException(data) => write!(f, "Memory Exception: {}", data.message),
61            OrbbecError::UnsupportedOperation(data) => {
62                write!(f, "Unsupported Operation: {}", data.message)
63            }
64        }
65    }
66}
67
68impl Error for OrbbecError {}
69
70impl From<&OBError> for OrbbecError {
71    fn from(err: &OBError) -> Self {
72        let message = err.message();
73        let function = err.function();
74        let args = err.args();
75        let exception_type = err.exception_type();
76
77        let data = OrbbecErrorData {
78            message,
79            function,
80            args,
81        };
82
83        match exception_type {
84            OBExceptionType::Unknown => OrbbecError::Unknown(data),
85            OBExceptionType::StdException => OrbbecError::StdException(data),
86            OBExceptionType::CameraDisconnected => OrbbecError::CameraDisconnected(data),
87            OBExceptionType::PlatformException => OrbbecError::PlatformException(data),
88            OBExceptionType::InvalidValue => OrbbecError::InvalidValue(data),
89            OBExceptionType::WrongAPICallSequence => OrbbecError::WrongAPICallSequence(data),
90            OBExceptionType::NotImplemented => OrbbecError::NotImplemented(data),
91            OBExceptionType::IOException => OrbbecError::IOException(data),
92            OBExceptionType::MemoryException => OrbbecError::MemoryException(data),
93            OBExceptionType::UnsupportedOperation => OrbbecError::UnsupportedOperation(data),
94        }
95    }
96}
97
98impl From<OBError> for OrbbecError {
99    fn from(err: OBError) -> Self {
100        OrbbecError::from(&err)
101    }
102}