dmsc/java/
exception.rs

1//! Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
2//!
3//! This file is part of DMSC.
4//! The DMSC project belongs to the Dunimd Team.
5//!
6//! Licensed under the Apache License, Version 2.0 (the "License");
7//! You may not use this file except in compliance with the License.
8//! You may obtain a copy of the License at
9//!
10//!     http://www.apache.org/licenses/LICENSE-2.0
11//!
12//! Unless required by applicable law or agreed to in writing, software
13//! distributed under the License is distributed on an "AS IS" BASIS,
14//! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15//! See the License for the specific language governing permissions and
16//! limitations under the License.
17
18//! # Java Exception Handling
19//!
20//! Provides utilities for throwing and handling Java exceptions from Rust.
21
22use jni::JNIEnv;
23use jni::sys::jlong;
24
25/// DMSC Java exception types
26pub enum DMSCJavaException {
27    /// General DMSC error
28    DMSCErrors,
29    /// Configuration error
30    ConfigError,
31    /// Validation error
32    ValidationError,
33    /// Authentication error
34    AuthError,
35    /// Database error
36    DatabaseError,
37    /// Cache error
38    CacheError,
39    /// Network error
40    NetworkError,
41    /// Null pointer error
42    NullPointerError,
43    /// Illegal argument error
44    IllegalArgumentError,
45}
46
47impl DMSCJavaException {
48    /// Get the Java class name for this exception type
49    pub fn class_name(&self) -> &'static str {
50        match self {
51            DMSCJavaException::DMSCErrors => "com/dunimd/dmsc/DMSCError",
52            DMSCJavaException::ConfigError => "com/dunimd/dmsc/DMSCConfigError",
53            DMSCJavaException::ValidationError => "com/dunimd/dmsc/validation/DMSCValidationError",
54            DMSCJavaException::AuthError => "com/dunimd/dmsc/auth/DMSCAuthError",
55            DMSCJavaException::DatabaseError => "com/dunimd/dmsc/database/DMSCDatabaseError",
56            DMSCJavaException::CacheError => "com/dunimd/dmsc/cache/DMSCCacheError",
57            DMSCJavaException::NetworkError => "com/dunimd/dmsc/DMSCNetworkError",
58            DMSCJavaException::NullPointerError => "java/lang/NullPointerException",
59            DMSCJavaException::IllegalArgumentError => "java/lang/IllegalArgumentException",
60        }
61    }
62}
63
64/// Throw a Java exception from Rust
65pub fn throw_exception(env: &mut JNIEnv, exception_type: DMSCJavaException, message: &str) {
66    let class_name = exception_type.class_name();
67    
68    if let Err(e) = env.throw_new(class_name, message) {
69        eprintln!("Failed to throw Java exception: {:?}", e);
70    }
71}
72
73/// Throw a DMSC error exception
74pub fn throw_dmsc_error(env: &mut JNIEnv, message: &str) {
75    throw_exception(env, DMSCJavaException::DMSCErrors, message);
76}
77
78/// Throw a null pointer exception
79pub fn throw_null_pointer(env: &mut JNIEnv, message: &str) {
80    throw_exception(env, DMSCJavaException::NullPointerError, message);
81}
82
83/// Throw an illegal argument exception
84pub fn throw_illegal_argument(env: &mut JNIEnv, message: &str) {
85    throw_exception(env, DMSCJavaException::IllegalArgumentError, message);
86}
87
88/// Check if a Java object is null and throw exception if so
89pub fn check_not_null(env: &mut JNIEnv, ptr: jlong, context: &str) -> bool {
90    if ptr == 0 {
91        throw_null_pointer(env, &format!("{} pointer is null", context));
92        false
93    } else {
94        true
95    }
96}
97
98/// Macro to check for null pointer and return early if null
99#[macro_export]
100macro_rules! check_java_ptr {
101    ($env:expr, $ptr:expr, $context:expr) => {
102        if !$crate::java::exception::check_not_null($env, $ptr, $context) {
103            return;
104        }
105    };
106}
107
108/// Macro to check for null pointer and return default value if null
109#[macro_export]
110macro_rules! check_java_ptr_or_default {
111    ($env:expr, $ptr:expr, $context:expr, $default:expr) => {
112        if !$crate::java::exception::check_not_null($env, $ptr, $context) {
113            return $default;
114        }
115    };
116}