Skip to main content

lingxia_platform/
error.rs

1use thiserror::Error;
2
3/// Platform-specific error types
4#[derive(Error, Debug)]
5pub enum PlatformError {
6    #[error("Platform error: {0}")]
7    Platform(String),
8
9    #[error("Not supported: {0}")]
10    NotSupported(String),
11
12    #[error("Asset not found: {0}")]
13    AssetNotFound(String),
14
15    #[error("Invalid parameter: {0}")]
16    InvalidParameter(String),
17
18    #[error("Business error: code {0}")]
19    BusinessError(u32),
20
21    #[error("Callback dropped")]
22    CallbackDropped,
23
24    /// The lxapp's native chrome host is not mounted, so nothing painted.
25    /// Not a failure: rust keeps the patch and the first presenter reads it.
26    #[error("Page chrome presenter not mounted")]
27    PresenterUnavailable,
28}
29
30/// Callback wire code for [`PlatformError::PresenterUnavailable`]. Mirrored by
31/// the Android and HarmonyOS SDKs, the only platforms whose chrome update can
32/// run before a presenter exists; the rest always have one or answer inline.
33pub const PRESENTER_UNAVAILABLE_CODE: u32 = 1001;
34
35/// A chrome update that ran before its presenter existed answers with the
36/// shared wire code; recover the variant so callers can tell a deferred paint
37/// from one that genuinely failed.
38#[cfg(any(target_os = "android", target_env = "ohos"))]
39pub(crate) fn unmounted_presenter_or(error: PlatformError) -> PlatformError {
40    match error {
41        PlatformError::BusinessError(PRESENTER_UNAVAILABLE_CODE) => {
42            PlatformError::PresenterUnavailable
43        }
44        other => other,
45    }
46}
47
48/// Result type for platform operations
49pub type PlatformResult<T> = Result<T, PlatformError>;
50
51#[cfg(target_os = "android")]
52impl From<jni::errors::Error> for PlatformError {
53    fn from(value: jni::errors::Error) -> Self {
54        PlatformError::Platform(format!("JNI error: {}", value))
55    }
56}