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 Name Validation error definitions.
60mod name_validation;
61pub use self::name_validation::*;
62
63/// Contains the Utils error definitions.
64mod utils;
65pub use self::utils::*;
66
67/// The LeoError type that contains all sub error types.
68/// This allows a unified error type throughout the Leo crates.
69#[derive(Debug, Error)]
70pub enum LeoError {
71    /// Represents an AST Error in a Leo Error.
72    #[error(transparent)]
73    AstError(#[from] AstError),
74    /// Represents a CLI Error in a Leo Error.
75    #[error(transparent)]
76    CliError(#[from] CliError),
77    /// Represents a Compiler Error in a Leo Error.
78    #[error(transparent)]
79    CompilerError(#[from] CompilerError),
80    #[error(transparent)]
81    InterpreterHalt(#[from] InterpreterHalt),
82    /// Represents a Package Error in a Leo Error.
83    #[error(transparent)]
84    PackageError(#[from] PackageError),
85    /// Represents a Parser Error in a Leo Error.
86    #[error(transparent)]
87    ParserError(#[from] ParserError),
88    /// Represents a Static Analyzer Error in a Leo Error.
89    #[error(transparent)]
90    StaticAnalyzerError(#[from] StaticAnalyzerError),
91    /// Represents a Type Checker Error in a Leo Error.
92    #[error(transparent)]
93    TypeCheckerError(#[from] TypeCheckerError),
94    /// Represents a Name Validation Error in a Leo Error.
95    #[error(transparent)]
96    NameValidationError(#[from] NameValidationError),
97    /// Represents a Loop Unroller Error in a Leo Error.
98    #[error(transparent)]
99    LoopUnrollerError(#[from] LoopUnrollerError),
100    /// Represents a Flatten Error in a Leo Error.
101    #[error(transparent)]
102    FlattenError(#[from] FlattenError),
103    /// Purely for just exiting with the correct status code and
104    /// not re-displaying an error.
105    #[error("")]
106    LastErrorCode(i32),
107    /// Represents a Utils Error in a Leo Error.
108    #[error(transparent)]
109    UtilError(#[from] UtilError),
110    /// Anyhow errors.
111    #[error(transparent)]
112    Anyhow(#[from] anyhow::Error),
113}
114
115impl LeoError {
116    /// Implement error code for each type of Error.
117    pub fn error_code(&self) -> String {
118        use LeoError::*;
119
120        match self {
121            AstError(error) => error.error_code(),
122            CompilerError(error) => error.error_code(),
123            CliError(error) => error.error_code(),
124            ParserError(error) => error.error_code(),
125            PackageError(error) => error.error_code(),
126            StaticAnalyzerError(error) => error.error_code(),
127            TypeCheckerError(error) => error.error_code(),
128            NameValidationError(error) => error.error_code(),
129            LoopUnrollerError(error) => error.error_code(),
130            FlattenError(error) => error.error_code(),
131            UtilError(error) => error.error_code(),
132            LastErrorCode(_) => unreachable!(),
133            Anyhow(_) => "SnarkVM Error".to_string(), // todo: implement error codes for snarkvm errors.
134            InterpreterHalt(_) => "Interpreter Halt".to_string(),
135        }
136    }
137
138    /// Implement exit code for each type of Error.
139    pub fn exit_code(&self) -> i32 {
140        use LeoError::*;
141
142        match self {
143            AstError(error) => error.exit_code(),
144            CompilerError(error) => error.exit_code(),
145            CliError(error) => error.exit_code(),
146            ParserError(error) => error.exit_code(),
147            PackageError(error) => error.exit_code(),
148            StaticAnalyzerError(error) => error.exit_code(),
149            TypeCheckerError(error) => error.exit_code(),
150            NameValidationError(error) => error.exit_code(),
151            LoopUnrollerError(error) => error.exit_code(),
152            FlattenError(error) => error.exit_code(),
153            UtilError(error) => error.exit_code(),
154            LastErrorCode(code) => *code,
155            Anyhow(_) => 11000, // todo: implement exit codes for snarkvm errors.
156            InterpreterHalt(_) => 1,
157        }
158    }
159}
160
161/// The LeoWarning type that contains all sub warning types.
162/// This allows a unified warning type throughout the Leo crates.
163#[derive(Debug, Error, Hash, PartialEq, Eq)]
164pub enum LeoWarning {
165    /// Represents an Parser Warning in a Leo Warning.
166    #[error(transparent)]
167    ParserWarning(#[from] ParserWarning),
168    /// Represents a Static Analyzer Warning in a Leo Warning.
169    #[error(transparent)]
170    StaticAnalyzerWarning(#[from] StaticAnalyzerWarning),
171    /// Represents a Type Checker Warning in a Leo Warning.
172    #[error(transparent)]
173    TypeCheckerWarning(#[from] TypeCheckerWarning),
174}
175
176impl LeoWarning {
177    /// Implement warning code for each type of Warning.
178    pub fn error_code(&self) -> String {
179        use LeoWarning::*;
180
181        match self {
182            ParserWarning(warning) => warning.warning_code(),
183            TypeCheckerWarning(warning) => warning.warning_code(),
184            StaticAnalyzerWarning(warning) => warning.warning_code(),
185        }
186    }
187}
188
189/// A global result type for all Leo crates, that defaults the errors to be a LeoError.
190pub type Result<T, E = LeoError> = core::result::Result<T, E>;