Skip to content

Commit 759d653

Browse files
committed
💥 BREAKING CHANGES: Handle custom errors codes/messages
1 parent c580258 commit 759d653

7 files changed

Lines changed: 138 additions & 77 deletions

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ Changelog
22
=========
33
=========
44

5+
#### Version 3.4.0
6+
* BREAKING CHANGES: Handle custom errors codes/messages
7+
58
#### Version 3.3.18
69
* Add sc_call_function_params.dart in archethic_lib_dart.dart
710

‎lib/archethic_lib_dart.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ export 'src/utils/confirmations/transaction_sender.dart';
5757
export 'src/utils/crypto.dart';
5858
export 'src/utils/notification_util.dart';
5959
export 'src/utils/oracle/archethic_oracle.dart';
60-
export 'src/utils/transaction_util.dart';
60+
export 'src/utils/transaction_error_util.dart';
6161
export 'src/utils/utils.dart';
6262
export 'src/utils/wallet_encoder.dart';

‎lib/src/utils/confirmations/archethic_transaction_sender.dart‎

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,13 @@ class ArchethicTransactionSender
159159
TransactionErrorHandler onError,
160160
) {
161161
_transactionErrorSubscription = _subscribe<TransactionError>(
162-
'subscription { transactionError(address: "$address") { context, reason} }',
162+
'subscription { transactionError(address: "$address") { context, error { code, data, message } } }',
163163
).listen(
164164
(result) {
165165
close();
166166
final transactionError = _errorDtoToModel(result.data);
167167
log(
168-
'>>> Transaction KO $address <<< (${transactionError.message})',
168+
'>>> Transaction KO $address <<< (${transactionError.messageLabel})',
169169
);
170170
onError(
171171
transactionError,
@@ -211,23 +211,26 @@ mixin ArchethicTransactionParser {
211211
TransactionError _errorDtoToModel(Map<String, dynamic>? data) {
212212
try {
213213
final transactionError = data?['transactionError'];
214-
final reason = transactionError?['reason'];
215-
216-
if (reason == 'Insufficient funds') {
217-
return const TransactionError.insufficientFunds();
218-
}
219-
220-
if (reason == 'consensus not reached') {
221-
return const TransactionError.consensusNotReached();
222-
}
214+
if (transactionError['error'] != null) {
215+
final error = transactionError?['error'];
216+
final code = error['code'] as int;
217+
switch (code) {
218+
case -31000:
219+
return const TransactionError.insufficientFunds();
220+
case -31501:
221+
return const TransactionError.consensusNotReached();
222+
case -31502:
223+
return const TransactionError.timeout();
224+
default:
225+
}
223226

224-
if (reason == 'timeout' || reason == 'connection timeout') {
225-
return const TransactionError.timeout();
227+
return TransactionError.other(
228+
code: error['code'] as int,
229+
data: error['data'],
230+
message: error['message'],
231+
);
226232
}
227-
228-
// TODO(reddwarf03): Handle other error types.
229-
230-
return TransactionError.other(reason: reason);
233+
return const TransactionError.other();
231234
} catch (e) {
232235
return const TransactionError.other();
233236
}

‎lib/src/utils/confirmations/transaction_event.dart‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:archethic_lib_dart/src/utils/transaction_error_util.dart';
12
import 'package:freezed_annotation/freezed_annotation.dart';
23

34
part 'transaction_event.freezed.dart';
@@ -28,10 +29,12 @@ class TransactionError with _$TransactionError implements Exception {
2829
Object? data,
2930
}) = _TransactionRPCError;
3031
const factory TransactionError.other({
31-
String? reason,
32+
int? code,
33+
Object? data,
34+
String? message,
3235
}) = _TransactionOtherError;
3336

34-
String get message => map(
37+
String get messageLabel => map(
3538
timeout: (_) => 'connection timeout',
3639
connectivity: (_) => 'connectivity issue',
3740
consensusNotReached: (_) => 'consensus not reached',
@@ -43,7 +46,17 @@ class TransactionError with _$TransactionError implements Exception {
4346
userRejected: (_) => 'user rejected',
4447
unknownAccount: (_) => 'unknown account',
4548
rpcError: (_) => 'JSON RPC error',
46-
other: (other) => other.reason ?? 'other reason',
49+
other: (other) {
50+
if (other.data != null && other.data is Map<String, dynamic>) {
51+
final detailedMessage = extractTransactionErrorMessages(
52+
other.data! as Map<String, dynamic>,
53+
);
54+
if (detailedMessage.isNotEmpty) {
55+
return '${other.message}$detailedMessage';
56+
}
57+
}
58+
return other.message ?? 'other error';
59+
},
4760
);
4861
}
4962

0 commit comments

Comments
 (0)