Browse Source

lang: adjust error code so `#[error_code]` works with just importing `anchor_lang::error_code` (#1610)

Paul 3 years ago
parent
commit
170a763625
4 changed files with 8 additions and 11 deletions
  1. 1 0
      CHANGELOG.md
  2. 1 1
      lang/src/error.rs
  3. 5 9
      lang/src/lib.rs
  4. 1 1
      lang/syn/src/codegen/error.rs

+ 1 - 0
CHANGELOG.md

@@ -29,6 +29,7 @@ incremented for features.
 * spl: Update `spl/governance` to use new errors ([#1582](https://github.com/project-serum/anchor/pull/1582)).
 * spl: Update `spl/governance` to use new errors ([#1582](https://github.com/project-serum/anchor/pull/1582)).
 * client: Fix `Cluster`'s `FromStr` implementation ([#1362](https://github.com/project-serum/anchor/pull/1362)).
 * client: Fix `Cluster`'s `FromStr` implementation ([#1362](https://github.com/project-serum/anchor/pull/1362)).
 * lang: implement `Key` for `Pubkey` again, so `associated_token::*` constraints can use pubkey targets again ([#1601](https://github.com/project-serum/anchor/pull/1601)).
 * lang: implement `Key` for `Pubkey` again, so `associated_token::*` constraints can use pubkey targets again ([#1601](https://github.com/project-serum/anchor/pull/1601)).
+* lang: adjust error code so `#[error_code]` works with just importing `anchor_lang::error_code` ([#1610](https://github.com/project-serum/anchor/pull/1610)).
 * cli/ts: generated `IDL` variable in `types/<project-name.ts>` should equal the `<project-name>.json` idl file ([#1609](https://github.com/project-serum/anchor/pull/1609)) 
 * cli/ts: generated `IDL` variable in `types/<project-name.ts>` should equal the `<project-name>.json` idl file ([#1609](https://github.com/project-serum/anchor/pull/1609)) 
 
 
 ### Breaking
 ### Breaking

+ 1 - 1
lang/src/error.rs

@@ -1,4 +1,4 @@
-use anchor_attribute_error::error_code;
+use anchor_lang::error_code;
 use borsh::maybestd::io::Error as BorshIoError;
 use borsh::maybestd::io::Error as BorshIoError;
 use solana_program::{program_error::ProgramError, pubkey::Pubkey};
 use solana_program::{program_error::ProgramError, pubkey::Pubkey};
 use std::fmt::{Debug, Display};
 use std::fmt::{Debug, Display};

+ 5 - 9
lang/src/lib.rs

@@ -48,7 +48,7 @@ pub use crate::bpf_upgradeable_state::*;
 pub use anchor_attribute_access_control::access_control;
 pub use anchor_attribute_access_control::access_control;
 pub use anchor_attribute_account::{account, declare_id, zero_copy};
 pub use anchor_attribute_account::{account, declare_id, zero_copy};
 pub use anchor_attribute_constant::constant;
 pub use anchor_attribute_constant::constant;
-pub use anchor_attribute_error;
+pub use anchor_attribute_error::*;
 pub use anchor_attribute_event::{emit, event};
 pub use anchor_attribute_event::{emit, event};
 pub use anchor_attribute_interface::interface;
 pub use anchor_attribute_interface::interface;
 pub use anchor_attribute_program::program;
 pub use anchor_attribute_program::program;
@@ -355,14 +355,12 @@ pub mod __private {
 macro_rules! require {
 macro_rules! require {
     ($invariant:expr, $error:tt $(,)?) => {
     ($invariant:expr, $error:tt $(,)?) => {
         if !($invariant) {
         if !($invariant) {
-            return Err(anchor_lang::anchor_attribute_error::error!(
-                crate::ErrorCode::$error
-            ));
+            return Err(anchor_lang::error!(crate::ErrorCode::$error));
         }
         }
     };
     };
     ($invariant:expr, $error:expr $(,)?) => {
     ($invariant:expr, $error:expr $(,)?) => {
         if !($invariant) {
         if !($invariant) {
-            return Err(anchor_lang::anchor_attribute_error::error!($error));
+            return Err(anchor_lang::error!($error));
         }
         }
     };
     };
 }
 }
@@ -566,12 +564,10 @@ macro_rules! require_gte {
 #[macro_export]
 #[macro_export]
 macro_rules! err {
 macro_rules! err {
     ($error:tt $(,)?) => {
     ($error:tt $(,)?) => {
-        Err(anchor_lang::anchor_attribute_error::error!(
-            crate::ErrorCode::$error
-        ))
+        Err(anchor_lang::error!(crate::ErrorCode::$error))
     };
     };
     ($error:expr $(,)?) => {
     ($error:expr $(,)?) => {
-        Err(anchor_lang::anchor_attribute_error::error!($error))
+        Err(anchor_lang::error!($error))
     };
     };
 }
 }
 
 

+ 1 - 1
lang/syn/src/codegen/error.rs

@@ -76,7 +76,7 @@ pub fn generate(error: Error) -> proc_macro2::TokenStream {
         }
         }
 
 
         impl From<#enum_name> for anchor_lang::error::Error {
         impl From<#enum_name> for anchor_lang::error::Error {
-            fn from(error_code: #enum_name) -> Error {
+            fn from(error_code: #enum_name) -> anchor_lang::error::Error {
                 anchor_lang::error::Error::from(
                 anchor_lang::error::Error::from(
                     anchor_lang::error::AnchorError {
                     anchor_lang::error::AnchorError {
                         error_name: error_code.name(),
                         error_name: error_code.name(),