From db5370c5df6aeeb04610d5e32b04ea49ff220b29 Mon Sep 17 00:00:00 2001 From: Tyera Eulberg Date: Mon, 11 Mar 2019 14:25:08 -0600 Subject: [PATCH] Add helper macro to implement bincode serialization of program-specific errors --- sdk/src/native_program.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/sdk/src/native_program.rs b/sdk/src/native_program.rs index 6c9e8e548d..ebebc23a51 100644 --- a/sdk/src/native_program.rs +++ b/sdk/src/native_program.rs @@ -51,6 +51,20 @@ impl std::fmt::Display for ProgramError { } impl std::error::Error for ProgramError {} +// Convenience macro to serialize (and potentially truncate) a program-specific error to pass in +// ProgramError::CustomError +#[macro_export] +macro_rules! custom_error( + ($program_error:expr) => ({ + use bincode::serialize; + let mut error = serialize(&$program_error).expect("failed to serialize program error"); + if error.len() > 32 { + error.truncate(32); + } + error + }); +); + // All native programs export a symbol named process() pub const ENTRYPOINT: &str = "process";