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
// Copyright (C) 2019-2024 Aleo Systems Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

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;

/// The indent for an error message.
pub(crate) const INDENT: &str = "    ";

/// Backtraced compiler output type
///     undefined value `x`
///     --> file.leo: 2:8
///      = help: Initialize a variable `x` first.
#[derive(Derivative)]
#[derivative(Clone, Debug, Default, Hash, PartialEq)]
pub struct Backtraced {
    /// The error message.
    pub message: String,
    /// The error help message if it exists.
    pub help: Option<String>,
    /// The error exit code.
    pub code: i32,
    /// The error leading digits identifier.
    pub code_identifier: i8,
    /// The characters representing the type of error.
    pub type_: String,
    /// Is this Backtrace a warning or error?
    pub error: bool,
    #[derivative(PartialEq = "ignore")]
    #[derivative(Hash = "ignore")]
    /// The backtrace representing where the error occurred in Leo.
    pub backtrace: Backtrace,
}

impl Backtraced {
    /// Creates a backtraced error from a backtrace.
    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 }
    }

    /// Gets the backtraced error exit code.
    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
    }

    /// Gets a unique error identifier.
    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,
        )
    }

    /// Gets a unique warning identifier.
    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,);

        // To avoid the color enabling characters for comparison with test expectations.
        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
    }
}