Skip to content

Commit

Permalink
adds error attribute note
Browse files Browse the repository at this point in the history
  • Loading branch information
damirka committed Oct 21, 2024
1 parent 6ad5232 commit 9ecaf8f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
13 changes: 12 additions & 1 deletion book/src/move-basics/assert-and-abort.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ The code above will, of course, abort with abort code `1`.
The `assert!` macro is a built-in macro that can be used to assert a condition. If the condition is
false, the transaction will abort with the given abort code. The `assert!` macro is a convenient way
to abort a transaction if a condition is not met. The macro shortens the code otherwise written with
an `if` expression + `abort`. The `code` argument is required and has to be a `u64` value.
an `if` expression + `abort`. The `code` argument is optional, but has to be a `u64` value or an
`#[error]` (see below for more information).

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

## Error messages

Move 2024 introduces a special type of error constant, marked with the `#[error]` attribute. This
attribute allows the error constant to be of type `vector<u8>` and can be used to store an error
message.

```move
{{#include ../../../packages/samples/sources/move-basics/assert-and-abort.move:error_attribute}}
```

## Further reading

- [Abort and Assert](/reference/abort-and-assert.html) in the Move Reference.
Expand Down
19 changes: 19 additions & 0 deletions packages/samples/sources/move-basics/assert-and-abort.move
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,22 @@ public fun update_record(/* ... , */ user_has_access: bool, field_exists: bool)
/* ... */
}
// ANCHOR_END: error_const

public struct User { is_authorized: bool, value: u64 }

// ANCHOR: error_attribute
#[error]
const ENotAuthorized: vector<u8> = b"The user is not authorized to perform this action";

#[error]
const EValueTooLow: vector<u8> = b"The value is too low, it should be at least 10";

/// Performs an action on behalf of the user.
public fun update_value(user: &mut User, value: u64) {
assert!(user.is_authorized, ENotAuthorized);
assert!(value >= 10, EValueTooLow);

user.value = value;
}
// ANCHOR_END: error_attribute
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

#[allow(unused_use)]
// ANCHOR: members
module book::more_imports;

use book::module_one::new; // imports the `new` function from the `module_one` module
use book::module_one::Character; // importing the `Character` struct from the `module_one` module

/// Calls the `new` function from the `module_one` module.
public fun create_character(): Character {
new()
}
// ANCHOR_END: members

0 comments on commit 9ecaf8f

Please sign in to comment.