diff --git a/CHANGELOG.md b/CHANGELOG.md index 9037e48..244af0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ Changelog ========= ========= +#### Version 3.4.0 +* BREAKING CHANGES: Handle custom errors codes/messages + #### Version 3.3.18 * Add sc_call_function_params.dart in archethic_lib_dart.dart diff --git a/lib/archethic_lib_dart.dart b/lib/archethic_lib_dart.dart index 1989a21..b103ee0 100644 --- a/lib/archethic_lib_dart.dart +++ b/lib/archethic_lib_dart.dart @@ -57,6 +57,6 @@ export 'src/utils/confirmations/transaction_sender.dart'; export 'src/utils/crypto.dart'; export 'src/utils/notification_util.dart'; export 'src/utils/oracle/archethic_oracle.dart'; -export 'src/utils/transaction_util.dart'; +export 'src/utils/transaction_error_util.dart'; export 'src/utils/utils.dart'; export 'src/utils/wallet_encoder.dart'; diff --git a/lib/src/utils/confirmations/archethic_transaction_sender.dart b/lib/src/utils/confirmations/archethic_transaction_sender.dart index bbbb068..c21c881 100644 --- a/lib/src/utils/confirmations/archethic_transaction_sender.dart +++ b/lib/src/utils/confirmations/archethic_transaction_sender.dart @@ -159,13 +159,13 @@ class ArchethicTransactionSender TransactionErrorHandler onError, ) { _transactionErrorSubscription = _subscribe( - 'subscription { transactionError(address: "$address") { context, reason} }', + 'subscription { transactionError(address: "$address") { context, error { code, data, message } } }', ).listen( (result) { close(); final transactionError = _errorDtoToModel(result.data); log( - '>>> Transaction KO $address <<< (${transactionError.message})', + '>>> Transaction KO $address <<< (${transactionError.messageLabel})', ); onError( transactionError, @@ -211,23 +211,26 @@ mixin ArchethicTransactionParser { TransactionError _errorDtoToModel(Map? data) { try { final transactionError = data?['transactionError']; - final reason = transactionError?['reason']; - - if (reason == 'Insufficient funds') { - return const TransactionError.insufficientFunds(); - } - - if (reason == 'consensus not reached') { - return const TransactionError.consensusNotReached(); - } + if (transactionError['error'] != null) { + final error = transactionError?['error']; + final code = error['code'] as int; + switch (code) { + case -31000: + return const TransactionError.insufficientFunds(); + case -31501: + return const TransactionError.consensusNotReached(); + case -31502: + return const TransactionError.timeout(); + default: + } - if (reason == 'timeout' || reason == 'connection timeout') { - return const TransactionError.timeout(); + return TransactionError.other( + code: error['code'] as int, + data: error['data'], + message: error['message'], + ); } - - // TODO(reddwarf03): Handle other error types. - - return TransactionError.other(reason: reason); + return const TransactionError.other(); } catch (e) { return const TransactionError.other(); } diff --git a/lib/src/utils/confirmations/transaction_event.dart b/lib/src/utils/confirmations/transaction_event.dart index 295904a..2999ad0 100644 --- a/lib/src/utils/confirmations/transaction_event.dart +++ b/lib/src/utils/confirmations/transaction_event.dart @@ -1,3 +1,4 @@ +import 'package:archethic_lib_dart/src/utils/transaction_error_util.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; part 'transaction_event.freezed.dart'; @@ -28,10 +29,12 @@ class TransactionError with _$TransactionError implements Exception { Object? data, }) = _TransactionRPCError; const factory TransactionError.other({ - String? reason, + int? code, + Object? data, + String? message, }) = _TransactionOtherError; - String get message => map( + String get messageLabel => map( timeout: (_) => 'connection timeout', connectivity: (_) => 'connectivity issue', consensusNotReached: (_) => 'consensus not reached', @@ -43,7 +46,17 @@ class TransactionError with _$TransactionError implements Exception { userRejected: (_) => 'user rejected', unknownAccount: (_) => 'unknown account', rpcError: (_) => 'JSON RPC error', - other: (other) => other.reason ?? 'other reason', + other: (other) { + if (other.data != null && other.data is Map) { + final detailedMessage = extractTransactionErrorMessages( + other.data! as Map, + ); + if (detailedMessage.isNotEmpty) { + return '${other.message}$detailedMessage'; + } + } + return other.message ?? 'other error'; + }, ); } diff --git a/lib/src/utils/confirmations/transaction_event.freezed.dart b/lib/src/utils/confirmations/transaction_event.freezed.dart index 1629286..515f4fe 100644 --- a/lib/src/utils/confirmations/transaction_event.freezed.dart +++ b/lib/src/utils/confirmations/transaction_event.freezed.dart @@ -29,7 +29,7 @@ mixin _$TransactionError { required TResult Function() userRejected, required TResult Function(String accountName) unknownAccount, required TResult Function(int code, String message, Object? data) rpcError, - required TResult Function(String? reason) other, + required TResult Function(int? code, Object? data, String? message) other, }) => throw _privateConstructorUsedError; @optionalTypeArgs @@ -45,7 +45,7 @@ mixin _$TransactionError { TResult? Function()? userRejected, TResult? Function(String accountName)? unknownAccount, TResult? Function(int code, String message, Object? data)? rpcError, - TResult? Function(String? reason)? other, + TResult? Function(int? code, Object? data, String? message)? other, }) => throw _privateConstructorUsedError; @optionalTypeArgs @@ -61,7 +61,7 @@ mixin _$TransactionError { TResult Function()? userRejected, TResult Function(String accountName)? unknownAccount, TResult Function(int code, String message, Object? data)? rpcError, - TResult Function(String? reason)? other, + TResult Function(int? code, Object? data, String? message)? other, required TResult orElse(), }) => throw _privateConstructorUsedError; @@ -194,7 +194,7 @@ class _$TransactionTimeoutImpl extends _TransactionTimeout { required TResult Function() userRejected, required TResult Function(String accountName) unknownAccount, required TResult Function(int code, String message, Object? data) rpcError, - required TResult Function(String? reason) other, + required TResult Function(int? code, Object? data, String? message) other, }) { return timeout(); } @@ -213,7 +213,7 @@ class _$TransactionTimeoutImpl extends _TransactionTimeout { TResult? Function()? userRejected, TResult? Function(String accountName)? unknownAccount, TResult? Function(int code, String message, Object? data)? rpcError, - TResult? Function(String? reason)? other, + TResult? Function(int? code, Object? data, String? message)? other, }) { return timeout?.call(); } @@ -232,7 +232,7 @@ class _$TransactionTimeoutImpl extends _TransactionTimeout { TResult Function()? userRejected, TResult Function(String accountName)? unknownAccount, TResult Function(int code, String message, Object? data)? rpcError, - TResult Function(String? reason)? other, + TResult Function(int? code, Object? data, String? message)? other, required TResult orElse(), }) { if (timeout != null) { @@ -372,7 +372,7 @@ class _$TransactionConnectionErrorImpl extends _TransactionConnectionError { required TResult Function() userRejected, required TResult Function(String accountName) unknownAccount, required TResult Function(int code, String message, Object? data) rpcError, - required TResult Function(String? reason) other, + required TResult Function(int? code, Object? data, String? message) other, }) { return connectivity(); } @@ -391,7 +391,7 @@ class _$TransactionConnectionErrorImpl extends _TransactionConnectionError { TResult? Function()? userRejected, TResult? Function(String accountName)? unknownAccount, TResult? Function(int code, String message, Object? data)? rpcError, - TResult? Function(String? reason)? other, + TResult? Function(int? code, Object? data, String? message)? other, }) { return connectivity?.call(); } @@ -410,7 +410,7 @@ class _$TransactionConnectionErrorImpl extends _TransactionConnectionError { TResult Function()? userRejected, TResult Function(String accountName)? unknownAccount, TResult Function(int code, String message, Object? data)? rpcError, - TResult Function(String? reason)? other, + TResult Function(int? code, Object? data, String? message)? other, required TResult orElse(), }) { if (connectivity != null) { @@ -552,7 +552,7 @@ class _$TransactionConsensusNotReachedErrorImpl required TResult Function() userRejected, required TResult Function(String accountName) unknownAccount, required TResult Function(int code, String message, Object? data) rpcError, - required TResult Function(String? reason) other, + required TResult Function(int? code, Object? data, String? message) other, }) { return consensusNotReached(); } @@ -571,7 +571,7 @@ class _$TransactionConsensusNotReachedErrorImpl TResult? Function()? userRejected, TResult? Function(String accountName)? unknownAccount, TResult? Function(int code, String message, Object? data)? rpcError, - TResult? Function(String? reason)? other, + TResult? Function(int? code, Object? data, String? message)? other, }) { return consensusNotReached?.call(); } @@ -590,7 +590,7 @@ class _$TransactionConsensusNotReachedErrorImpl TResult Function()? userRejected, TResult Function(String accountName)? unknownAccount, TResult Function(int code, String message, Object? data)? rpcError, - TResult Function(String? reason)? other, + TResult Function(int? code, Object? data, String? message)? other, required TResult orElse(), }) { if (consensusNotReached != null) { @@ -727,7 +727,7 @@ class _$TransactionInvalidImpl extends _TransactionInvalid { required TResult Function() userRejected, required TResult Function(String accountName) unknownAccount, required TResult Function(int code, String message, Object? data) rpcError, - required TResult Function(String? reason) other, + required TResult Function(int? code, Object? data, String? message) other, }) { return invalidTransaction(); } @@ -746,7 +746,7 @@ class _$TransactionInvalidImpl extends _TransactionInvalid { TResult? Function()? userRejected, TResult? Function(String accountName)? unknownAccount, TResult? Function(int code, String message, Object? data)? rpcError, - TResult? Function(String? reason)? other, + TResult? Function(int? code, Object? data, String? message)? other, }) { return invalidTransaction?.call(); } @@ -765,7 +765,7 @@ class _$TransactionInvalidImpl extends _TransactionInvalid { TResult Function()? userRejected, TResult Function(String accountName)? unknownAccount, TResult Function(int code, String message, Object? data)? rpcError, - TResult Function(String? reason)? other, + TResult Function(int? code, Object? data, String? message)? other, required TResult orElse(), }) { if (invalidTransaction != null) { @@ -906,7 +906,7 @@ class _$TransactionInvalidConfirmationImpl required TResult Function() userRejected, required TResult Function(String accountName) unknownAccount, required TResult Function(int code, String message, Object? data) rpcError, - required TResult Function(String? reason) other, + required TResult Function(int? code, Object? data, String? message) other, }) { return invalidConfirmation(); } @@ -925,7 +925,7 @@ class _$TransactionInvalidConfirmationImpl TResult? Function()? userRejected, TResult? Function(String accountName)? unknownAccount, TResult? Function(int code, String message, Object? data)? rpcError, - TResult? Function(String? reason)? other, + TResult? Function(int? code, Object? data, String? message)? other, }) { return invalidConfirmation?.call(); } @@ -944,7 +944,7 @@ class _$TransactionInvalidConfirmationImpl TResult Function()? userRejected, TResult Function(String accountName)? unknownAccount, TResult Function(int code, String message, Object? data)? rpcError, - TResult Function(String? reason)? other, + TResult Function(int? code, Object? data, String? message)? other, required TResult orElse(), }) { if (invalidConfirmation != null) { @@ -1085,7 +1085,7 @@ class _$TransactionInsufficientFundsImpl extends _TransactionInsufficientFunds { required TResult Function() userRejected, required TResult Function(String accountName) unknownAccount, required TResult Function(int code, String message, Object? data) rpcError, - required TResult Function(String? reason) other, + required TResult Function(int? code, Object? data, String? message) other, }) { return insufficientFunds(); } @@ -1104,7 +1104,7 @@ class _$TransactionInsufficientFundsImpl extends _TransactionInsufficientFunds { TResult? Function()? userRejected, TResult? Function(String accountName)? unknownAccount, TResult? Function(int code, String message, Object? data)? rpcError, - TResult? Function(String? reason)? other, + TResult? Function(int? code, Object? data, String? message)? other, }) { return insufficientFunds?.call(); } @@ -1123,7 +1123,7 @@ class _$TransactionInsufficientFundsImpl extends _TransactionInsufficientFunds { TResult Function()? userRejected, TResult Function(String accountName)? unknownAccount, TResult Function(int code, String message, Object? data)? rpcError, - TResult Function(String? reason)? other, + TResult Function(int? code, Object? data, String? message)? other, required TResult orElse(), }) { if (insufficientFunds != null) { @@ -1264,7 +1264,7 @@ class _$TransactionServiceNotFoundImpl extends _TransactionServiceNotFound { required TResult Function() userRejected, required TResult Function(String accountName) unknownAccount, required TResult Function(int code, String message, Object? data) rpcError, - required TResult Function(String? reason) other, + required TResult Function(int? code, Object? data, String? message) other, }) { return serviceNotFound(); } @@ -1283,7 +1283,7 @@ class _$TransactionServiceNotFoundImpl extends _TransactionServiceNotFound { TResult? Function()? userRejected, TResult? Function(String accountName)? unknownAccount, TResult? Function(int code, String message, Object? data)? rpcError, - TResult? Function(String? reason)? other, + TResult? Function(int? code, Object? data, String? message)? other, }) { return serviceNotFound?.call(); } @@ -1302,7 +1302,7 @@ class _$TransactionServiceNotFoundImpl extends _TransactionServiceNotFound { TResult Function()? userRejected, TResult Function(String accountName)? unknownAccount, TResult Function(int code, String message, Object? data)? rpcError, - TResult Function(String? reason)? other, + TResult Function(int? code, Object? data, String? message)? other, required TResult orElse(), }) { if (serviceNotFound != null) { @@ -1444,7 +1444,7 @@ class _$TransactionServiceAlreadyExistsImpl required TResult Function() userRejected, required TResult Function(String accountName) unknownAccount, required TResult Function(int code, String message, Object? data) rpcError, - required TResult Function(String? reason) other, + required TResult Function(int? code, Object? data, String? message) other, }) { return serviceAlreadyExists(); } @@ -1463,7 +1463,7 @@ class _$TransactionServiceAlreadyExistsImpl TResult? Function()? userRejected, TResult? Function(String accountName)? unknownAccount, TResult? Function(int code, String message, Object? data)? rpcError, - TResult? Function(String? reason)? other, + TResult? Function(int? code, Object? data, String? message)? other, }) { return serviceAlreadyExists?.call(); } @@ -1482,7 +1482,7 @@ class _$TransactionServiceAlreadyExistsImpl TResult Function()? userRejected, TResult Function(String accountName)? unknownAccount, TResult Function(int code, String message, Object? data)? rpcError, - TResult Function(String? reason)? other, + TResult Function(int? code, Object? data, String? message)? other, required TResult orElse(), }) { if (serviceAlreadyExists != null) { @@ -1622,7 +1622,7 @@ class _$TransactionUserRejectedImpl extends _TransactionUserRejected { required TResult Function() userRejected, required TResult Function(String accountName) unknownAccount, required TResult Function(int code, String message, Object? data) rpcError, - required TResult Function(String? reason) other, + required TResult Function(int? code, Object? data, String? message) other, }) { return userRejected(); } @@ -1641,7 +1641,7 @@ class _$TransactionUserRejectedImpl extends _TransactionUserRejected { TResult? Function()? userRejected, TResult? Function(String accountName)? unknownAccount, TResult? Function(int code, String message, Object? data)? rpcError, - TResult? Function(String? reason)? other, + TResult? Function(int? code, Object? data, String? message)? other, }) { return userRejected?.call(); } @@ -1660,7 +1660,7 @@ class _$TransactionUserRejectedImpl extends _TransactionUserRejected { TResult Function()? userRejected, TResult Function(String accountName)? unknownAccount, TResult Function(int code, String message, Object? data)? rpcError, - TResult Function(String? reason)? other, + TResult Function(int? code, Object? data, String? message)? other, required TResult orElse(), }) { if (userRejected != null) { @@ -1828,7 +1828,7 @@ class _$TransactionUnknownAccountImpl extends _TransactionUnknownAccount { required TResult Function() userRejected, required TResult Function(String accountName) unknownAccount, required TResult Function(int code, String message, Object? data) rpcError, - required TResult Function(String? reason) other, + required TResult Function(int? code, Object? data, String? message) other, }) { return unknownAccount(accountName); } @@ -1847,7 +1847,7 @@ class _$TransactionUnknownAccountImpl extends _TransactionUnknownAccount { TResult? Function()? userRejected, TResult? Function(String accountName)? unknownAccount, TResult? Function(int code, String message, Object? data)? rpcError, - TResult? Function(String? reason)? other, + TResult? Function(int? code, Object? data, String? message)? other, }) { return unknownAccount?.call(accountName); } @@ -1866,7 +1866,7 @@ class _$TransactionUnknownAccountImpl extends _TransactionUnknownAccount { TResult Function()? userRejected, TResult Function(String accountName)? unknownAccount, TResult Function(int code, String message, Object? data)? rpcError, - TResult Function(String? reason)? other, + TResult Function(int? code, Object? data, String? message)? other, required TResult orElse(), }) { if (unknownAccount != null) { @@ -2051,7 +2051,7 @@ class _$TransactionRPCErrorImpl extends _TransactionRPCError { required TResult Function() userRejected, required TResult Function(String accountName) unknownAccount, required TResult Function(int code, String message, Object? data) rpcError, - required TResult Function(String? reason) other, + required TResult Function(int? code, Object? data, String? message) other, }) { return rpcError(code, message, data); } @@ -2070,7 +2070,7 @@ class _$TransactionRPCErrorImpl extends _TransactionRPCError { TResult? Function()? userRejected, TResult? Function(String accountName)? unknownAccount, TResult? Function(int code, String message, Object? data)? rpcError, - TResult? Function(String? reason)? other, + TResult? Function(int? code, Object? data, String? message)? other, }) { return rpcError?.call(code, message, data); } @@ -2089,7 +2089,7 @@ class _$TransactionRPCErrorImpl extends _TransactionRPCError { TResult Function()? userRejected, TResult Function(String accountName)? unknownAccount, TResult Function(int code, String message, Object? data)? rpcError, - TResult Function(String? reason)? other, + TResult Function(int? code, Object? data, String? message)? other, required TResult orElse(), }) { if (rpcError != null) { @@ -2193,7 +2193,7 @@ abstract class _$$TransactionOtherErrorImplCopyWith<$Res> { $Res Function(_$TransactionOtherErrorImpl) then) = __$$TransactionOtherErrorImplCopyWithImpl<$Res>; @useResult - $Res call({String? reason}); + $Res call({int? code, Object? data, String? message}); } /// @nodoc @@ -2207,12 +2207,19 @@ class __$$TransactionOtherErrorImplCopyWithImpl<$Res> @pragma('vm:prefer-inline') @override $Res call({ - Object? reason = freezed, + Object? code = freezed, + Object? data = freezed, + Object? message = freezed, }) { return _then(_$TransactionOtherErrorImpl( - reason: freezed == reason - ? _value.reason - : reason // ignore: cast_nullable_to_non_nullable + code: freezed == code + ? _value.code + : code // ignore: cast_nullable_to_non_nullable + as int?, + data: freezed == data ? _value.data : data, + message: freezed == message + ? _value.message + : message // ignore: cast_nullable_to_non_nullable as String?, )); } @@ -2221,14 +2228,19 @@ class __$$TransactionOtherErrorImplCopyWithImpl<$Res> /// @nodoc class _$TransactionOtherErrorImpl extends _TransactionOtherError { - const _$TransactionOtherErrorImpl({this.reason}) : super._(); + const _$TransactionOtherErrorImpl({this.code, this.data, this.message}) + : super._(); @override - final String? reason; + final int? code; + @override + final Object? data; + @override + final String? message; @override String toString() { - return 'TransactionError.other(reason: $reason)'; + return 'TransactionError.other(code: $code, data: $data, message: $message)'; } @override @@ -2236,11 +2248,14 @@ class _$TransactionOtherErrorImpl extends _TransactionOtherError { return identical(this, other) || (other.runtimeType == runtimeType && other is _$TransactionOtherErrorImpl && - (identical(other.reason, reason) || other.reason == reason)); + (identical(other.code, code) || other.code == code) && + const DeepCollectionEquality().equals(other.data, data) && + (identical(other.message, message) || other.message == message)); } @override - int get hashCode => Object.hash(runtimeType, reason); + int get hashCode => Object.hash( + runtimeType, code, const DeepCollectionEquality().hash(data), message); @JsonKey(ignore: true) @override @@ -2263,9 +2278,9 @@ class _$TransactionOtherErrorImpl extends _TransactionOtherError { required TResult Function() userRejected, required TResult Function(String accountName) unknownAccount, required TResult Function(int code, String message, Object? data) rpcError, - required TResult Function(String? reason) other, + required TResult Function(int? code, Object? data, String? message) other, }) { - return other(reason); + return other(code, data, message); } @override @@ -2282,9 +2297,9 @@ class _$TransactionOtherErrorImpl extends _TransactionOtherError { TResult? Function()? userRejected, TResult? Function(String accountName)? unknownAccount, TResult? Function(int code, String message, Object? data)? rpcError, - TResult? Function(String? reason)? other, + TResult? Function(int? code, Object? data, String? message)? other, }) { - return other?.call(reason); + return other?.call(code, data, message); } @override @@ -2301,11 +2316,11 @@ class _$TransactionOtherErrorImpl extends _TransactionOtherError { TResult Function()? userRejected, TResult Function(String accountName)? unknownAccount, TResult Function(int code, String message, Object? data)? rpcError, - TResult Function(String? reason)? other, + TResult Function(int? code, Object? data, String? message)? other, required TResult orElse(), }) { if (other != null) { - return other(reason); + return other(code, data, message); } return orElse(); } @@ -2384,11 +2399,15 @@ class _$TransactionOtherErrorImpl extends _TransactionOtherError { } abstract class _TransactionOtherError extends TransactionError { - const factory _TransactionOtherError({final String? reason}) = - _$TransactionOtherErrorImpl; + const factory _TransactionOtherError( + {final int? code, + final Object? data, + final String? message}) = _$TransactionOtherErrorImpl; const _TransactionOtherError._() : super._(); - String? get reason; + int? get code; + Object? get data; + String? get message; @JsonKey(ignore: true) _$$TransactionOtherErrorImplCopyWith<_$TransactionOtherErrorImpl> get copyWith => throw _privateConstructorUsedError; diff --git a/lib/src/utils/transaction_error_util.dart b/lib/src/utils/transaction_error_util.dart new file mode 100644 index 0000000..e199cda --- /dev/null +++ b/lib/src/utils/transaction_error_util.dart @@ -0,0 +1,23 @@ +/// SPDX-License-Identifier: AGPL-3.0-or-later + +String extractTransactionErrorMessages(Map json) { + final messageBuffer = StringBuffer(); + + void extractMessages(Map map) { + map.forEach((key, value) { + if (key == 'message') { + messageBuffer.write(' - '); + messageBuffer.write(value); + if (value is Map) { + extractMessages(value); + } + } else if (value is Map) { + extractMessages(value); + } + }); + } + + extractMessages(json); + + return messageBuffer.toString(); +} diff --git a/pubspec.yaml b/pubspec.yaml index 09f17dd..09f827e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -3,7 +3,7 @@ name: archethic_lib_dart description: Archethic dart library for Flutter for Node and Browser. This library aims to provide a easy way to create Archethic transaction and to send them over the network homepage: https://github.com/archethic-foundation/libdart -version: 3.3.18 +version: 3.4.0 environment: sdk: ">=3.3.0 <4.0.0"