Skip to main content

leo_errors/errors/utils/
util_errors.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
17use std::{
18    error::Error as ErrorArg,
19    fmt::{Debug, Display},
20};
21
22create_messages!(
23    /// InputError enum that represents all the errors for the `utils` crate.
24    UtilError,
25    code_mask: 10000i32,
26    code_prefix: "UTL",
27
28    @backtraced
29    util_file_io_error {
30        args: (msg: impl Display, err: impl ErrorArg),
31        msg: format!("File system io error: {msg}. Error: {err}"),
32        help: None,
33    }
34
35    @formatted
36    toml_serizalization_error {
37        args: (error: impl ErrorArg),
38        msg: format!("TOML serialization error: {error}"),
39        help: None,
40    }
41
42    @formatted
43    json_serialization_error {
44        args: (error: impl ErrorArg),
45        msg: format!("JSON serialization error: {error}"),
46        help: None,
47    }
48
49    @backtraced
50    snarkvm_parsing_error {
51        args: (name: impl Display),
52        msg: format!("Failed to parse the source file for `{name}.aleo` into a valid Aleo program."),
53        help: None,
54    }
55
56    @backtraced
57    circular_dependency_error {
58        args: (),
59        msg: "Circular dependency detected".to_string(),
60        help: None,
61    }
62
63    @backtraced
64    network_error {
65        args: (url: impl Display, status: impl Display),
66        msg: format!("Failed network request to {url}. Status: {status}"),
67        help: Some("Make sure that you are using the correct `--network` and `--endpoint` options.".to_string()),
68    }
69
70    @formatted
71    duplicate_dependency_name_error {
72        args: (dependency: impl Display),
73        msg: format!("Duplicate dependency found: {dependency}"),
74        help: None,
75    }
76
77    // TODO: Unused, remove.
78    @backtraced
79    reqwest_error {
80        args: (error: impl Display),
81        msg: format!("{}", error),
82        help: None,
83    }
84
85    @backtraced
86    failed_to_open_file {
87        args: (error: impl Display),
88        msg: format!("Failed to open file {error}"),
89        help: None,
90    }
91
92    @backtraced
93    failed_to_read_file {
94        args: (error: impl Display),
95        msg: format!("Failed to read file {error}"),
96        help: None,
97    }
98
99    @backtraced
100    failed_to_deserialize_file {
101        args: (error: impl Display),
102        msg: format!("Failed to deserialize file {error}"),
103        help: None,
104    }
105
106    @formatted
107    failed_to_retrieve_dependencies {
108        args: (error: impl Display),
109        msg: format!("Failed to retrieve dependencies. {error}"),
110        help: None,
111    }
112
113    @formatted
114    missing_network_error {
115        args: (dependency: impl Display),
116        msg: format!("Dependency {dependency} is missing a network specification"),
117        help: Some("Add a network specification to the dependency in the `program.json` file. Example: `network: \"testnet\"`".to_string()),
118    }
119
120    @formatted
121    missing_path_error {
122        args: (dependency: impl Display),
123        msg: format!("Local dependency {dependency} is missing a path specification"),
124        help: Some("Add a path in the `program.json` file to the dependency project root . Example: `path: \"../../board\"`".to_string()),
125    }
126
127    @formatted
128    program_name_mismatch_error {
129        args: (program_json_name: impl Display, dep_name: impl Display, path: impl Display),
130        msg: format!("Name mismatch: Local program at path `{path}` is named `{program_json_name}` in `program.json` but `{dep_name}` in the program that imports it"),
131        help: Some("Change one of the names to match the other".to_string()),
132    }
133
134    @formatted
135    snarkvm_error_building_program_id {
136        args: (),
137        msg: "Snarkvm error building program id".to_string(),
138        help: None,
139    }
140
141    @backtraced
142    failed_to_retrieve_from_endpoint {
143        args: (url: impl Display, error: impl Display),
144        msg: format!("Failed to retrieve from endpoint `{url}`: {error}"),
145        help: None,
146    }
147
148    @formatted
149    build_file_does_not_exist {
150        args: (path: impl Display),
151        msg: format!("Compiled file at `{path}` does not exist, cannot compile parent."),
152        help: Some("If you were using the `--non-recursive` flag, remove it and try again.".to_string()),
153    }
154
155    @backtraced
156    invalid_input_id_len {
157        args: (input: impl Display, expected_type: impl Display),
158        msg: format!("Invalid input: {input}."),
159        help: Some(format!("Type `{expected_type}` must contain exactly 61 lowercase characters or numbers.")),
160    }
161
162    @backtraced
163    invalid_input_id {
164        args: (input: impl Display, expected_type: impl Display, expected_preface: impl Display),
165        msg: format!("Invalid input: {input}."),
166        help: Some(format!("Type `{expected_type}` must start with \"{expected_preface}\".")),
167    }
168
169    @backtraced
170    invalid_numerical_input {
171        args: (input: impl Display),
172        msg: format!("Invalid numerical input: {input}."),
173        help: Some("Input must be a valid u32.".to_string()),
174    }
175
176    @backtraced
177    invalid_range {
178        args: (),
179        msg: "The range must be less than or equal to 50 blocks.".to_string(),
180        help: None,
181    }
182
183    @backtraced
184    invalid_height_or_hash {
185        args: (input: impl Display),
186        msg: format!("Invalid input: {input}."),
187        help: Some("Input must be a valid height or hash. Valid hashes are 61 characters long, composed of only numbers and lower case letters, and be prefaced with \"ab1\".".to_string()),
188    }
189
190    @backtraced
191    invalid_field {
192        args: (field: impl Display),
193        msg: format!("Invalid field: {field}."),
194        help: Some("Field element must be numerical string with optional \"field\" suffix.".to_string()),
195    }
196
197    @backtraced
198    invalid_bound {
199        args: (bound: impl Display),
200        msg: format!("Invalid bound: {bound}."),
201        help: Some("Bound must be a valid u32.".to_string()),
202    }
203
204    @backtraced
205    endpoint_moved_error {
206        args: (endpoint: impl Display),
207        msg: format!("The endpoint `{endpoint}` has been permanently moved."),
208        help: Some("Try using `https://api.explorer.provable.com/v1` in your `.env` file or via the `--endpoint` flag.".to_string()),
209    }
210
211    @backtraced
212    program_size_limit_exceeded {
213        args: (name: impl Display, size: usize, limit: usize),
214        msg: format!("Program `{name}.aleo` exceeds the maximum size limit. Program size: {size} bytes; maximum allowed: {limit} bytes."),
215        help: Some("Reduce the program size by removing unnecessary code, optimizing functions, or splitting the program into smaller programs.".to_string()),
216    }
217);