Skip to content

Commit

Permalink
Added argument checks - for better error display. (#475)
Browse files Browse the repository at this point in the history
* Added argument checks - for better error display.

* Update content/courses/onchain-development/anchor-pdas.md

* Update content/courses/onchain-development/anchor-pdas.md

---------

Co-authored-by: Mike MacCana <[email protected]>
  • Loading branch information
mercy-wumi and mikemaccana authored Oct 7, 2024
1 parent cd05d15 commit 0ebfd1e
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions content/courses/onchain-development/anchor-pdas.md
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ public key, and the movie review's rating, title, and description.

```rust
#[derive(Accounts)]
#[instruction(title:String, description:String)]
#[instruction(title:String)]
pub struct AddMovieReview<'info> {
#[account(
init,
Expand Down Expand Up @@ -639,6 +639,16 @@ pub mod anchor_movie_review_program {
description: String,
rating: u8,
) -> Result<()> {

// We require that the rating is between 1 and 5
require!(rating >= MIN_RATING && rating <= MAX_RATING, MovieReviewError::InvalidRating);

// We require that the title is not longer than 20 characters
require!(title.len() <= MAX_TITLE_LENGTH, MovieReviewError::TitleTooLong);

// We require that the description is not longer than 50 characters
require!(description.len() <= MAX_DESCRIPTION_LENGTH, MovieReviewError::DescriptionTooLong);

msg!("Movie review account space reallocated");
msg!("Title: {}", title);
msg!("Description: {}", description);
Expand Down Expand Up @@ -668,7 +678,7 @@ We'll also still need the `seeds` and `bump` constraints as we had them in

```rust
#[derive(Accounts)]
#[instruction(title:String, description:String)]
#[instruction(title:String)]
pub struct UpdateMovieReview<'info> {
#[account(
mut,
Expand Down

0 comments on commit 0ebfd1e

Please sign in to comment.