Skip to content

Commit 62d7847

Browse files
authored
feat: error messages (cairo-book#727)
1 parent d02fb5d commit 62d7847

7 files changed

Lines changed: 44 additions & 51 deletions

File tree

listings/ch04-understanding-ownership/no_listing_04_no_drop_derive_fails/src/lib.cairo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
struct A {}
33

44
fn main() {
5-
A {}; // error: Value not dropped.
5+
A {}; // error: Variable not dropped.
66
}

src/SUMMARY.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@
9999
- [B - Operators and Symbols](appendix-02-operators-and-symbols.md)
100100
- [C - Derivable Traits](appendix-03-derivable-traits.md)
101101
- [D - Common Types & Traits and the Cairo Prelude](appendix-04-common-types-and-traits-and-cairo-prelude.md)
102-
- [E - Useful Development Tools](appendix-05-useful-development-tools.md)
103-
- [F - Installing Cairo binaries](appendix-06-cairo-binaries.md)
102+
- [E - Common Error Messages](appendix-05-common-error-messages.md)
103+
- [F - Useful Development Tools](appendix-06-useful-development-tools.md)
104+
- [G - Installing Cairo binaries](appendix-07-cairo-binaries.md)
104105

105106
---
106107

@@ -157,4 +158,4 @@
157158
## Appendix
158159

159160
- [Appendix](appendix-00.md)
160-
- [A - System Calls](appendix-07-system-calls.md)
161+
- [A - System Calls](appendix-08-system-calls.md)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Appendix E - Common Error Messages
2+
3+
You might encounter error messages when writing Cairo code. Some of them occur very frequently, which is why we will be listing the most common error messages in this appendix to help you resolve common issues.
4+
5+
- `Variable not dropped.`: this error message means that you are trying to make a variable with a type that do not implement the `Drop` trait go out of scope, withtout destroying it. Make sure that variables that need to be dropped at the end of the execution of a function implement the `Drop` trait or the `Destruct` trait. See [Ownership](ch04-01-what-is-ownership.md#destroying-values---example-with-feltdict) section.
6+
7+
- `Variable was previously moved.`: this error message means that you are trying to use a variable whose ownership has already been transferred to another function. When a variable doesn't implement the `Copy` trait, it is passed by value to functions, and ownership of the variable is transferred to the function. Such a variable cannot be used anymore in the current context after its ownership has been transferred. It is often useful to use the `clone` method in this situation.
8+
9+
- `error: Trait has no implementation in context: core::fmt::Display::<package_name::struct_name>`: this error message is encountered if you try to print an instance of a custom data type with `{}` placeholders in a `print!` or `println!` macro. To mitigate this issue, you need to either manually implement the `Display` trait for your type, or use the `Debug` trait by applying `derive(Debug)` to your type, allowing to print your instance by adding `:?` in `{}` placeholders.
10+
11+
- `Got an exception while executing a hint: Hint Error: Failed to deserialize param #x.`: this error means that the execution failed because an entrypoint was called without the expected arguments. Make sure that the arguments you provide when calling an entrypoint are correct. There is a classic issue with `u256` variables, which are actually structs of 2 `u128`. Therefore, when calling a function that takes a `u256` as argument, you need to pass 2 values.
12+
13+
- `Item path::item is not visible in this context.`: this error message lets us know that the path to bring an item into scope is correct, but there is a vibility issue. In cairo, all items are private to parent modules by default. To resolve this issue, make sure that all the modules on the path to items and items themselves are declared with `pub(crate)` or `pub` to have access to them.
14+
15+
- `Identifier not found.`: this error message is a bit aspecific but might indicate that:
16+
- A variable is being used before it has been declared. Make sure to declare variables with the `let` keyword.
17+
- The path to bring an item into scope is wrongly defined. Make sure to use valid paths.
18+
19+
## Starknet Components Related Error Messages
20+
21+
You might encounter some errors when trying to implement components.
22+
Unfortunately, some of them lack meaningful error messages to help debug. This
23+
section aims to provide you with some pointers to help you debug your code.
24+
25+
- `Trait not found. Not a trait.`: this error can occur when you're not importing the component's impl block
26+
correctly in your contract. Make sure to respect the following syntax:
27+
28+
```rust,noplayground
29+
#[abi(embed_v0)]
30+
impl IMPL_NAME = PATH_TO_COMPONENT::EMBEDDED_NAME<ContractState>
31+
```
32+
33+
- `Plugin diagnostic: name is not a substorage member in the contract's Storage. Consider adding to Storage: (...)`: the compiler helps you a lot debugging this by giving you recommendation on the action to take. Basically, you forgot to add the component's storage to your contract's storage. Make sure to add the path to the component's storage annotated with the `#[substorage(v0)]` attribute to your contract's storage.
34+
35+
- `Plugin diagnostic: name is not a nested event in the contract's Event enum. Consider adding to the Event enum:` similar to the previous error, the compiler tells you that you forgot to add the component's events to your contract's events. Make sure to add the path to the component's events to your contract's events.

src/appendix-05-useful-development-tools.md renamed to src/appendix-06-useful-development-tools.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Appendix E - Useful Development Tools
1+
# Appendix F - Useful Development Tools
22

33
In this appendix, we talk about some useful development tools that the Cairo
44
project provides. We’ll look at automatic formatting, quick ways to apply
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Appendix F - Installing the Cairo Binaries
1+
# Appendix G - Installing the Cairo Binaries
22

33
If you want to have access to the Cairo binaries, for anything that you could not achieve by purely using Scarb you can install them by following the instructions below.
44

File renamed without changes.

src/ch16-02-00-composability-and-components.md

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,6 @@ ownable_component::Event`).
174174
Indeed, we don't want to expose externally the functions defined in this
175175
impl. However, we might still want to access them internally.
176176

177-
<!-- TODO: Add content on impl aliases -->
178-
179177
For example, to embed the `Ownable` component defined above, we would do the
180178
following:
181179

@@ -195,7 +193,7 @@ with the components functions externally by calling them using the
195193

196194
The composability of components really shines when combining multiple of them
197195
together. Each adds its features onto the contract. You can rely on
198-
[Openzeppelin's](https://github.com/OpenZeppelin/cairo-contracts) implementation
196+
[Openzeppelin's][OpenZeppelin Cairo Contracts] implementation
199197
of components to quickly plug-in all the common functionalities you need a contract
200198
to have.
201199

@@ -206,46 +204,5 @@ Components can even [depend](./ch16-02-02-component-dependencies.md) on other co
206204
`TContractstate` they're generic on to implement the trait of another component.
207205
Before we dive into this mechanism, let's first look at [how components work under the hood](./ch16-02-01-under-the-hood.md).
208206

209-
## Troubleshooting
210-
211-
You might encounter some errors when trying to implement components.
212-
Unfortunately, some of them lack meaningful error messages to help debug. This
213-
section aims to provide you with some pointers to help you debug your code.
214-
215-
- `Trait not found. Not a trait.`
216-
217-
This error can occur when you're not importing the component's impl block
218-
correctly in your contract. Make sure to respect the following syntax:
219-
220-
```rust
221-
#[abi(embed_v0)]
222-
impl IMPL_NAME = upgradeable::EMBEDDED_NAME<ContractState>
223-
```
224-
225-
Referring to our previous example, this would be:
226-
227-
```rust
228-
#[abi(embed_v0)]
229-
impl OwnableImpl = upgradeable::Ownable<ContractState>
230-
```
231-
232-
- `Plugin diagnostic: name is not a substorage member in the contract's Storage.
233-
Consider adding to Storage: (...)`
234-
235-
The compiler helps you a lot debugging this by giving you recommendation on
236-
the action to take. Basically, you forgot to add the component's storage to
237-
your contract's storage. Make sure to add the path to the component's storage
238-
annotated with the `#[substorage(v0)]` attribute to your contract's storage.
239-
240-
- `Plugin diagnostic: name is not a nested event in the contract's Event enum.
241-
Consider adding to the Event enum:`
242-
243-
Similar to the previous error, the compiler, you forgot to add the component's
244-
events to your contract's events. Make sure to add the path to the component's
245-
events to your contract's events.
246-
247-
- Components functions are not accessible externally
248207

249-
This can happen if you forgot to annotate the component's impl block with
250-
`#[abi(embed_v0)]`. Make sure to add this annotation when embedding the
251-
component's impl in your contract.
208+
[OpenZeppelin Cairo Contracts]: https://github.com/OpenZeppelin/cairo-contracts

0 commit comments

Comments
 (0)