Skip to main content

aic_sdk/
error.rs

1use thiserror::Error;
2
3use aic_sdk_sys::AicErrorCode::{self, *};
4
5/// Error type for AIC SDK operations.
6#[derive(Debug, Clone, PartialEq, Eq, Error)]
7pub enum AicError {
8    /// Parameter value is outside the acceptable range. Check documentation for valid values.
9    #[error(
10        "Parameter value is outside the acceptable range. Check documentation for valid values."
11    )]
12    ParameterOutOfRange,
13    /// Handle must be initialized before calling this operation.
14    #[error("Handle must be initialized before calling this operation.")]
15    NotInitialized,
16    /// Audio configuration (sample_rate, block_size) is not supported by the model
17    #[error("Audio configuration (sample_rate, block_size) is not supported by the model")]
18    AudioConfigUnsupported,
19    /// Audio block configuration differs from the one provided during initialization
20    #[error("Audio block configuration differs from the one provided during initialization")]
21    AudioConfigMismatch,
22    /// Processing is not allowed because the SDK key was not authorized or usage reporting failed.
23    #[error(
24        "Processing is not allowed because the SDK key was not authorized or usage reporting failed."
25    )]
26    ProcessingNotAllowed,
27    /// Internal error occurred. Contact support.
28    #[error("Internal error occurred. Contact support.")]
29    Internal,
30    /// License key format is invalid or corrupted. Verify the key was copied correctly.
31    #[error("License key format is invalid or corrupted. Verify the key was copied correctly.")]
32    LicenseFormatInvalid,
33    /// License version is not compatible with the SDK version. Update SDK or contact support.
34    #[error(
35        "License version is not compatible with the SDK version. Update SDK or contact support."
36    )]
37    LicenseVersionUnsupported,
38    /// License key has expired. Renew your license to continue.
39    #[error("License key has expired. Renew your license to continue.")]
40    LicenseExpired,
41    /// Updating the token is only supported when both the original and new keys are JWT-form licenses.
42    #[error(
43        "Updating the token is only supported when both the original and new keys are JWT-form licenses."
44    )]
45    TokenUpdateUnsupported,
46    /// The model file is invalid or corrupted. Verify the file is correct.
47    #[error("The model file is invalid or corrupted. Verify the file is correct.")]
48    ModelInvalid,
49    /// The model file version is not compatible with this SDK version.
50    #[error("The model file version is not compatible with this SDK version.")]
51    ModelVersionUnsupported,
52    /// The model type is not supported by the requested API.
53    #[error("The model type is not supported by the requested API.")]
54    ModelTypeUnsupported,
55    /// The file path is invalid.
56    #[error("The file path is invalid.")]
57    FilePathInvalid,
58    /// The model file cannot be opened due to a filesystem error. Verify that the file exists.
59    #[error(
60        "The model file cannot be opened due to a filesystem error. Verify that the file exists."
61    )]
62    FileSystemError,
63    /// The model data is not aligned to 64 bytes.
64    #[error("The model data is not aligned to 64 bytes.")]
65    ModelDataUnaligned,
66    /// Model download error.
67    #[error("Model download error: {0}")]
68    ModelDownload(String),
69    /// Unknown error code.
70    #[error("Unknown error code: {0}")]
71    Unknown(AicErrorCode::Type),
72}
73
74impl From<AicErrorCode::Type> for AicError {
75    fn from(error_code: AicErrorCode::Type) -> Self {
76        match error_code {
77            AIC_ERROR_CODE_NULL_POINTER => {
78                // This should never happen in our Rust wrapper, but if it does,
79                // it indicates a serious bug in our wrapper logic
80                panic!(
81                    "Unexpected null pointer error from C library - this is a bug in the Rust wrapper"
82                );
83            }
84            AIC_ERROR_CODE_PARAMETER_OUT_OF_RANGE => AicError::ParameterOutOfRange,
85            AIC_ERROR_CODE_NOT_INITIALIZED => AicError::NotInitialized,
86            AIC_ERROR_CODE_AUDIO_CONFIG_UNSUPPORTED => AicError::AudioConfigUnsupported,
87            AIC_ERROR_CODE_AUDIO_CONFIG_MISMATCH => AicError::AudioConfigMismatch,
88            AIC_ERROR_CODE_PROCESSING_NOT_ALLOWED => AicError::ProcessingNotAllowed,
89            AIC_ERROR_CODE_INTERNAL_ERROR => AicError::Internal,
90            AIC_ERROR_CODE_LICENSE_FORMAT_INVALID => AicError::LicenseFormatInvalid,
91            AIC_ERROR_CODE_LICENSE_VERSION_UNSUPPORTED => AicError::LicenseVersionUnsupported,
92            AIC_ERROR_CODE_LICENSE_EXPIRED => AicError::LicenseExpired,
93            AIC_ERROR_CODE_TOKEN_UPDATE_UNSUPPORTED => AicError::TokenUpdateUnsupported,
94            AIC_ERROR_CODE_MODEL_INVALID => AicError::ModelInvalid,
95            AIC_ERROR_CODE_MODEL_VERSION_UNSUPPORTED => AicError::ModelVersionUnsupported,
96            AIC_ERROR_CODE_MODEL_TYPE_UNSUPPORTED => AicError::ModelTypeUnsupported,
97            AIC_ERROR_CODE_FILE_PATH_INVALID => AicError::FilePathInvalid,
98            AIC_ERROR_CODE_FILE_SYSTEM_ERROR => AicError::FileSystemError,
99            AIC_ERROR_CODE_MODEL_DATA_UNALIGNED => AicError::ModelDataUnaligned,
100            code => AicError::Unknown(code),
101        }
102    }
103}
104
105/// Helper function to convert C error codes into Result.
106pub(crate) fn handle_error(error_code: AicErrorCode::Type) -> Result<(), AicError> {
107    match error_code {
108        AIC_ERROR_CODE_SUCCESS => Ok(()),
109        code => Err(AicError::from(code)),
110    }
111}
112
113pub(crate) fn assert_success(error_code: AicErrorCode::Type, message: &str) {
114    assert_eq!(error_code, AIC_ERROR_CODE_SUCCESS, "{}", message);
115}