Skip to content

Commit 86e3cc7

Browse files
authored
refactor: Error type (#1270)
closes: #1236 All the suggestions from the issue were incorporated. In addition, error messages were changed to follow the guidelines from [rust-api-guidelines](https://rust-lang.github.io/api-guidelines/interoperability.html?highlight=lowercase#error-types-are-meaningful-and-well-behaved-c-good-err) i.e. `The error message given by the Display representation of an error type should be lowercase without trailing punctuation, and typically concise.` BREAKING CHANGE: the `fuels-rs` `Error` type is changed together with the error messages.
1 parent 9d8e675 commit 86e3cc7

File tree

87 files changed

+1003
-1052
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+1003
-1052
lines changed

docs/src/calling-contracts/call-params.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Then, in Rust, after setting up and deploying the above contract, you can config
2828
<!-- payable:example:start -->
2929
`call_params` returns a result to ensure you don't forward assets to a contract method that isn't payable.
3030
<!-- payable:example:end -->
31-
In the following example, we try to forward an amount of 100 of the base asset to `non_payable`. As its name suggests, `non_payable` isn't annotated with `#[payable]` in the contract code. Passing `CallParameters` with an amount other than 0 leads to an `InvalidCallParameters` error:
31+
In the following example, we try to forward an amount of `100` of the base asset to `non_payable`. As its name suggests, `non_payable` isn't annotated with `#[payable]` in the contract code. Passing `CallParameters` with an amount other than `0` leads to an error:
3232

3333
```rust,ignore
3434
{{#include ../../../packages/fuels/tests/contracts.rs:non_payable_params}}

docs/src/calling-contracts/logs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ You can use the `decode_logs()` function to retrieve a `LogResult` struct contai
2222

2323
Due to possible performance hits, it is not recommended to use `decode_logs()` outside of a debugging scenario.
2424

25-
> **Note:** String slices can not be logged directly. Use the `__to_str_array()` function to convert it to a `str[N]` first.
25+
> **Note:** String slices cannot be logged directly. Use the `__to_str_array()` function to convert it to a `str[N]` first.

examples/contracts/src/lib.rs

Lines changed: 31 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#[cfg(test)]
22
mod tests {
3-
use fuels::core::codec::EncoderConfig;
43
use fuels::{
5-
core::codec::DecoderConfig,
4+
core::codec::{DecoderConfig, EncoderConfig},
65
prelude::{Config, LoadConfiguration, StorageConfiguration},
7-
types::{errors::Result, Bits256},
6+
types::{
7+
errors::{transaction::Reason, Result},
8+
Bits256,
9+
},
810
};
911

1012
#[tokio::test]
@@ -403,7 +405,7 @@ mod tests {
403405

404406
assert!(matches!(
405407
response,
406-
Err(Error::RevertTransactionError { .. })
408+
Err(Error::Transaction(Reason::Reverted { .. }))
407409
));
408410
// ANCHOR_END: dependency_estimation_fail
409411

@@ -438,80 +440,32 @@ mod tests {
438440
#[tokio::test]
439441
#[allow(unused_variables)]
440442
async fn get_contract_outputs() -> Result<()> {
441-
use fuels::{prelude::*, tx::Receipt};
442-
{
443-
abigen!(Contract(
444-
name = "TestContract",
445-
abi =
446-
"packages/fuels/tests/contracts/contract_test/out/debug/contract_test-abi.json"
447-
));
448-
let wallet = launch_provider_and_get_wallet().await?;
449-
450-
let contract_id = Contract::load_from(
451-
"../../packages/fuels/tests/contracts/contract_test/out/debug/contract_test.bin",
452-
LoadConfiguration::default(),
453-
)?
454-
.deploy(&wallet, TxPolicies::default())
455-
.await?;
443+
use fuels::prelude::*;
456444

457-
let contract_methods = TestContract::new(contract_id, wallet).methods();
458-
459-
let response = contract_methods.increment_counter(162).call().await?;
460-
let response = contract_methods.increment_counter(162).call().await;
461-
match response {
462-
// The transaction is valid and executes to completion
463-
Ok(call_response) => {
464-
let receipts: Vec<Receipt> = call_response.receipts;
465-
// Do things with logs and receipts
466-
}
467-
// The transaction is malformed
468-
Err(Error::ValidationError(e)) => {
469-
println!("Transaction is malformed (ValidationError): {e}");
470-
}
471-
// Failed request to provider
472-
Err(Error::ProviderError(reason)) => {
473-
println!("Provider request failed with reason: {reason}");
474-
}
475-
// The transaction is valid but reverts
476-
Err(Error::RevertTransactionError {
477-
reason, receipts, ..
478-
}) => {
479-
println!("ContractCall failed with reason: {reason}");
480-
println!("Transaction receipts are: {receipts:?}");
481-
}
482-
Err(_) => {}
483-
}
484-
}
485-
{
486-
// ANCHOR: deployed_contracts
487-
abigen!(Contract(
488-
name = "MyContract",
489-
// Replace with your contract ABI.json path
490-
abi =
491-
"packages/fuels/tests/contracts/contract_test/out/debug/contract_test-abi.json"
492-
));
493-
let wallet_original = launch_provider_and_get_wallet().await?;
494-
495-
let wallet = wallet_original.clone();
496-
// Your bech32m encoded contract ID.
497-
let contract_id: Bech32ContractId =
498-
"fuel1vkm285ypjesypw7vhdlhnty3kjxxx4efckdycqh3ttna4xvmxtfs6murwy"
499-
.parse()
500-
.expect("Invalid ID");
501-
502-
let connected_contract_instance = MyContract::new(contract_id, wallet);
503-
// You can now use the `connected_contract_instance` just as you did above!
504-
// ANCHOR_END: deployed_contracts
505-
506-
let wallet = wallet_original;
507-
// ANCHOR: deployed_contracts_hex
508-
let contract_id: ContractId =
509-
"0x65b6a3d081966040bbccbb7f79ac91b48c635729c59a4c02f15ae7da999b32d3"
510-
.parse()
511-
.expect("Invalid ID");
512-
let connected_contract_instance = MyContract::new(contract_id, wallet);
513-
// ANCHOR_END: deployed_contracts_hex
514-
}
445+
// ANCHOR: deployed_contracts
446+
abigen!(Contract(
447+
name = "MyContract",
448+
// Replace with your contract ABI.json path
449+
abi = "packages/fuels/tests/contracts/contract_test/out/debug/contract_test-abi.json"
450+
));
451+
let wallet_original = launch_provider_and_get_wallet().await?;
452+
453+
let wallet = wallet_original.clone();
454+
// Your bech32m encoded contract ID.
455+
let contract_id: Bech32ContractId =
456+
"fuel1vkm285ypjesypw7vhdlhnty3kjxxx4efckdycqh3ttna4xvmxtfs6murwy".parse()?;
457+
458+
let connected_contract_instance = MyContract::new(contract_id, wallet);
459+
// You can now use the `connected_contract_instance` just as you did above!
460+
// ANCHOR_END: deployed_contracts
461+
462+
let wallet = wallet_original;
463+
// ANCHOR: deployed_contracts_hex
464+
let contract_id: ContractId =
465+
"0x65b6a3d081966040bbccbb7f79ac91b48c635729c59a4c02f15ae7da999b32d3".parse()?;
466+
467+
let connected_contract_instance = MyContract::new(contract_id, wallet);
468+
// ANCHOR_END: deployed_contracts_hex
515469

516470
Ok(())
517471
}

examples/cookbook/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ mod tests {
3535

3636
// ANCHOR: liquidity_wallet
3737
let base_asset_id: AssetId =
38-
"0x9ae5b658754e096e4d681c548daf46354495a437cc61492599e33fc64dcdc30c"
39-
.parse()
40-
.unwrap();
38+
"0x9ae5b658754e096e4d681c548daf46354495a437cc61492599e33fc64dcdc30c".parse()?;
4139

4240
let asset_ids = [AssetId::default(), base_asset_id];
4341
let asset_configs = asset_ids
@@ -171,7 +169,7 @@ mod tests {
171169
let mut inputs = vec![];
172170
let mut outputs = vec![];
173171
for (id_string, amount) in balances {
174-
let id = AssetId::from_str(&id_string).unwrap();
172+
let id = AssetId::from_str(&id_string)?;
175173

176174
// leave the base asset to cover transaction fees
177175
if id == BASE_ASSET_ID {

examples/predicates/src/lib.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,13 @@ mod tests {
1111
async fn predicate_example() -> Result<()> {
1212
// ANCHOR: predicate_wallets
1313
let secret_key1: SecretKey =
14-
"0x862512a2363db2b3a375c0d4bbbd27172180d89f23f2e259bac850ab02619301"
15-
.parse()
16-
.unwrap();
14+
"0x862512a2363db2b3a375c0d4bbbd27172180d89f23f2e259bac850ab02619301".parse()?;
1715

1816
let secret_key2: SecretKey =
19-
"0x37fa81c84ccd547c30c176b118d5cb892bdb113e8e80141f266519422ef9eefd"
20-
.parse()
21-
.unwrap();
17+
"0x37fa81c84ccd547c30c176b118d5cb892bdb113e8e80141f266519422ef9eefd".parse()?;
2218

2319
let secret_key3: SecretKey =
24-
"0x976e5c3fa620092c718d852ca703b6da9e3075b9f2ecb8ed42d9f746bf26aafb"
25-
.parse()
26-
.unwrap();
20+
"0x976e5c3fa620092c718d852ca703b6da9e3075b9f2ecb8ed42d9f746bf26aafb".parse()?;
2721

2822
let mut wallet = WalletUnlocked::new_from_private_key(secret_key1, None);
2923
let mut wallet2 = WalletUnlocked::new_from_private_key(secret_key2, None);

examples/providers/src/lib.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ mod tests {
1818
let provider = Provider::connect("beta-4.fuel.network").await.unwrap();
1919

2020
// Setup a private key
21-
let secret =
22-
SecretKey::from_str("a1447cd75accc6b71a976fd3401a1f6ce318d27ba660b0315ee6ac347bf39568")
23-
.unwrap();
21+
let secret = SecretKey::from_str(
22+
"a1447cd75accc6b71a976fd3401a1f6ce318d27ba660b0315ee6ac347bf39568",
23+
)?;
2424

2525
// Create the wallet
2626
let wallet = WalletUnlocked::new_from_private_key(secret, Some(provider));
@@ -33,10 +33,9 @@ mod tests {
3333
let port = provider.url().split(':').last().unwrap();
3434

3535
// ANCHOR: local_node_address
36-
let _provider = Provider::connect(format!("127.0.0.1:{port}"))
37-
.await
38-
.unwrap();
36+
let _provider = Provider::connect(format!("127.0.0.1:{port}")).await?;
3937
// ANCHOR_END: local_node_address
38+
4039
Ok(())
4140
}
4241

examples/types/src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ mod tests {
2727

2828
// From a hex string.
2929
let hex_str = "0x0000000000000000000000000000000000000000000000000000000000000000";
30-
let b256 = Bytes32::from_str(hex_str).expect("failed to create Bytes32 from string");
30+
let b256 = Bytes32::from_str(hex_str)?;
3131
assert_eq!([0u8; 32], *b256);
3232
// ANCHOR_END: bytes32
3333

@@ -62,7 +62,7 @@ mod tests {
6262

6363
// From a string.
6464
let hex_str = "0x0000000000000000000000000000000000000000000000000000000000000000";
65-
let address = Address::from_str(hex_str).expect("failed to create Address from string");
65+
let address = Address::from_str(hex_str)?;
6666
assert_eq!([0u8; 32], *address);
6767
// ANCHOR_END: address
6868
Ok(())
@@ -83,8 +83,7 @@ mod tests {
8383

8484
// From a string.
8585
let address = "fuel1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqsx2mt2";
86-
let bech32_address =
87-
Bech32Address::from_str(address).expect("failed to create Bech32 address from string");
86+
let bech32_address = Bech32Address::from_str(address)?;
8887
assert_eq!([0u8; 32], *bech32_address.hash());
8988

9089
// From Address
@@ -120,7 +119,7 @@ mod tests {
120119

121120
// From a string.
122121
let hex_str = "0x0000000000000000000000000000000000000000000000000000000000000000";
123-
let asset_id = AssetId::from_str(hex_str).expect("failed to create AssetId from string");
122+
let asset_id = AssetId::from_str(hex_str)?;
124123
assert_eq!([0u8; 32], *asset_id);
125124
// ANCHOR_END: asset_id
126125
Ok(())
@@ -146,8 +145,7 @@ mod tests {
146145

147146
// From a string.
148147
let hex_str = "0x0000000000000000000000000000000000000000000000000000000000000000";
149-
let contract_id =
150-
ContractId::from_str(hex_str).expect("failed to create ContractId from string");
148+
let contract_id = ContractId::from_str(hex_str)?;
151149
assert_eq!([0u8; 32], *contract_id);
152150
// ANCHOR_END: contract_id
153151
Ok(())

examples/wallets/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,9 @@ mod tests {
353353
let wallet = wallets.first().unwrap();
354354

355355
let amount = 1000;
356-
let base_layer_address =
357-
Address::from_str("0x4710162c2e3a95a6faff05139150017c9e38e5e280432d546fae345d6ce6d8fe")
358-
.expect("Invalid address.");
356+
let base_layer_address = Address::from_str(
357+
"0x4710162c2e3a95a6faff05139150017c9e38e5e280432d546fae345d6ce6d8fe",
358+
)?;
359359
let base_layer_address = Bech32Address::from(base_layer_address);
360360
// Transfer an amount of 1000 to the specified base layer address
361361
let (tx_id, msg_id, _receipts) = wallet
@@ -369,12 +369,13 @@ mod tests {
369369
.try_provider()?
370370
.get_message_proof(&tx_id, &msg_id, None, Some(2))
371371
.await?
372-
.expect("Failed to retrieve message proof.");
372+
.expect("failed to retrieve message proof");
373373

374374
// Verify the amount and recipient
375375
assert_eq!(proof.amount, amount);
376376
assert_eq!(proof.recipient, base_layer_address);
377377
// ANCHOR_END: wallet_withdraw_to_base
378+
378379
Ok(())
379380
}
380381
}

packages/fuels-accounts/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ fuel-crypto = { workspace = true, features = ["random"] }
1919
fuel-tx = { workspace = true }
2020
fuel-types = { workspace = true, features = ["random"] }
2121
fuels-core = { workspace = true, default-features = false }
22-
hex = { workspace = true, default-features = false, features = ["std"] }
2322
rand = { workspace = true, default-features = false }
2423
semver = { workspace = true }
2524
tai64 = { workspace = true, features = ["serde"] }

0 commit comments

Comments
 (0)