-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add generalized contract error with derive macro (#104)
- Loading branch information
Showing
9 changed files
with
101 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "axelar-wasm-std-derive" | ||
version = "0.1.0" | ||
edition = "2021" | ||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
axelar-wasm-std = { workspace = true } | ||
error-stack = { workspace = true } | ||
quote = "1.0.33" | ||
report = { workspace = true } | ||
syn = "2.0.29" | ||
thiserror = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::DeriveInput; | ||
|
||
#[proc_macro_derive(IntoContractError)] | ||
pub fn into_contract_error_derive(input: TokenStream) -> TokenStream { | ||
let ast: DeriveInput = syn::parse(input).unwrap(); | ||
|
||
let name = &ast.ident; | ||
|
||
let gen = quote! { | ||
use axelar_wasm_std::ContractError as _ContractError; | ||
|
||
impl From<#name> for _ContractError { | ||
fn from(error: #name) -> Self { | ||
use report::LoggableError; | ||
use error_stack::report; | ||
|
||
LoggableError::from(&report!(error)).into() | ||
} | ||
} | ||
}; | ||
|
||
gen.into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use axelar_wasm_std::ContractError; | ||
use axelar_wasm_std_derive::IntoContractError; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug, IntoContractError)] | ||
enum TestError { | ||
#[error("error")] | ||
Something, | ||
} | ||
|
||
#[test] | ||
fn can_convert_error() { | ||
_ = ContractError::from(TestError::Something); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use cosmwasm_std::StdError; | ||
use report::LoggableError; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug, PartialEq)] | ||
pub enum ContractError { | ||
#[error(transparent)] | ||
Std(#[from] StdError), | ||
#[error(transparent)] | ||
Structured(#[from] LoggableError), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters