Skip to content

Commit

Permalink
Merge pull request #1294 from CosmWasm/improve-BackendError
Browse files Browse the repository at this point in the history
Improve BackendError
  • Loading branch information
webmaster128 authored May 5, 2022
2 parents 36766bf + 6518742 commit 03d4e21
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 39 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ and this project adheres to
- all: Updated Rust edition to 2021
- cosmwasm-std: Rename `SubMsgExecutionResponse` to `SubMsgResponse`.
- cosmwasm-crypto: Update dependency `k256` to ^0.10.4.
- cosmwasm-vm: `BackendError` was changed to `non_exhaustive` for future
extension; `BackendError` now implements `PartialEq` for easier test code; the
`msg` in `BackendError::Unknown` became non-optional because it was always
set; the argument in `BackendError::unknown`/`::user_err` was change to
`impl Into<String>` to avoid unnecessary clones.

### Deprecated

Expand Down
45 changes: 14 additions & 31 deletions packages/vm/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ pub trait Querier {
/// attached.
pub type BackendResult<T> = (core::result::Result<T, BackendError>, GasInfo);

#[derive(Error, Debug)]
#[derive(Error, Debug, PartialEq)]
#[non_exhaustive]
pub enum BackendError {
#[error("Panic in FFI call")]
ForeignPanic {},
Expand All @@ -169,8 +170,8 @@ pub enum BackendError {
IteratorDoesNotExist { id: u32 },
#[error("Ran out of gas during call into backend")]
OutOfGas {},
#[error("Unknown error during call into backend: {msg:?}")]
Unknown { msg: Option<String> },
#[error("Unknown error during call into backend: {msg}")]
Unknown { msg: String },
// This is the only error case of BackendError that is reported back to the contract.
#[error("User error during call into backend: {msg}")]
UserErr { msg: String },
Expand All @@ -193,21 +194,12 @@ impl BackendError {
BackendError::OutOfGas {}
}

pub fn unknown(msg: impl ToString) -> Self {
BackendError::Unknown {
msg: Some(msg.to_string()),
}
pub fn unknown(msg: impl Into<String>) -> Self {
BackendError::Unknown { msg: msg.into() }
}

/// Use `::unknown(msg: S)` if possible
pub fn unknown_without_message() -> Self {
BackendError::Unknown { msg: None }
}

pub fn user_err(msg: impl ToString) -> Self {
BackendError::UserErr {
msg: msg.to_string(),
}
pub fn user_err(msg: impl Into<String>) -> Self {
BackendError::UserErr { msg: msg.into() }
}
}

Expand Down Expand Up @@ -308,7 +300,7 @@ mod tests {
// constructors

#[test]
fn ffi_error_foreign_panic() {
fn backend_err_foreign_panic() {
let error = BackendError::foreign_panic();
match error {
BackendError::ForeignPanic { .. } => {}
Expand All @@ -317,7 +309,7 @@ mod tests {
}

#[test]
fn ffi_error_bad_argument() {
fn backend_err_bad_argument() {
let error = BackendError::bad_argument();
match error {
BackendError::BadArgument { .. } => {}
Expand All @@ -335,7 +327,7 @@ mod tests {
}

#[test]
fn ffi_error_out_of_gas() {
fn backend_err_out_of_gas() {
let error = BackendError::out_of_gas();
match error {
BackendError::OutOfGas { .. } => {}
Expand All @@ -344,25 +336,16 @@ mod tests {
}

#[test]
fn ffi_error_unknown() {
fn backend_err_unknown() {
let error = BackendError::unknown("broken");
match error {
BackendError::Unknown { msg, .. } => assert_eq!(msg.unwrap(), "broken"),
e => panic!("Unexpected error: {:?}", e),
}
}

#[test]
fn ffi_error_unknown_without_message() {
let error = BackendError::unknown_without_message();
match error {
BackendError::Unknown { msg, .. } => assert!(msg.is_none()),
BackendError::Unknown { msg, .. } => assert_eq!(msg, "broken"),
e => panic!("Unexpected error: {:?}", e),
}
}

#[test]
fn ffi_error_user_err() {
fn backend_err_user_err() {
let error = BackendError::user_err("invalid input");
match error {
BackendError::UserErr { msg, .. } => assert_eq!(msg, "invalid input"),
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/src/errors/vm_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ mod tests {
VmError::BackendErr {
source: BackendError::Unknown { msg },
..
} => assert_eq!(msg.unwrap(), "something went wrong"),
} => assert_eq!(msg, "something went wrong"),
e => panic!("Unexpected error: {:?}", e),
}
}
Expand Down
10 changes: 3 additions & 7 deletions packages/vm/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,9 +896,7 @@ mod tests {
VmError::BackendErr {
source: BackendError::Unknown { msg, .. },
..
} => {
assert_eq!(msg.unwrap(), "Temporarily unavailable");
}
} => assert_eq!(msg, "Temporarily unavailable"),
err => panic!("Incorrect error returned: {:?}", err),
}
}
Expand Down Expand Up @@ -989,9 +987,7 @@ mod tests {
VmError::BackendErr {
source: BackendError::Unknown { msg, .. },
..
} => {
assert_eq!(msg.unwrap(), "Temporarily unavailable");
}
} => assert_eq!(msg, "Temporarily unavailable"),
err => panic!("Incorrect error returned: {:?}", err),
}
}
Expand Down Expand Up @@ -1093,7 +1089,7 @@ mod tests {
VmError::BackendErr {
source: BackendError::Unknown { msg, .. },
..
} => assert_eq!(msg.unwrap(), "Temporarily unavailable"),
} => assert_eq!(msg, "Temporarily unavailable"),
err => panic!("Incorrect error returned: {:?}", err),
};
}
Expand Down

0 comments on commit 03d4e21

Please sign in to comment.