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    #[error(
9        "Parameter value is outside the acceptable range. Check documentation for valid values."
10    )]
11    ParameterOutOfRange,
12    #[error(
13        "Processor must be initialized before calling this operation. Call `Processor::initialize` first."
14    )]
15    ProcessorNotInitialized,
16    #[error(
17        "Audio configuration (samplerate, num_channels, num_frames) is not supported by the model"
18    )]
19    AudioConfigUnsupported,
20    #[error("Audio buffer configuration differs from the one provided during initialization")]
21    AudioConfigMismatch,
22    #[error(
23        "SDK key was not authorized or process failed to report usage. Check if you have internet connection."
24    )]
25    EnhancementNotAllowed,
26    #[error("Internal error occurred. Contact support.")]
27    Internal,
28    #[error("License key format is invalid or corrupted. Verify the key was copied correctly.")]
29    LicenseFormatInvalid,
30    #[error(
31        "License version is not compatible with the SDK version. Update SDK or contact support."
32    )]
33    LicenseVersionUnsupported,
34    #[error("License key has expired. Renew your license to continue.")]
35    LicenseExpired,
36    #[error(
37        "Updating the token is only supported when both the original and new keys are JWT-form licenses."
38    )]
39    TokenUpdateUnsupported,
40    #[error("The model file is invalid or corrupted. Verify the file is correct.")]
41    ModelInvalid,
42    #[error("The model file version is not compatible with this SDK version.")]
43    ModelVersionUnsupported,
44    #[error("The model type is not supported by this operation.")]
45    ModelTypeUnsupported,
46    #[error("The path to the model file is invalid")]
47    ModelFilePathInvalid,
48    #[error(
49        "The model file cannot be opened due to a filesystem error. Verify that the file exists."
50    )]
51    FileSystemError,
52    #[error("The model data is not aligned to 64 bytes.")]
53    ModelDataUnaligned,
54    #[error("Model download error: {0}")]
55    ModelDownload(String),
56    #[error("Unknown error code: {0}")]
57    Unknown(AicErrorCode::Type),
58}
59
60impl From<AicErrorCode::Type> for AicError {
61    fn from(error_code: AicErrorCode::Type) -> Self {
62        match error_code {
63            AIC_ERROR_CODE_NULL_POINTER => {
64                // This should never happen in our Rust wrapper, but if it does,
65                // it indicates a serious bug in our wrapper logic
66                panic!(
67                    "Unexpected null pointer error from C library - this is a bug in the Rust wrapper"
68                );
69            }
70            AIC_ERROR_CODE_PARAMETER_OUT_OF_RANGE => AicError::ParameterOutOfRange,
71            AIC_ERROR_CODE_PROCESSOR_NOT_INITIALIZED => AicError::ProcessorNotInitialized,
72            AIC_ERROR_CODE_AUDIO_CONFIG_UNSUPPORTED => AicError::AudioConfigUnsupported,
73            AIC_ERROR_CODE_AUDIO_CONFIG_MISMATCH => AicError::AudioConfigMismatch,
74            AIC_ERROR_CODE_ENHANCEMENT_NOT_ALLOWED => AicError::EnhancementNotAllowed,
75            AIC_ERROR_CODE_INTERNAL_ERROR => AicError::Internal,
76            AIC_ERROR_CODE_LICENSE_FORMAT_INVALID => AicError::LicenseFormatInvalid,
77            AIC_ERROR_CODE_LICENSE_VERSION_UNSUPPORTED => AicError::LicenseVersionUnsupported,
78            AIC_ERROR_CODE_LICENSE_EXPIRED => AicError::LicenseExpired,
79            AIC_ERROR_CODE_TOKEN_UPDATE_UNSUPPORTED => AicError::TokenUpdateUnsupported,
80            AIC_ERROR_CODE_MODEL_INVALID => AicError::ModelInvalid,
81            AIC_ERROR_CODE_MODEL_VERSION_UNSUPPORTED => AicError::ModelVersionUnsupported,
82            AIC_ERROR_CODE_MODEL_TYPE_UNSUPPORTED => AicError::ModelTypeUnsupported,
83            AIC_ERROR_CODE_MODEL_FILE_PATH_INVALID => AicError::ModelFilePathInvalid,
84            AIC_ERROR_CODE_FILE_SYSTEM_ERROR => AicError::FileSystemError,
85            AIC_ERROR_CODE_MODEL_DATA_UNALIGNED => AicError::ModelDataUnaligned,
86            code => AicError::Unknown(code),
87        }
88    }
89}
90
91/// Helper function to convert C error codes into Result.
92pub(crate) fn handle_error(error_code: AicErrorCode::Type) -> Result<(), AicError> {
93    match error_code {
94        AIC_ERROR_CODE_SUCCESS => Ok(()),
95        code => Err(AicError::from(code)),
96    }
97}
98
99pub(crate) fn assert_success(error_code: AicErrorCode::Type, message: &str) {
100    assert_eq!(error_code, AIC_ERROR_CODE_SUCCESS, "{}", message);
101}