Skip to main content

leo_errors/errors/
mod.rs

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