Skip to content

Commit 19c6397

Browse files
authored
Introduce folders in node-api to scale down files (#596)
* move metadata to its own folder * create event folder * move error to separate folder * fix alloc
1 parent b6a998e commit 19c6397

File tree

10 files changed

+446
-366
lines changed

10 files changed

+446
-366
lines changed

node-api/src/error.rs renamed to node-api/src/error/dispatch_error.rs

Lines changed: 17 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
// This file was taken from subxt (Parity Technologies (UK))
2-
// https://github.com/paritytech/subxt/
3-
// And was adapted by Supercomputing Systems AG and Integritee AG.
4-
//
5-
// Copyright 2019-2022 Parity Technologies (UK) Ltd, Supercomputing Systems AG and Integritee AG.
6-
// This file is licensed as Apache-2.0
7-
// see LICENSE for license details.
1+
/*
2+
Copyright 2021 Integritee AG and Supercomputing Systems AG
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
813

9-
//! General node-api Error and Substrate DispatchError implementation.
10-
11-
use crate::{
12-
alloc::{
13-
borrow::Cow,
14-
format,
15-
string::{String, ToString},
16-
vec::Vec,
17-
},
18-
metadata::Metadata,
14+
use crate::metadata::Metadata;
15+
use alloc::{
16+
borrow::Cow,
17+
string::{String, ToString},
18+
vec::Vec,
1919
};
20-
2120
use codec::{Decode, Encode};
2221
use core::fmt::Debug;
2322
use derive_more::From;
@@ -32,45 +31,6 @@ pub use crate::{
3231
pub use sp_core::crypto::SecretStringError;
3332
pub use sp_runtime::transaction_validity::TransactionValidityError;
3433

35-
/// The underlying error enum, generic over the type held by the `Runtime`
36-
/// variant. Prefer to use the [`Error<E>`] and [`Error`] aliases over
37-
/// using this type directly.
38-
#[derive(Debug, From)]
39-
pub enum Error {
40-
/// Codec error.
41-
Codec(codec::Error),
42-
/// Serde serialization error
43-
Serialization(serde_json::error::Error),
44-
/// Secret string error.
45-
SecretString(SecretStringError),
46-
/// Extrinsic validity error
47-
Invalid(TransactionValidityError),
48-
/// Invalid metadata error
49-
InvalidMetadata(InvalidMetadataError),
50-
/// Invalid metadata error
51-
Metadata(MetadataError),
52-
/// Runtime error.
53-
Runtime(DispatchError),
54-
/// Error decoding to a [`crate::dynamic::Value`].
55-
DecodeValue(DecodeError),
56-
/// Error encoding from a [`crate::dynamic::Value`].
57-
EncodeValue(EncodeError),
58-
/// Transaction progress error.
59-
Transaction(TransactionError),
60-
/// Block related error.
61-
Block(BlockError),
62-
/// An error encoding a storage address.
63-
StorageAddress(StorageAddressError),
64-
/// Other error.
65-
Other(String),
66-
}
67-
68-
impl From<&str> for Error {
69-
fn from(error: &str) -> Self {
70-
Error::Other(error.into())
71-
}
72-
}
73-
7434
/// An error dispatching a transaction. See Substrate DispatchError
7535
//https://github.com/paritytech/substrate/blob/890451221db37176e13cb1a306246f02de80590a/primitives/runtime/src/lib.rs#L524
7636
#[derive(Debug, From)]
@@ -295,32 +255,6 @@ pub enum TransactionalError {
295255
NoLayer,
296256
}
297257

298-
/// Block error
299-
#[derive(Clone, Debug, Eq, PartialEq)]
300-
pub enum BlockError {
301-
/// The block
302-
BlockHashNotFound(String),
303-
}
304-
305-
impl BlockError {
306-
/// Produce an error that a block with the given hash cannot be found.
307-
pub fn block_hash_not_found(hash: impl AsRef<[u8]>) -> BlockError {
308-
let hash = format!("0x{}", hex::encode(hash));
309-
BlockError::BlockHashNotFound(hash)
310-
}
311-
}
312-
313-
/// Transaction error.
314-
#[derive(Clone, Debug, Eq, PartialEq)]
315-
pub enum TransactionError {
316-
/// The finality subscription expired (after ~512 blocks we give up if the
317-
/// block hasn't yet been finalized).
318-
FinalitySubscriptionTimeout,
319-
/// The block hash that the transaction was added to could not be found.
320-
/// This is probably because the block was retracted before being finalized.
321-
BlockHashNotFound,
322-
}
323-
324258
/// Details about a module error that has occurred.
325259
#[derive(Clone, Debug)]
326260
pub struct ModuleError {
@@ -352,26 +286,3 @@ impl ModuleErrorData {
352286
self.error[0]
353287
}
354288
}
355-
356-
/// Something went wrong trying to encode a storage address.
357-
#[derive(Clone, Debug)]
358-
pub enum StorageAddressError {
359-
/// Storage map type must be a composite type.
360-
MapTypeMustBeTuple,
361-
/// Storage lookup does not have the expected number of keys.
362-
WrongNumberOfKeys {
363-
/// The actual number of keys needed, based on the metadata.
364-
actual: usize,
365-
/// The number of keys provided in the storage address.
366-
expected: usize,
367-
},
368-
/// Storage lookup requires a type that wasn't found in the metadata.
369-
TypeNotFound(u32),
370-
/// This storage entry in the metadata does not have the correct number of hashers to fields.
371-
WrongNumberOfHashers {
372-
/// The number of hashers in the metadata for this storage entry.
373-
hashers: usize,
374-
/// The number of fields in the metadata for this storage entry.
375-
fields: usize,
376-
},
377-
}

node-api/src/error/mod.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// This file was taken from subxt (Parity Technologies (UK))
2+
// https://github.com/paritytech/subxt/
3+
// And was adapted by Supercomputing Systems AG and Integritee AG.
4+
//
5+
// Copyright 2019-2022 Parity Technologies (UK) Ltd, Supercomputing Systems AG and Integritee AG.
6+
// This file is licensed as Apache-2.0
7+
// see LICENSE for license details.
8+
9+
//! General node-api Error implementation.
10+
11+
use alloc::{format, string::String};
12+
use core::fmt::Debug;
13+
use derive_more::From;
14+
15+
// Re-expose the errors we use from other crates here:
16+
pub use crate::{
17+
metadata::{InvalidMetadataError, MetadataError},
18+
scale_value::{DecodeError, EncodeError},
19+
};
20+
pub use sp_core::crypto::SecretStringError;
21+
pub use sp_runtime::transaction_validity::TransactionValidityError;
22+
23+
mod dispatch_error;
24+
pub use dispatch_error::*;
25+
26+
/// The underlying error enum, generic over the type held by the `Runtime`
27+
/// variant. Prefer to use the [`Error<E>`] and [`Error`] aliases over
28+
/// using this type directly.
29+
#[derive(Debug, From)]
30+
pub enum Error {
31+
/// Codec error.
32+
Codec(codec::Error),
33+
/// Serde serialization error
34+
Serialization(serde_json::error::Error),
35+
/// Secret string error.
36+
SecretString(SecretStringError),
37+
/// Extrinsic validity error
38+
Invalid(TransactionValidityError),
39+
/// Invalid metadata error
40+
InvalidMetadata(InvalidMetadataError),
41+
/// Invalid metadata error
42+
Metadata(MetadataError),
43+
/// Runtime error.
44+
Runtime(DispatchError),
45+
/// Error decoding to a [`crate::dynamic::Value`].
46+
DecodeValue(DecodeError),
47+
/// Error encoding from a [`crate::dynamic::Value`].
48+
EncodeValue(EncodeError),
49+
/// Transaction progress error.
50+
Transaction(TransactionError),
51+
/// Block related error.
52+
Block(BlockError),
53+
/// An error encoding a storage address.
54+
StorageAddress(StorageAddressError),
55+
/// Other error.
56+
Other(String),
57+
}
58+
59+
impl From<&str> for Error {
60+
fn from(error: &str) -> Self {
61+
Error::Other(error.into())
62+
}
63+
}
64+
65+
/// Block error
66+
#[derive(Clone, Debug, Eq, PartialEq)]
67+
pub enum BlockError {
68+
/// The block
69+
BlockHashNotFound(String),
70+
}
71+
72+
impl BlockError {
73+
/// Produce an error that a block with the given hash cannot be found.
74+
pub fn block_hash_not_found(hash: impl AsRef<[u8]>) -> BlockError {
75+
let hash = format!("0x{}", hex::encode(hash));
76+
BlockError::BlockHashNotFound(hash)
77+
}
78+
}
79+
80+
/// Transaction error.
81+
#[derive(Clone, Debug, Eq, PartialEq)]
82+
pub enum TransactionError {
83+
/// The finality subscription expired (after ~512 blocks we give up if the
84+
/// block hasn't yet been finalized).
85+
FinalitySubscriptionTimeout,
86+
/// The block hash that the transaction was added to could not be found.
87+
/// This is probably because the block was retracted before being finalized.
88+
BlockHashNotFound,
89+
}
90+
91+
/// Something went wrong trying to encode a storage address.
92+
#[derive(Clone, Debug)]
93+
pub enum StorageAddressError {
94+
/// Storage map type must be a composite type.
95+
MapTypeMustBeTuple,
96+
/// Storage lookup does not have the expected number of keys.
97+
WrongNumberOfKeys {
98+
/// The actual number of keys needed, based on the metadata.
99+
actual: usize,
100+
/// The number of keys provided in the storage address.
101+
expected: usize,
102+
},
103+
/// Storage lookup requires a type that wasn't found in the metadata.
104+
TypeNotFound(u32),
105+
/// This storage entry in the metadata does not have the correct number of hashers to fields.
106+
WrongNumberOfHashers {
107+
/// The number of hashers in the metadata for this storage entry.
108+
hashers: usize,
109+
/// The number of fields in the metadata for this storage entry.
110+
fields: usize,
111+
},
112+
}

0 commit comments

Comments
 (0)