Repository: FuelLabs/fuels-rs (Rust)

Issue:
Code duplication for `Errors`
It seems that `fuels-contract/src/errors.rs` and `fuels-core/src/errors.rs` could benefit from some refactoring, because they contain pretty much the same content.


Discussion:

Patch:
diff --git a/fuels-contract/src/contract.rs b/fuels-contract/src/contract.rs
index 7eb7a23544..c922f5b3a1 100644
--- a/fuels-contract/src/contract.rs
+++ b/fuels-contract/src/contract.rs
@@ -1,6 +1,5 @@
 use crate::abi_decoder::ABIDecoder;
 use crate::abi_encoder::ABIEncoder;
-use crate::errors::Error;
 use crate::parameters::{CallParameters, TxParameters};
 use crate::script::Script;
 use anyhow::Result;
@@ -13,6 +12,7 @@ use fuel_types::{Bytes32, Salt, Word};
 use fuel_vm::consts::{REG_CGAS, REG_ONE};
 use fuel_vm::prelude::Contract as FuelContract;
 use fuel_vm::script_with_data_offset;
+use fuels_core::errors::Error;
 use fuels_core::{
     constants::DEFAULT_COIN_AMOUNT, constants::WORD_SIZE, Detokenize, Selector, Token,
 };
diff --git a/fuels-contract/src/errors.rs b/fuels-contract/src/errors.rs
deleted file mode 100644
index 3ae0f8ab23..0000000000
--- a/fuels-contract/src/errors.rs
+++ /dev/null
@@ -1,61 +0,0 @@
-use core::str::Utf8Error;
-use fuels_core::errors::CodecError;
-use fuels_core::InvalidOutputType;
-use fuels_signers::wallet::WalletError;
-use thiserror::Error;
-pub type Result<T> = core::result::Result<T, Error>;
-use std::net;
-
-#[derive(Error, Debug)]
-pub enum Error {
-    #[error("Invalid name: {0}")]
-    InvalidName(String),
-    #[error("Invalid data")]
-    InvalidData,
-    #[error("Missing data: {0}")]
-    MissingData(String),
-    #[error("Serialization error: {0}")]
-    SerdeJson(#[from] serde_json::Error),
-    #[error("Invalid type: {0}")]
-    InvalidType(String),
-    #[error("Parse integer error: {0}")]
-    ParseIntError(#[from] std::num::ParseIntError),
-    #[error("Parse boolean error: {0}")]
-    ParseBoolError(#[from] std::str::ParseBoolError),
-    #[error("Parse hex error: {0}")]
-    ParseHexError(#[from] hex::FromHexError),
-    #[error("Utf8 error: {0}")]
-    Utf8Error(#[from] Utf8Error),
-    #[error("Compilation error: {0}")]
-    CompilationError(String),
-    #[error("Network error: {0}")]
-    NetworkError(#[from] net::AddrParseError),
-    #[error("Transaction error: {0}")]
-    TransactionError(String),
-    #[error("Infrastructure error: {0}")]
-    InfrastructureError(String),
-    #[error("Contract call error: {0}")]
-    ContractCallError(String),
-    #[error("Wallet error: {0}")]
-    WalletError(#[from] WalletError),
-}
-
-impl From<CodecError> for Error {
-    fn from(err: CodecError) -> Error {
-        match err {
-            CodecError::InvalidData => Error::InvalidData,
-            CodecError::Utf8Error(e) => Error::Utf8Error(e),
-        }
-    }
-}
-impl From<std::io::Error> for Error {
-    fn from(err: std::io::Error) -> Error {
-        Error::ContractCallError(err.to_string())
-    }
-}
-
-impl From<InvalidOutputType> for Error {
-    fn from(err: InvalidOutputType) -> Error {
-        Error::ContractCallError(err.0)
-    }
-}
diff --git a/fuels-contract/src/lib.rs b/fuels-contract/src/lib.rs
index a2936edd65..48c82ea1e7 100644
--- a/fuels-contract/src/lib.rs
+++ b/fuels-contract/src/lib.rs
@@ -1,5 +1,4 @@
 pub mod contract;
-pub mod errors;
 pub mod parameters;
 pub mod script;
 
diff --git a/fuels-contract/src/script.rs b/fuels-contract/src/script.rs
index e3e6832fb2..25e8177bd5 100644
--- a/fuels-contract/src/script.rs
+++ b/fuels-contract/src/script.rs
@@ -1,7 +1,7 @@
-use crate::errors::Error;
 use anyhow::Result;
 use fuel_gql_client::client::{types::TransactionStatus, FuelClient};
 use fuel_tx::{Receipt, Transaction};
+use fuels_core::errors::Error;
 
 /// Script is a very thin layer on top of fuel-client with some
 /// extra functionalities needed and provided by the SDK.
diff --git a/fuels-core/src/errors.rs b/fuels-core/src/errors.rs
index f5a829c021..f549eefd76 100644
--- a/fuels-core/src/errors.rs
+++ b/fuels-core/src/errors.rs
@@ -55,6 +55,8 @@ pub enum Error {
     InfrastructureError(String),
     #[error("Contract call error: {0}")]
     ContractCallError(String),
+    #[error("Wallet error: {0}")]
+    WalletError(String),
 }
 
 impl From<CodecError> for Error {
@@ -77,3 +79,8 @@ impl From<InvalidOutputType> for Error {
         Error::ContractCallError(err.0)
     }
 }
+impl From<std::io::Error> for Error {
+    fn from(err: std::io::Error) -> Error {
+        Error::ContractCallError(err.to_string())
+    }
+}
diff --git a/fuels-signers/src/wallet.rs b/fuels-signers/src/wallet.rs
index 1e9ff1d3c7..c874a87061 100644
--- a/fuels-signers/src/wallet.rs
+++ b/fuels-signers/src/wallet.rs
@@ -6,6 +6,7 @@ use fuel_crypto::Hasher;
 use fuel_gql_client::client::schema::coin::Coin;
 use fuel_tx::{Address, AssetId, Bytes64, Input, Output, Receipt, Transaction, UtxoId, Witness};
 use fuel_vm::crypto::secp256k1_sign_compact_recoverable;
+use fuels_core::errors::Error;
 use secp256k1::{PublicKey, Secp256k1, SecretKey};
 use std::{fmt, io};
 use thiserror::Error;
@@ -86,6 +87,12 @@ pub enum WalletError {
     ProviderError(#[from] ProviderError),
 }
 
+impl From<WalletError> for Error {
+    fn from(e: WalletError) -> Self {
+        Error::WalletError(e.to_string())
+    }
+}
+
 impl Wallet {
     pub fn new_from_private_key(
         private_key: SecretKey,


Test patch:
diff --git a/fuels-abigen-macro/tests/harness.rs b/fuels-abigen-macro/tests/harness.rs
index 4223d763cc..843b765401 100644
--- a/fuels-abigen-macro/tests/harness.rs
+++ b/fuels-abigen-macro/tests/harness.rs
@@ -1,9 +1,9 @@
 use fuel_tx::{AssetId, ContractId, Receipt, Salt};
 use fuels_abigen_macro::abigen;
 use fuels_contract::contract::Contract;
-use fuels_contract::errors::Error;
 use fuels_contract::parameters::{CallParameters, TxParameters};
 use fuels_core::constants::{DEFAULT_INITIAL_BALANCE, NATIVE_ASSET_ID};
+use fuels_core::errors::Error;
 use fuels_core::Token;
 use fuels_signers::util::test_helpers::{
     setup_address_and_coins, setup_test_provider, setup_test_provider_and_wallet,