Skip to content

Commit 9ecaf8f

Browse files
committed
adds error attribute note
1 parent 6ad5232 commit 9ecaf8f

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

book/src/move-basics/assert-and-abort.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ The code above will, of course, abort with abort code `1`.
4545
The `assert!` macro is a built-in macro that can be used to assert a condition. If the condition is
4646
false, the transaction will abort with the given abort code. The `assert!` macro is a convenient way
4747
to abort a transaction if a condition is not met. The macro shortens the code otherwise written with
48-
an `if` expression + `abort`. The `code` argument is required and has to be a `u64` value.
48+
an `if` expression + `abort`. The `code` argument is optional, but has to be a `u64` value or an
49+
`#[error]` (see below for more information).
4950

5051
```move
5152
{{#include ../../../packages/samples/sources/move-basics/assert-and-abort.move:assert}}
@@ -63,6 +64,16 @@ code and make it easier to understand the abort scenarios.
6364
{{#include ../../../packages/samples/sources/move-basics/assert-and-abort.move:error_const}}
6465
```
6566

67+
## Error messages
68+
69+
Move 2024 introduces a special type of error constant, marked with the `#[error]` attribute. This
70+
attribute allows the error constant to be of type `vector<u8>` and can be used to store an error
71+
message.
72+
73+
```move
74+
{{#include ../../../packages/samples/sources/move-basics/assert-and-abort.move:error_attribute}}
75+
```
76+
6677
## Further reading
6778

6879
- [Abort and Assert](/reference/abort-and-assert.html) in the Move Reference.

packages/samples/sources/move-basics/assert-and-abort.move

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,22 @@ public fun update_record(/* ... , */ user_has_access: bool, field_exists: bool)
5050
/* ... */
5151
}
5252
// ANCHOR_END: error_const
53+
54+
public struct User { is_authorized: bool, value: u64 }
55+
56+
// ANCHOR: error_attribute
57+
#[error]
58+
const ENotAuthorized: vector<u8> = b"The user is not authorized to perform this action";
59+
60+
#[error]
61+
const EValueTooLow: vector<u8> = b"The value is too low, it should be at least 10";
62+
63+
/// Performs an action on behalf of the user.
64+
public fun update_value(user: &mut User, value: u64) {
65+
assert!(user.is_authorized, ENotAuthorized);
66+
assert!(value >= 10, EValueTooLow);
67+
68+
user.value = value;
69+
}
70+
// ANCHOR_END: error_attribute
71+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) Mysten Labs, Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#[allow(unused_use)]
5+
// ANCHOR: members
6+
module book::more_imports;
7+
8+
use book::module_one::new; // imports the `new` function from the `module_one` module
9+
use book::module_one::Character; // importing the `Character` struct from the `module_one` module
10+
11+
/// Calls the `new` function from the `module_one` module.
12+
public fun create_character(): Character {
13+
new()
14+
}
15+
// ANCHOR_END: members

0 commit comments

Comments
 (0)