droid_wrap_utils/
error.rs

1/*
2 * Copyright (c) 2025. The RigelA open source project team and
3 * its contributors reserve all rights.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and limitations under the License.
12 */
13
14use jni::errors::{Error as JniError, JniError as JniCallError};
15use std::{
16    error::Error,
17    fmt::{Display, Formatter, Result as FmtResult},
18    result::Result as StdResult,
19    str::Utf8Error,
20};
21
22/// 错误类型
23#[derive(Debug)]
24pub enum DroidWrapError {
25    Jni(JniError),
26    FromStr(String),
27    Utf8(Utf8Error),
28}
29
30impl Clone for DroidWrapError {
31    fn clone(&self) -> Self {
32        match self {
33            Self::Jni(e) => Self::Jni(match e {
34                JniError::WrongJValueType(e, e2) => JniError::WrongJValueType(e, e2),
35                JniError::InvalidCtorReturn => JniError::InvalidCtorReturn,
36                JniError::InvalidArgList(e) => JniError::InvalidArgList(e.to_owned()),
37                JniError::MethodNotFound { name, sig } => JniError::MethodNotFound {
38                    name: name.to_owned(),
39                    sig: sig.to_owned(),
40                },
41                JniError::FieldNotFound { name, sig } => JniError::FieldNotFound {
42                    name: name.to_owned(),
43                    sig: sig.to_owned(),
44                },
45                JniError::JavaException => JniError::JavaException,
46                JniError::JNIEnvMethodNotFound(e) => JniError::JNIEnvMethodNotFound(e),
47                JniError::NullPtr(e) => JniError::NullPtr(e),
48                JniError::NullDeref(e) => JniError::NullDeref(e),
49                JniError::TryLock => JniError::TryLock,
50                JniError::JavaVMMethodNotFound(e) => JniError::JavaVMMethodNotFound(e),
51                JniError::FieldAlreadySet(e) => JniError::FieldAlreadySet(e.to_owned()),
52                JniError::ThrowFailed(e) => JniError::ThrowFailed(e.to_owned()),
53                JniError::ParseFailed(e, e2) => JniError::ParseFailed(e.to_owned(), e2.to_owned()),
54                JniError::JniCall(e) => JniError::JniCall(match e {
55                    JniCallError::Unknown => JniCallError::Unknown,
56                    JniCallError::ThreadDetached => JniCallError::ThreadDetached,
57                    JniCallError::WrongVersion => JniCallError::WrongVersion,
58                    JniCallError::NoMemory => JniCallError::NoMemory,
59                    JniCallError::AlreadyCreated => JniCallError::AlreadyCreated,
60                    JniCallError::InvalidArguments => JniCallError::InvalidArguments,
61                    JniCallError::Other(e) => JniCallError::Other(*e),
62                }),
63            }),
64            Self::Utf8(e) => Self::Utf8(e.to_owned()),
65            Self::FromStr(s) => Self::FromStr(s.to_owned())
66        }
67    }
68}
69
70impl Error for DroidWrapError {}
71
72impl Display for DroidWrapError {
73    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
74        write!(f, "DroidWrapError: ")?;
75        match self {
76            Self::FromStr(e) => Display::fmt(e, f),
77            Self::Jni(e) => Display::fmt(e, f),
78            Self::Utf8(e) => Display::fmt(e, f),
79        }
80    }
81}
82
83impl From<JniError> for DroidWrapError {
84    fn from(value: JniError) -> Self {
85        Self::Jni(value)
86    }
87}
88
89impl From<Utf8Error> for DroidWrapError {
90    fn from(value: Utf8Error) -> Self {
91        Self::Utf8(value)
92    }
93}
94
95/// 结果类型
96pub type Result<T> = StdResult<T, DroidWrapError>;