Skip to content
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

Merged
merged 9 commits into from
Nov 18, 2024
Merged

Migrate custom error trait impls to thiserror #1856

merged 9 commits into from
Nov 18, 2024

Conversation

pkhry
Copy link
Contributor

@pkhry pkhry commented Nov 6, 2024

Description

Migrates Error trait impls to thiserror as it now supports nostd

@pkhry pkhry requested a review from a team as a code owner November 6, 2024 12:20
core/Cargo.toml Outdated Show resolved Hide resolved
@@ -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)?;
Copy link
Collaborator

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_errs necessary?

Copy link
Contributor Author

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

Copy link
Collaborator

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?

Copy link
Contributor Author

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

Copy link
Collaborator

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 :)

core/src/error.rs Outdated Show resolved Hide resolved
core/src/error.rs Outdated Show resolved Hide resolved
core/src/error.rs Outdated Show resolved Hide resolved
core/src/error.rs Outdated Show resolved Hide resolved
Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
Copy link
Member

@niklasad1 niklasad1 left a 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

@pkhry pkhry requested a review from jsdw November 6, 2024 13:28
Copy link
Collaborator

@jsdw jsdw left a 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;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Comment on lines +44 to +50
// 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)
}
}
Copy link
Collaborator

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?

Copy link
Member

@niklasad1 niklasad1 Nov 15, 2024

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.

Comment on lines +249 to +253
impl From<bip39::Error> for Error {
fn from(err: bip39::Error) -> Self {
Error::Phrase(err)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User #[from] instead?

Copy link
Member

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 {
Copy link
Collaborator

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 {
Copy link
Collaborator

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?

Copy link
Collaborator

@jsdw jsdw left a 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 :)

@jsdw jsdw merged commit 7d10021 into master Nov 18, 2024
13 checks passed
@jsdw jsdw deleted the pkhry/thiserror branch November 18, 2024 09:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants