Search code examples
blockchainsmartcontractssolanaanchor-solana

error is imported here, but it is a function-like macro


I have created a macro to handle all the errors, which will then be implemented individually in the code. But I am getting some errors.

#[error]
pub enum ErrorCode {
    #[msg("SOMETHING 1")]
    Unauthorized,
}

and implemented them like for example :-

if a != b {
  return Err(ErrorCode::Unauthorized.into());
}

Now the errors are

error: cannot find attribute `error` in this scope
   --> programs/xxx/src/lib.rs:287:3
    |
287 | #[error]
    |   ^^^^^
    |
note: `error` is imported here, but it is a function-like macro
   --> programs/xxx/src/lib.rs:1:5
    |
1   | use anchor_lang::prelude::*;
    |     ^^^^^^^^^^^^^^^^^^^^^^^

error: cannot find attribute `msg` in this scope
   --> programs/xxx/src/lib.rs:289:7
    |
289 |     #[msg("You are not authorized to complete this transaction")]
    |       ^^^
    |
note: `msg` is imported here, but it is a function-like macro
   --> programs/xxx/src/lib.rs:1:5
    |
1   | use anchor_lang::prelude::*;
    |     ^^^^^^^^^^^^^^^^^^^^^^^
error[E0599]: no variant or associated item named `Unauthorized` found for enum `anchor_lang::error::ErrorCode` in the current scope
   --> programs/xxx/src/lib.rs:132:39
    |
132 |                 return Err(ErrorCode::Unauthorized.Into());
    |                                       ^^^^^^^^^^^^ variant or associated item not found in `anchor_lang::error::ErrorCode`

Solution

  • You need use [error_code] macro instead of [error] I think this a change that was made during the updates .