-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathjava_error.rs
More file actions
78 lines (67 loc) · 1.8 KB
/
java_error.rs
File metadata and controls
78 lines (67 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use crate::objects::object::GlobalJavaObject;
use std::error::Error;
use std::fmt::Debug;
#[derive(Debug)]
pub struct JavaError {
causes: Vec<String>,
stack_frames: Vec<String>,
alt_text: String,
throwable: Option<GlobalJavaObject>,
}
impl JavaError {
pub fn new(causes: Vec<String>, stack_frames: Vec<String>, alt_text: String) -> Self {
Self {
causes,
stack_frames,
alt_text,
throwable: None,
}
}
pub fn new_with_throwable(
causes: Vec<String>,
stack_frames: Vec<String>,
alt_text: String,
throwable: GlobalJavaObject,
) -> Self {
let res = Self {
causes,
stack_frames,
alt_text,
throwable: Some(throwable),
};
#[cfg(feature = "log")]
log::debug!("Exception thrown:\n{res}");
res
}
pub fn get_throwable(&self) -> Option<GlobalJavaObject> {
self.throwable.clone()
}
}
impl std::fmt::Display for JavaError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut causes = self.causes.clone();
let root = causes.pop();
let new_line = if !self.stack_frames.is_empty() {
"\n"
} else {
""
};
let stack_frames = self
.stack_frames
.clone()
.into_iter()
.map(|f| format!(" at {}", f))
.collect::<Vec<_>>()
.join("\n");
if let Some(root) = root {
write!(f, "{}{}{}", root, new_line, stack_frames)
} else {
write!(f, "{}{}{}", self.alt_text, new_line, stack_frames)
}
}
}
impl Error for JavaError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(self)
}
}