-
Notifications
You must be signed in to change notification settings - Fork 254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate custom error trait impls to thiserror
#1856
Conversation
core/src/events.rs
Outdated
@@ -376,7 +376,8 @@ impl<T: Config> EventDetails<T> { | |||
.map(|f| scale_decode::Field::new(f.ty.id, f.name.as_deref())); | |||
|
|||
let decoded = | |||
scale_value::scale::decode_as_fields(bytes, &mut fields, self.metadata.types())?; | |||
scale_value::scale::decode_as_fields(bytes, &mut fields, self.metadata.types()) | |||
.map_err(Into::<scale_decode::Error>::into)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious what has changed that makes these new .map_err
s necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
decode_as_fields returns DecodeError
and although scale_decode::Error
implements From<DecodeError>
compiler needs an explicit cast
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get why that would be the case though offhand, when it wasn't before? Also, out of interest, can you use Into::into
or do you need the explicit type there too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea either.
Fails to infer the type without explicit annotation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a look and there was a missing From
impl for scale_decode::visitor::DecodeError
(confusingly similar name!); adding that let the Into's go away :)
Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
Would be good if you could double-check whether we should use #[error(transparent)]
from every wrapped error type to avoid duplicate stuff in the display impl.
Such Decode error: Decode error <some message>
but I'm not sure I could be wrong :P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
@@ -5,14 +5,15 @@ | |||
//! A Polkadot-JS account loader. | |||
|
|||
use base64::Engine; | |||
use core::fmt::Display; | |||
use crypto_secretbox::{ | |||
aead::{Aead, KeyInit}, | |||
Key, Nonce, XSalsa20Poly1305, | |||
}; | |||
use serde::Deserialize; | |||
use subxt_core::utils::AccountId32; | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// TODO: when `codec::Error` implements `core::Error` | ||
// remove this impl and replace it by thiserror #[from] | ||
impl From<codec::Error> for Error { | ||
fn from(err: codec::Error) -> Error { | ||
Error::Codec(err) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does not implementing core::Error
stop you from using #[from]
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
error[E0599]: the method `as_dyn_error` exists for reference `&Error`, but its trait bounds were not satisfied
--> core/src/error.rs:17:13
|
17 | Codec(#[from] codec::Error),
| ^^^^ method cannot be called on `&Error` due to unsatisfied trait bounds
|
::: /home/niklasad1/.cargo/registry/src/index.crates.io-6f17d22bba15001f/parity-scale-codec-3.6.12/src/error.rs:26:1
|
26 | pub struct Error {
| ---------------- doesn't satisfy `parity_scale_codec::Error: AsDynError<'_>` or `parity_scale_codec::Error: StdError`
|
= note: the following trait bounds were not satisfied:
`parity_scale_codec::Error: StdError`
which is required by `parity_scale_codec::Error: AsDynError<'_>`
`&parity_scale_codec::Error: StdError`
which is required by `&parity_scale_codec::Error: AsDynError<'_>`
It's probably because codec::Error/no_std
doesn't implement core::error::Error/StdError and thus the from impl bounds isn't satisfied.
impl From<bip39::Error> for Error { | ||
fn from(err: bip39::Error) -> Self { | ||
Error::Phrase(err) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
User #[from]
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as ☝️
Error::Hex(e) => write!(f, "Cannot parse hex string: {e}"), | ||
} | ||
|
||
impl From<hex::FromHexError> for Error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use #[from]
instead?
Error::Hex(e) => write!(f, "Cannot parse hex string: {e}"), | ||
Error::Signature(e) => write!(f, "Signature error: {e}"), | ||
} | ||
impl From<schnorrkel::SignatureError> for Error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use #[from]
instead of the below manual From impls?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just a few places it looks like you can use #[from]
unless I'm not seeing the reason why not :)
Description
Migrates Error trait impls to
thiserror
as it now supports nostd