Skip to content

Commit

Permalink
Merge pull request #20 from archethic-foundation/error_management
Browse files Browse the repository at this point in the history
Error_management
  • Loading branch information
redDwarf03 authored Oct 1, 2024
2 parents e5e4d30 + 282e7c7 commit 0a4bdb5
Show file tree
Hide file tree
Showing 11 changed files with 625 additions and 385 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,29 @@ wagmi.Web3Modal.init(
wagmi.Web3Modal.open();
```

### Error management

Errors from wagmi can be handled using the `WagmiError` object.
Wagmi provides an error stack through the `cause` property.
The `findError` method with the error type allows checking if a specific error is in the error stack.

The different exceptions are available in the `WagmiErrors` enumeration

```dart
try {
final transactionHash = await wagmi.Core.writeContract(
parameters,
);
} on wagmi.WagmiError catch (e, stackTrace) {
if (e.findError(wagmi.WagmiErrors.UserRejectedRequestError) != null) {
throw Exception('userRejected');
}
if (e.findError(wagmi.WagmiErrors.InsufficientFundsError) != null) {
throw Exception('insufficientFunds');
}
throw Exception('${e.shortMessage}');
}
```

## Available Actions

Expand Down
3 changes: 2 additions & 1 deletion example/lib/actions/add_token.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:example/actions/components/spacer.dart';
import 'package:example/actions/components/tab_header.dart';
import 'package:flutter/material.dart';
import 'package:wagmi_flutter_web/wagmi_flutter_web.dart' as wagmi;
import 'package:wagmi_flutter_web/wagmi_flutter_web.dart';

class AddTokenExample extends StatefulWidget {
const AddTokenExample({super.key});
Expand Down Expand Up @@ -78,7 +79,7 @@ class _AddTokenExampleState extends State<AddTokenExample> {
),
);
_operationSucceed('Token added');
} catch (e) {
} on WagmiError catch (e) {
_operationFailed(e.toString());
}
},
Expand Down
3 changes: 2 additions & 1 deletion example/lib/actions/read_contract.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:example/actions/components/spacer.dart';
import 'package:example/actions/components/tab_header.dart';
import 'package:flutter/material.dart';
import 'package:wagmi_flutter_web/wagmi_flutter_web.dart' as wagmi;
import 'package:wagmi_flutter_web/wagmi_flutter_web.dart';

class ReadContractExample extends StatefulWidget {
const ReadContractExample({super.key});
Expand Down Expand Up @@ -175,7 +176,7 @@ class _ReadContractExampleState extends State<ReadContractExample> {
),
);
_operationSucceed(result.toString());
} catch (e) {
} on WagmiError catch (e) {
_operationFailed(e.toString());
}
},
Expand Down
3 changes: 2 additions & 1 deletion example/lib/actions/write_contract.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:example/actions/components/spacer.dart';
import 'package:example/actions/components/tab_header.dart';
import 'package:flutter/material.dart';
import 'package:wagmi_flutter_web/wagmi_flutter_web.dart' as wagmi;
import 'package:wagmi_flutter_web/wagmi_flutter_web.dart';

class WriteContractExample extends StatefulWidget {
const WriteContractExample({super.key});
Expand Down Expand Up @@ -188,7 +189,7 @@ class _WriteContractExampleState extends State<WriteContractExample> {
),
);
_operationSucceed(result);
} catch (e) {
} on WagmiError catch (e) {
_operationFailed(e.toString());
}
},
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.1-dev.7"
version: "0.0.1-dev.8"
watcher:
dependency: transitive
description:
Expand Down
25 changes: 20 additions & 5 deletions lib/src/js/models/error.js.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
part of '../wagmi.js.dart';

/// Generic JSError
extension type JSError._(JSObject _) implements JSObject {}

/// Generic indexed DB/Javascript error.
extension JSErrorExt on JSError {
@JS()
extension type JSError._(JSObject _) implements JSObject {
/// Get the error message
external String? get message;

/// Get the error message
external String? get name;

external JSArray<JSString>? get metaMessages;
external JSString? get shortMessage;
external JSString? get version;
external JSError? get cause;
external JSString? get docsPath;

WagmiError get toDart => WagmiError(
message: message,
name: name,
name:
WagmiErrors.values.firstWhereOrNull((value) => value.name == name),
metaMessages: metaMessages?.toDart
.map(
(message) => message.toDart,
)
.toList(),
shortMessage: shortMessage?.toDart,
version: version?.toDart,
cause: cause?.toDart,
docsPath: docsPath?.toDart,
details: toMap(),
);
}
20 changes: 10 additions & 10 deletions lib/src/js/wagmi.js.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import 'dart:js_interop';
// ignore: avoid_web_libraries_in_flutter
import 'dart:js_util' as js_util;

import 'package:collection/collection.dart';
import 'package:wagmi_flutter_web/src/models/connections.dart';
import 'package:wagmi_flutter_web/src/models/error.dart';
import 'package:wagmi_flutter_web/src/models/log.dart';
import 'package:wagmi_flutter_web/wagmi_flutter_web.dart';

part 'actions/call.js.dart';
part 'actions/deploy_contract.js.dart';
part 'actions/disconnect.js.dart';
part 'actions/estimate_fees_per_gas.js.dart';
part 'actions/estimate_gas.js.dart';
Expand All @@ -26,30 +27,29 @@ part 'actions/get_transaction.js.dart';
part 'actions/get_transaction_confirmations.js.dart';
part 'actions/get_transaction_count.js.dart';
part 'actions/get_transaction_receipt.js.dart';
part 'actions/get_wallet_client.js.dart';
part 'actions/read_contract.js.dart';
part 'actions/read_contracts.js.dart';
part 'actions/send_transaction.js.dart';
part 'actions/sign_message.js.dart';
part 'actions/watch_asset.js.dart';
part 'actions/switch_account.js.dart';
part 'actions/switch_chain.js.dart';
part 'actions/verify_message.js.dart';
part 'actions/wait_for_transaction_receipt.js.dart';
part 'actions/watch_account.js.dart';
part 'actions/watch_asset.js.dart';
part 'actions/watch_chain_id.js.dart';
part 'actions/watch_connections.js.dart';
part 'actions/watch_contract_event.js.dart';
part 'actions/write_contract.js.dart';
part 'actions/watch_account.js.dart';
part 'actions/watch_connections.js.dart';
part 'actions/get_wallet_client.js.dart';
part 'actions/deploy_contract.js.dart';
part 'actions/verify_message.js.dart';
part 'actions/switch_account.js.dart';
part 'actions/switch_chain.js.dart';
part 'models/account.js.dart';
part 'models/appkit.js.dart';
part 'models/bigint.js.dart';
part 'models/block_tag.js.dart';
part 'models/chain.js.dart';
part 'models/config.js.dart';
part 'models/connector.js.dart';
part 'models/connections.js.dart';
part 'models/connector.js.dart';
part 'models/data_image.js.dart';
part 'models/error.js.dart';
part 'models/format_unit.js.dart';
Expand Down
191 changes: 188 additions & 3 deletions lib/src/models/error.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,197 @@
// ignore_for_file: constant_identifier_names

class WagmiError implements Exception {
WagmiError({
this.message,
this.name,
this.message,
this.metaMessages,
this.shortMessage,
this.version,
this.cause,
this.docsPath,
this.details,
});

final WagmiErrors? name;
final String? message;
final String? name;
final List<String>? metaMessages;
final String? shortMessage;
final String? version;
final WagmiError? cause;
final String? docsPath;

/// The raw error object
final Map<String, dynamic>? details;

/// Returns the first error or cause matching given type.
WagmiError? findError(WagmiErrors errorName) {
if (name == errorName) return this;

if (cause == null) return null;

return cause!.findError(errorName);
}

@override
String toString() => '$name : $message';
String toString() => '$name : ${message ?? shortMessage}';
}

enum WagmiErrors {
/// [abi](https://github.com/wevm/viem/blob/main/src/errors/abi.ts)
AbiConstructorNotFoundError,
AbiConstructorParamsNotFoundError,
AbiDecodingDataSizeInvalidError,
AbiDecodingDataSizeTooSmallError,
AbiDecodingZeroDataError,
AbiEncodingArrayLengthMismatchError,
AbiEncodingBytesSizeMismatchError,
AbiEncodingLengthMismatchError,
AbiErrorInputsNotFoundError,
AbiErrorNotFoundError,
AbiErrorSignatureNotFoundError,
AbiEventSignatureEmptyTopicsError,
AbiEventSignatureNotFoundError,
AbiEventNotFoundError,
AbiFunctionNotFoundError,
AbiFunctionOutputsNotFoundError,
AbiFunctionSignatureNotFoundError,
AbiItemAmbiguityError,
BytesSizeMismatchError,
InvalidAbiEncodingTypeError,
InvalidAbiDecodingTypeError,
InvalidArrayError,
InvalidDefinitionTypeError,

/// [account](https://github.com/wevm/viem/blob/main/src/errors/account.ts)
AccountNotFoundError,
AccountTypeNotSupportedError,

/// [address](https://github.com/wevm/viem/blob/main/src/errors/address.ts)
InvalidAddressError,

/// [blob](https://github.com/wevm/viem/blob/main/src/errors/blob.ts)
BlobSizeTooLargeError,
EmptyBlobError,
InvalidVersionedHashSizeError,
InvalidVersionedHashVersionError,

/// [block](https://github.com/wevm/viem/blob/main/src/errors/block.ts)
BlockNotFoundError,

/// [ccip](https://github.com/wevm/viem/blob/main/src/errors/ccip.ts)
OffchainLookupError,
OffchainLookupResponseMalformedError,
OffchainLookupSenderMismatchError,

/// [chain](https://github.com/wevm/viem/blob/main/src/errors/chain.ts)
ChainMismatchError,
ChainNotFoundError,
ClientChainNotConfiguredError,
InvalidChainIdError,

/// [contract](https://github.com/wevm/viem/blob/main/src/errors/contract.ts)
CallExecutionError,
ContractFunctionExecutionError,
ContractFunctionRevertedError,
ContractFunctionZeroDataError,
CounterfactualDeploymentFailedError,
RawContractError,

/// [cursor](https://github.com/wevm/viem/blob/main/src/errors/cursor.ts)
NegativeOffsetError,
PositionOutOfBoundsError,
RecursiveReadLimitExceededError,

/// [data](https://github.com/wevm/viem/blob/main/src/errors/data.ts)
SliceOffsetOutOfBoundsError,
SizeExceedsPaddingSizeError,
InvalidBytesLengthError,

/// [encoding](https://github.com/wevm/viem/blob/main/src/errors/encoding.ts)
IntegerOutOfRangeError,
InvalidBytesBooleanError,
InvalidHexBooleanError,
InvalidHexValueError,
SizeOverflowError,

/// [ens](https://github.com/wevm/viem/blob/main/src/errors/ens.ts)
EnsAvatarInvalidMetadataError,
EnsAvatarInvalidNftUriError,
EnsAvatarUriResolutionError,
EnsAvatarUnsupportedNamespaceError,

/// [estimateGas](https://github.com/wevm/viem/blob/main/src/errors/estimateGas.ts)
EstimateGasExecutionError,

/// [fee](https://github.com/wevm/viem/blob/main/src/errors/fee.ts)
BaseFeeScalarError,
MaxFeePerGasTooLowError,

/// [log](https://github.com/wevm/viem/blob/main/src/errors/log.ts)
FilterTypeNotSupportedError,

/// [node](https://github.com/wevm/viem/blob/main/src/errors/node.ts)
ExecutionRevertedError,
FeeCapTooHighError,
FeeCapTooLowError,
NonceTooHighError,
NonceTooLowError,
NonceMaxValueError,
InsufficientFundsError,
IntrinsicGasTooHighError,
IntrinsicGasTooLowError,
TransactionTypeNotSupportedError,
TipAboveFeeCapError,
UnknownNodeError,

/// [request](https://github.com/wevm/viem/blob/main/src/errors/request.ts)
HttpRequestError,
WebSocketRequestError,
RpcRequestError,
SocketClosedError,
TimeoutError,

/// [rpc](https://github.com/wevm/viem/blob/main/src/errors/rpc.ts)
ProviderRpcError,
ParseRpcError,
InvalidRequestRpcError,
MethodNotFoundRpcError,
InvalidParamsRpcError,
InternalRpcError,
InvalidInputRpcError,
ResourceNotFoundRpcError,
ResourceUnavailableRpcError,
TransactionRejectedRpcError,
MethodNotSupportedRpcError,
LimitExceededRpcError,
JsonRpcVersionUnsupportedError,
UserRejectedRequestError,
UnauthorizedProviderError,
UnsupportedProviderMethodError,
ProviderDisconnectedError,
ChainDisconnectedError,
SwitchChainError,
UnknownRpcError,

/// [siwe](https://github.com/wevm/viem/blob/main/src/errors/siwe.ts)
SiweInvalidMessageFieldError,

/// [stateOverride](https://github.com/wevm/viem/blob/main/src/errors/stateOverride.ts)
AccountStateConflictError,
StateAssignmentConflictError,

/// [transaction](https://github.com/wevm/viem/blob/main/src/errors/transaction.ts)
FeeConflictError,
InvalidLegacyVError,
InvalidSerializableTransactionError,
InvalidSerializedTransactionTypeError,
InvalidSerializedTransactionError,
InvalidStorageKeySizeError,
TransactionExecutionError,
TransactionNotFoundError,
TransactionReceiptNotFoundError,
WaitForTransactionReceiptTimeoutError,

/// [transport](https://github.com/wevm/viem/blob/main/src/errors/transport.ts)
UrlRequiredError,
}
Loading

0 comments on commit 0a4bdb5

Please sign in to comment.