Skip to content

Commit 0d3a2a0

Browse files
committed
thiserror compatibility
1 parent 34eacf2 commit 0d3a2a0

File tree

5 files changed

+19
-29
lines changed

5 files changed

+19
-29
lines changed

contracts/cyberpunk/src/errors.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,5 @@ use thiserror::Error;
55
pub enum ContractError {
66
#[error("{0}")]
77
/// this is needed so we can use `bucket.load(...)?` and have it auto-converted to the custom error
8-
Std(StdError),
9-
}
10-
11-
impl From<StdError> for ContractError {
12-
fn from(err: StdError) -> Self {
13-
Self::Std(err)
14-
}
8+
Std(#[from] StdError),
159
}

contracts/hackatom/src/errors.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,11 @@ use thiserror::Error;
55
pub enum HackError {
66
#[error("{0}")]
77
/// this is needed so we can use `bucket.load(...)?` and have it auto-converted to the custom error
8-
Std(StdError),
8+
Std(#[from] StdError),
99
// this is whatever we want
1010
#[error("Unauthorized")]
1111
Unauthorized {},
1212
// this is whatever we want
1313
#[error("Downgrade is not supported")]
1414
Downgrade,
1515
}
16-
17-
impl From<StdError> for HackError {
18-
fn from(err: StdError) -> Self {
19-
Self::Std(err)
20-
}
21-
}

contracts/reflect/src/errors.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,10 @@ use thiserror::Error;
55
pub enum ReflectError {
66
#[error("{0}")]
77
// let thiserror implement From<StdError> for you
8-
Std(StdError),
8+
Std(#[from] StdError),
99
// this is whatever we want
1010
#[error("Permission denied: the sender is not the current owner")]
1111
NotCurrentOwner { expected: String, actual: String },
1212
#[error("Messages empty. Must reflect at least one message")]
1313
MessagesEmpty,
1414
}
15-
16-
impl From<StdError> for ReflectError {
17-
fn from(err: StdError) -> Self {
18-
Self::Std(err)
19-
}
20-
}

contracts/virus/src/errors.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@ use thiserror::Error;
55
pub enum ContractError {
66
#[error("{0}")]
77
/// this is needed so we can use `bucket.load(...)?` and have it auto-converted to the custom error
8-
Std(StdError),
8+
Std(#[from] StdError),
99
#[error("{0}")]
1010
Instantiate2Address(#[from] Instantiate2AddressError),
1111
}
12-
13-
impl From<StdError> for ContractError {
14-
fn from(err: StdError) -> Self {
15-
Self::Std(err)
16-
}
17-
}

packages/std/src/errors/std_error.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use alloc::string::ToString;
22
use core::fmt;
3-
use std::{error::Error, str, string};
3+
use std::{error::Error, ops::Deref, str, string};
44

55
use super::BT;
66

@@ -58,6 +58,14 @@ impl AsRef<dyn Error + Send + Sync> for StdError {
5858
}
5959
}
6060

61+
impl Deref for StdError {
62+
type Target = dyn Error + Send + Sync;
63+
64+
fn deref(&self) -> &Self::Target {
65+
&*self.0.inner
66+
}
67+
}
68+
6169
impl StdError {
6270
pub fn msg<D>(msg: D) -> Self
6371
where
@@ -294,6 +302,12 @@ mod tests {
294302
use core::str;
295303
use std::string;
296304

305+
#[derive(Debug, thiserror::Error)]
306+
enum AssertThiserrorWorks {
307+
#[error(transparent)]
308+
Std(#[from] StdError),
309+
}
310+
297311
#[test]
298312
fn implements_debug() {
299313
let error: StdError = StdError::from(OverflowError::new(OverflowOperation::Sub));

0 commit comments

Comments
 (0)