Skip to content

Commit

Permalink
💥 BREAKING CHANGES: Handle custom errors codes/messages
Browse files Browse the repository at this point in the history
  • Loading branch information
redDwarf03 committed Jun 6, 2024
1 parent c580258 commit 759d653
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 77 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/archethic_lib_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
37 changes: 20 additions & 17 deletions lib/src/utils/confirmations/archethic_transaction_sender.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,13 @@ class ArchethicTransactionSender
TransactionErrorHandler onError,
) {
_transactionErrorSubscription = _subscribe<TransactionError>(
'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,
Expand Down Expand Up @@ -211,23 +211,26 @@ mixin ArchethicTransactionParser {
TransactionError _errorDtoToModel(Map<String, dynamic>? 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();
}
Expand Down
19 changes: 16 additions & 3 deletions lib/src/utils/confirmations/transaction_event.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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',
Expand All @@ -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<String, dynamic>) {
final detailedMessage = extractTransactionErrorMessages(
other.data! as Map<String, dynamic>,
);
if (detailedMessage.isNotEmpty) {
return '${other.message}$detailedMessage';
}
}
return other.message ?? 'other error';
},
);
}

Expand Down
Loading

0 comments on commit 759d653

Please sign in to comment.