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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use std::fmt;
use backtrace::Backtrace;
use color_backtrace::{BacktracePrinter, Verbosity};
use colored::Colorize;
use derivative::Derivative;
use leo_span::source_map::is_not_test_framework;
pub(crate) const INDENT: &str = " ";
#[derive(Derivative)]
#[derivative(Clone, Debug, Default, Hash, PartialEq)]
pub struct Backtraced {
pub message: String,
pub help: Option<String>,
pub code: i32,
pub code_identifier: i8,
pub type_: String,
pub error: bool,
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
pub backtrace: Backtrace,
}
impl Backtraced {
pub fn new_from_backtrace<S>(
message: S,
help: Option<String>,
code: i32,
code_identifier: i8,
type_: String,
error: bool,
backtrace: Backtrace,
) -> Self
where
S: ToString,
{
Self {
message: message.to_string(),
help,
code,
code_identifier,
type_,
error,
backtrace,
}
}
pub fn exit_code(&self) -> i32 {
let mut code: i32;
if self.code_identifier > 99 {
code = self.code_identifier as i32 * 100_000;
} else if self.code_identifier as i32 > 9 {
code = self.code_identifier as i32 * 10_000;
} else {
code = self.code_identifier as i32 * 1_000;
}
code += self.code;
code
}
pub fn error_code(&self) -> String {
format!(
"E{error_type}{code_identifier:0>3}{exit_code:0>4}",
error_type = self.type_,
code_identifier = self.code_identifier,
exit_code = self.code,
)
}
pub fn warning_code(&self) -> String {
format!(
"W{error_type}{code_identifier:0>3}{exit_code:0>4}",
error_type = self.type_,
code_identifier = self.code_identifier,
exit_code = self.code,
)
}
}
impl fmt::Display for Backtraced {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let (kind, code) = if self.error {
("Error", self.error_code())
} else {
("Warning", self.warning_code())
};
let message = format!("{kind} [{code}]: {message}", message = self.message,);
if is_not_test_framework() {
if self.error {
write!(f, "{}", message.bold().red())?;
} else {
write!(f, "{}", message.bold().yellow())?;
}
} else {
write!(f, "{message}")?;
};
if let Some(help) = &self.help {
write!(
f,
"\n{INDENT } |\n\
{INDENT } = {help}",
)?;
}
let leo_backtrace = std::env::var("LEO_BACKTRACE").unwrap_or_default().trim().to_owned();
match leo_backtrace.as_ref() {
"1" => {
let mut printer = BacktracePrinter::default();
printer = printer.lib_verbosity(Verbosity::Medium);
let trace = printer
.format_trace_to_string(&self.backtrace)
.map_err(|_| fmt::Error)?;
write!(f, "{trace}")?;
}
"full" => {
let mut printer = BacktracePrinter::default();
printer = printer.lib_verbosity(Verbosity::Full);
let trace = printer
.format_trace_to_string(&self.backtrace)
.map_err(|_| fmt::Error)?;
write!(f, "{trace}")?;
}
_ => {}
}
Ok(())
}
}
impl std::error::Error for Backtraced {
fn description(&self) -> &str {
&self.message
}
}