Skip to main content

ohos_web_binding/
error.rs

1#[derive(Debug, Clone)]
2pub enum ArkWebError {
3    WebviewCreateFailed(String),
4    ArkWebApiMemberMissing(String),
5    EvaluateScriptCallbackAlreadyExists,
6    JsApiRegisterFailed(String),
7    NativeError { operation: &'static str, code: u32 },
8}
9
10impl std::fmt::Display for ArkWebError {
11    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12        match self {
13            ArkWebError::WebviewCreateFailed(web_tag) => {
14                write!(f, "Webview create failed: {}", web_tag)
15            }
16            ArkWebError::ArkWebApiMemberMissing(member) => {
17                write!(f, "ArkWeb API member missing: {}", member)
18            }
19            ArkWebError::EvaluateScriptCallbackAlreadyExists => {
20                write!(f, "Evaluate script callback already exists")
21            }
22            ArkWebError::JsApiRegisterFailed(obj_name) => {
23                write!(f, "JS API register failed: {}", obj_name)
24            }
25            ArkWebError::NativeError { operation, code } => {
26                write!(f, "ArkWeb {} failed with error code {}", operation, code)
27            }
28        }
29    }
30}
31
32impl std::error::Error for ArkWebError {}
33
34/// An error registering, initializing, or reading an HTTP request body stream.
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub enum HttpBodyStreamError {
37    NullStream,
38    SetUserDataFailed(i32),
39    SetReadCallbackFailed(i32),
40    NotInitialized,
41    InitializationFailed(i32),
42    InvalidReadSize(usize),
43    ReadFailed(i32),
44    InvalidReadCount { count: i32, capacity: usize },
45    UnexpectedBuffer,
46    NoProgress,
47}
48
49impl std::fmt::Display for HttpBodyStreamError {
50    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51        match self {
52            Self::NullStream => f.write_str("HTTP body stream is null"),
53            Self::SetUserDataFailed(code) => {
54                write!(f, "Failed to register body stream state: {code}")
55            }
56            Self::SetReadCallbackFailed(code) => {
57                write!(f, "Failed to register body read callback: {code}")
58            }
59            Self::NotInitialized => f.write_str("HTTP body stream has not been initialized"),
60            Self::InitializationFailed(code) => write!(f, "Body initialization failed: {code}"),
61            Self::InvalidReadSize(size) => write!(f, "Body read size exceeds i32::MAX: {size}"),
62            Self::ReadFailed(code) => write!(f, "Body read failed: {code}"),
63            Self::InvalidReadCount { count, capacity } => write!(
64                f,
65                "Body read returned {count} bytes for a {capacity}-byte buffer"
66            ),
67            Self::UnexpectedBuffer => f.write_str("Body read returned an unexpected buffer"),
68            Self::NoProgress => f.write_str("Body stream stopped before EOF"),
69        }
70    }
71}
72
73impl std::error::Error for HttpBodyStreamError {}