subxt_codegen/
error.rs

1// Copyright 2019-2023 Parity Technologies (UK) Ltd.
2// This file is dual-licensed as Apache-2.0 or GPL-3.0.
3// see LICENSE for license details.
4
5//! Errors that can be emitted from codegen.
6
7use proc_macro2::{Span, TokenStream as TokenStream2};
8use scale_typegen::TypegenError;
9
10/// Error returned when the Codegen cannot generate the runtime API.
11#[derive(Debug, thiserror::Error)]
12#[non_exhaustive]
13pub enum CodegenError {
14    /// Cannot decode the metadata bytes.
15    #[error("Could not decode metadata, only V14 and V15 metadata are supported: {0}")]
16    Decode(#[from] codec::Error),
17    /// Out of line modules are not supported.
18    #[error("Out-of-line subxt modules are not supported, make sure you are providing a body to your module: pub mod polkadot {{ ... }}")]
19    InvalidModule(Span),
20    /// Invalid type path.
21    #[error("Invalid type path {0}: {1}")]
22    InvalidTypePath(String, syn::Error),
23    /// Metadata for constant could not be found.
24    #[error("Metadata for constant entry {0}_{1} could not be found. Make sure you are providing a valid substrate-based metadata")]
25    MissingConstantMetadata(String, String),
26    /// Metadata for storage could not be found.
27    #[error("Metadata for storage entry {0}_{1} could not be found. Make sure you are providing a valid substrate-based metadata")]
28    MissingStorageMetadata(String, String),
29    /// Metadata for call could not be found.
30    #[error("Metadata for call entry {0}_{1} could not be found. Make sure you are providing a valid substrate-based metadata")]
31    MissingCallMetadata(String, String),
32    /// Metadata for call could not be found.
33    #[error("Metadata for runtime API entry {0}_{1} could not be found. Make sure you are providing a valid substrate-based metadata")]
34    MissingRuntimeApiMetadata(String, String),
35    /// Call variant must have all named fields.
36    #[error("Call variant for type {0} must have all named fields. Make sure you are providing a valid substrate-based metadata")]
37    InvalidCallVariant(u32),
38    /// Type should be an variant/enum.
39    #[error("{0} type should be an variant/enum type. Make sure you are providing a valid substrate-based metadata")]
40    InvalidType(String),
41    /// Extrinsic call type could not be found.
42    #[error("Extrinsic call type could not be found. Make sure you are providing a valid substrate-based metadata")]
43    MissingCallType,
44    /// There are too many or too few hashers.
45    #[error("Could not generate functions for storage entry {storage_entry_name}. There are {key_count} keys, but only {hasher_count} hashers. The number of hashers must equal the number of keys or be exactly 1.")]
46    InvalidStorageHasherCount {
47        /// The name of the storage entry
48        storage_entry_name: String,
49        /// Number of keys
50        key_count: usize,
51        /// Number of hashers
52        hasher_count: usize,
53    },
54    /// Cannot generate types.
55    #[error("Type Generation failed: {0}")]
56    TypeGeneration(#[from] TypegenError),
57    /// Error when generating metadata from Wasm-runtime
58    #[error("Failed to generate metadata from wasm file. reason: {0}")]
59    Wasm(String),
60    /// Other error.
61    #[error("Other error: {0}")]
62    Other(String),
63}
64
65impl CodegenError {
66    /// Fetch the location for this error.
67    // Todo: Probably worth storing location outside of the variant,
68    // so that there's a common way to set a location for some error.
69    fn get_location(&self) -> Span {
70        match self {
71            Self::InvalidModule(span) => *span,
72            Self::TypeGeneration(TypegenError::InvalidSubstitute(err)) => err.span,
73            Self::InvalidTypePath(_, err) => err.span(),
74            _ => proc_macro2::Span::call_site(),
75        }
76    }
77    /// Render the error as an invocation of syn::compile_error!.
78    pub fn into_compile_error(self) -> TokenStream2 {
79        let msg = self.to_string();
80        let span = self.get_location();
81        syn::Error::new(span, msg).into_compile_error()
82    }
83}