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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 58 additions & 36 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ proc-macro2 = "1.0.86"
quote = "1.0.37"
regex = { version = "1.11.0", default-features = false }
scale-info = { version = "2.11.4", default-features = false }
scale-value = { version = "0.17.0", default-features = false }
scale-bits = { version = "0.6.0", default-features = false }
scale-decode = { version = "0.14.0", default-features = false }
scale-encode = { version = "0.8.0", default-features = false }
scale-typegen = "0.9.0"
scale-typegen-description = "0.9.0"
scale-value = { version = "0.18.0", default-features = false }
scale-bits = { version = "0.7.0", default-features = false }
scale-decode = { version = "0.16.0", default-features = false }
scale-encode = { version = "0.10.0", default-features = false }
scale-typegen = "0.10.0"
scale-typegen-description = "0.10.0"
serde = { version = "1.0.210", default-features = false, features = ["derive"] }
serde_json = { version = "1.0.128", default-features = false }
syn = { version = "2.0.77", features = ["full", "extra-traits"] }
Expand Down
7 changes: 1 addition & 6 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ default = ["std"]
std = [
"codec/std",
"scale-info/std",
"scale-value/std",
"scale-bits/std",
"scale-decode/std",
"scale-encode/std",
"frame-metadata/std",
"subxt-metadata/std",
"hex/std",
Expand Down Expand Up @@ -67,7 +63,6 @@ tracing = { workspace = true, default-features = false }
# AccountId20
keccak-hash = { workspace = true}


[dev-dependencies]
assert_matches = { workspace = true }
bitvec = { workspace = true }
Expand All @@ -85,4 +80,4 @@ rustdoc-args = ["--cfg", "docsrs"]
default-features = true

[lints]
workspace = true
workspace = true
12 changes: 10 additions & 2 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use thiserror::Error as DeriveError;
#[derive(Debug, DeriveError)]
pub enum Error {
/// Codec error.
#[error(transparent)]
Codec(#[from] codec::Error),
#[error("Codec error: {0}")]
Codec(codec::Error),
/// Metadata error.
#[error(transparent)]
Metadata(#[from] MetadataError),
Expand All @@ -41,6 +41,14 @@ impl From<scale_decode::visitor::DecodeError> for Error {
}
}

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


/// Block error
#[derive(Debug, DeriveError)]
pub enum BlockError {
Expand Down
Loading