leo_errors/errors/
mod.rs

1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17/// Contains the ASG error definitions.
18use crate::LeoMessageCode;
19
20/// Contains the AST error definitions.
21mod ast;
22pub use self::ast::*;
23
24/// Contains the CLI error definitions.
25mod cli;
26pub use self::cli::*;
27
28/// Contains the Compiler error definitions.
29mod compiler;
30pub use self::compiler::*;
31
32/// Contains the Flattener error definitions.
33mod flattener;
34pub use self::flattener::*;
35
36/// Contains the Loop Unroller error definitions.
37mod loop_unroller;
38pub use self::loop_unroller::*;
39
40mod interpreter_halt;
41pub use self::interpreter_halt::*;
42
43/// Contains the Package error definitions.
44mod package;
45pub use self::package::*;
46
47/// Contains the Parser error definitions.
48mod parser;
49pub use self::parser::*;
50
51/// Contains the Static Analyzer error definitions.
52mod static_analyzer;
53pub use self::static_analyzer::*;
54
55/// Contains the Type Checker error definitions.
56mod type_checker;
57pub use self::type_checker::*;
58
59/// Contains the Utils error definitions.
60mod utils;
61pub use self::utils::*;
62
63/// The LeoError type that contains all sub error types.
64/// This allows a unified error type throughout the Leo crates.
65#[derive(Debug, Error)]
66pub enum LeoError {
67    /// Represents an AST Error in a Leo Error.
68    #[error(transparent)]
69    AstError(#[from] AstError),
70    /// Represents a CLI Error in a Leo Error.
71    #[error(transparent)]
72    CliError(#[from] CliError),
73    /// Represents a Compiler Error in a Leo Error.
74    #[error(transparent)]
75    CompilerError(#[from] CompilerError),
76    #[error(transparent)]
77    InterpreterHalt(#[from] InterpreterHalt),
78    /// Represents a Package Error in a Leo Error.
79    #[error(transparent)]
80    PackageError(#[from] PackageError),
81    /// Represents a Parser Error in a Leo Error.
82    #[error(transparent)]
83    ParserError(#[from] ParserError),
84    /// Represents a Static Analyzer Error in a Leo Error.
85    #[error(transparent)]
86    StaticAnalyzerError(#[from] StaticAnalyzerError),
87    /// Represents a Type Checker Error in a Leo Error.
88    #[error(transparent)]
89    TypeCheckerError(#[from] TypeCheckerError),
90    /// Represents a Loop Unroller Error in a Leo Error.
91    #[error(transparent)]
92    LoopUnrollerError(#[from] LoopUnrollerError),
93    /// Represents a Flatten Error in a Leo Error.
94    #[error(transparent)]
95    FlattenError(#[from] FlattenError),
96    /// Purely for just exiting with the correct status code and
97    /// not re-displaying an error.
98    #[error("")]
99    LastErrorCode(i32),
100    /// Represents a Utils Error in a Leo Error.
101    #[error(transparent)]
102    UtilError(#[from] UtilError),
103    /// Anyhow errors.
104    #[error(transparent)]
105    Anyhow(#[from] anyhow::Error),
106}
107
108impl LeoError {
109    /// Implement error code for each type of Error.
110    pub fn error_code(&self) -> String {
111        use LeoError::*;
112
113        match self {
114            AstError(error) => error.error_code(),
115            CompilerError(error) => error.error_code(),
116            CliError(error) => error.error_code(),
117            ParserError(error) => error.error_code(),
118            PackageError(error) => error.error_code(),
119            StaticAnalyzerError(error) => error.error_code(),
120            TypeCheckerError(error) => error.error_code(),
121            LoopUnrollerError(error) => error.error_code(),
122            FlattenError(error) => error.error_code(),
123            UtilError(error) => error.error_code(),
124            LastErrorCode(_) => unreachable!(),
125            Anyhow(_) => "SnarkVM Error".to_string(), // todo: implement error codes for snarkvm errors.
126            InterpreterHalt(_) => "Interpreter Halt".to_string(),
127        }
128    }
129
130    /// Implement exit code for each type of Error.
131    pub fn exit_code(&self) -> i32 {
132        use LeoError::*;
133
134        match self {
135            AstError(error) => error.exit_code(),
136            CompilerError(error) => error.exit_code(),
137            CliError(error) => error.exit_code(),
138            ParserError(error) => error.exit_code(),
139            PackageError(error) => error.exit_code(),
140            StaticAnalyzerError(error) => error.exit_code(),
141            TypeCheckerError(error) => error.exit_code(),
142            LoopUnrollerError(error) => error.exit_code(),
143            FlattenError(error) => error.exit_code(),
144            UtilError(error) => error.exit_code(),
145            LastErrorCode(code) => *code,
146            Anyhow(_) => 11000, // todo: implement exit codes for snarkvm errors.
147            InterpreterHalt(_) => 1,
148        }
149    }
150}
151
152/// The LeoWarning type that contains all sub warning types.
153/// This allows a unified warning type throughout the Leo crates.
154#[derive(Debug, Error, Hash, PartialEq, Eq)]
155pub enum LeoWarning {
156    /// Represents an Parser Warning in a Leo Warning.
157    #[error(transparent)]
158    ParserWarning(#[from] ParserWarning),
159    /// Represents a Static Analyzer Warning in a Leo Warning.
160    #[error(transparent)]
161    StaticAnalyzerWarning(#[from] StaticAnalyzerWarning),
162    /// Represents a Type Checker Warning in a Leo Warning.
163    #[error(transparent)]
164    TypeCheckerWarning(#[from] TypeCheckerWarning),
165}
166
167impl LeoWarning {
168    /// Implement warning code for each type of Warning.
169    pub fn error_code(&self) -> String {
170        use LeoWarning::*;
171
172        match self {
173            ParserWarning(warning) => warning.warning_code(),
174            TypeCheckerWarning(warning) => warning.warning_code(),
175            StaticAnalyzerWarning(warning) => warning.warning_code(),
176        }
177    }
178}
179
180/// A global result type for all Leo crates, that defaults the errors to be a LeoError.
181pub type Result<T, E = LeoError> = core::result::Result<T, E>;