leo_errors/common/
formatted.rs1use crate::{Backtraced, INDENT};
18
19use leo_span::{Span, with_session_globals};
20
21use backtrace::Backtrace;
22use color_backtrace::{BacktracePrinter, Verbosity};
23use colored::Colorize;
24use std::fmt;
25
26#[derive(Clone, Debug, Default, Hash, PartialEq)]
36pub struct Formatted {
37 pub span: Span,
39 pub backtrace: Backtraced,
41}
42
43impl Formatted {
44 #[allow(clippy::too_many_arguments)]
46 pub fn new_from_span<S>(
47 message: S,
48 help: Option<String>,
49 code: i32,
50 code_identifier: i8,
51 type_: String,
52 error: bool,
53 span: Span,
54 backtrace: Backtrace,
55 ) -> Self
56 where
57 S: ToString,
58 {
59 Self {
60 span,
61 backtrace: Backtraced::new_from_backtrace(
62 message.to_string(),
63 help,
64 code,
65 code_identifier,
66 type_,
67 error,
68 backtrace,
69 ),
70 }
71 }
72
73 pub fn exit_code(&self) -> i32 {
75 self.backtrace.exit_code()
76 }
77
78 pub fn error_code(&self) -> String {
80 self.backtrace.error_code()
81 }
82
83 pub fn warning_code(&self) -> String {
85 self.backtrace.warning_code()
86 }
87}
88
89impl fmt::Display for Formatted {
90 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
91 let Some(source_file) = with_session_globals(|s| s.source_map.find_source_file(self.span.lo)) else {
92 return write!(f, "Can't find source file");
93 };
94
95 let line_contents = source_file.line_contents(self.span);
96
97 let (kind, code) =
98 if self.backtrace.error { ("Error", self.error_code()) } else { ("Warning", self.warning_code()) };
99
100 let message = format!("{kind} [{code}]: {message}", message = self.backtrace.message,);
101
102 if std::env::var("NOCOLOR").unwrap_or_default().trim().to_owned().is_empty() {
104 if self.backtrace.error {
105 writeln!(f, "{}", message.bold().red())?;
106 } else {
107 writeln!(f, "{}", message.bold().yellow())?;
108 }
109 } else {
110 writeln!(f, "{message}")?;
111 };
112
113 writeln!(
114 f,
115 "{indent }--> {path}:{line_start}:{start}",
116 indent = INDENT,
117 path = &source_file.name,
118 line_start = line_contents.line + 1,
120 start = line_contents.start + 1,
123 )?;
124
125 write!(f, "{line_contents}")?;
126
127 if let Some(help) = &self.backtrace.help {
128 writeln!(
129 f,
130 "{INDENT } |\n\
131 {INDENT } = {help}",
132 )?;
133 }
134
135 let leo_backtrace = std::env::var("LEO_BACKTRACE").unwrap_or_default().trim().to_owned();
136 match leo_backtrace.as_ref() {
137 "1" => {
138 let mut printer = BacktracePrinter::default();
139 printer = printer.verbosity(Verbosity::Medium);
140 printer = printer.lib_verbosity(Verbosity::Medium);
141 let trace = printer.format_trace_to_string(&self.backtrace.backtrace).map_err(|_| fmt::Error)?;
142 write!(f, "\n{trace}")?;
143 }
144 "full" => {
145 let mut printer = BacktracePrinter::default();
146 printer = printer.verbosity(Verbosity::Full);
147 printer = printer.lib_verbosity(Verbosity::Full);
148 let trace = printer.format_trace_to_string(&self.backtrace.backtrace).map_err(|_| fmt::Error)?;
149 write!(f, "\n{trace}")?;
150 }
151 _ => {}
152 }
153
154 Ok(())
155 }
156}
157
158impl std::error::Error for Formatted {
159 fn description(&self) -> &str {
160 &self.backtrace.message
161 }
162}