Browse Source

lang/syn: Log errors on exit (#158)

Armani Ferrante 4 years ago
parent
commit
fe2ada85b7
2 changed files with 37 additions and 4 deletions
  1. 33 3
      lang/syn/src/codegen/error.rs
  2. 4 1
      lang/syn/src/codegen/program.rs

+ 33 - 3
lang/syn/src/codegen/error.rs

@@ -2,8 +2,36 @@ use crate::Error;
 use quote::quote;
 use quote::quote;
 
 
 pub fn generate(error: Error) -> proc_macro2::TokenStream {
 pub fn generate(error: Error) -> proc_macro2::TokenStream {
-    let error_enum = error.raw_enum;
+    let error_enum = &error.raw_enum;
     let enum_name = &error.ident;
     let enum_name = &error.ident;
+    // Each arm of the `match` statement for implementing `std::fmt::Display`
+    // on the user defined error code.
+    let variant_dispatch: Vec<proc_macro2::TokenStream> = error
+        .raw_enum
+        .variants
+        .iter()
+        .enumerate()
+        .map(|(idx, variant)| {
+            let ident = &variant.ident;
+            let error_code = &error.codes[idx];
+            let msg = match &error_code.msg {
+                None => {
+                    quote! {
+                        <Self as std::fmt::Debug>::fmt(self, fmt)
+                    }
+                }
+                Some(msg) => {
+                    quote! {
+                        write!(fmt, #msg)
+                    }
+                }
+            };
+            quote! {
+                #enum_name::#ident => #msg
+            }
+        })
+        .collect();
+
     quote! {
     quote! {
         type Result<T> = std::result::Result<T, Error>;
         type Result<T> = std::result::Result<T, Error>;
 
 
@@ -11,7 +39,7 @@ pub fn generate(error: Error) -> proc_macro2::TokenStream {
         pub enum Error {
         pub enum Error {
             #[error(transparent)]
             #[error(transparent)]
             ProgramError(#[from] ProgramError),
             ProgramError(#[from] ProgramError),
-            #[error("{0:?}")]
+            #[error(transparent)]
             ErrorCode(#[from] #enum_name),
             ErrorCode(#[from] #enum_name),
         }
         }
 
 
@@ -21,7 +49,9 @@ pub fn generate(error: Error) -> proc_macro2::TokenStream {
 
 
         impl std::fmt::Display for #enum_name {
         impl std::fmt::Display for #enum_name {
             fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
             fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
-                <Self as std::fmt::Debug>::fmt(self, fmt)
+                match self {
+                    #(#variant_dispatch),*
+                }
             }
             }
         }
         }
 
 

+ 4 - 1
lang/syn/src/codegen/program.rs

@@ -647,7 +647,10 @@ pub fn generate_non_inlined_handlers(program: &Program) -> proc_macro2::TokenStr
                     #program_name::#ix_name(
                     #program_name::#ix_name(
                         Context::new(program_id, &mut accounts, remaining_accounts),
                         Context::new(program_id, &mut accounts, remaining_accounts),
                         #(#ix_arg_names),*
                         #(#ix_arg_names),*
-                    )?;
+                    ).map_err(|e| {
+                        anchor_lang::solana_program::msg!(&e.to_string());
+                        e
+                    })?;
                     accounts.exit(program_id)
                     accounts.exit(program_id)
                 }
                 }
             }
             }