Skip to content

Commit ec1bc33

Browse files
committed
implement error check type
1 parent 5437719 commit ec1bc33

File tree

3 files changed

+55
-21
lines changed

3 files changed

+55
-21
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class StellarBalanceException implements Exception {
2+
final String message;
3+
final String? resultXdr;
4+
final bool isLowReserve;
5+
6+
StellarBalanceException({
7+
required this.message,
8+
this.resultXdr,
9+
this.isLowReserve = false,
10+
});
11+
12+
@override
13+
String toString() {
14+
if (resultXdr != null) {
15+
return 'StellarBalanceException: $message (XDR: $resultXdr)';
16+
}
17+
return 'StellarBalanceException: $message';
18+
}
19+
20+
static StellarBalanceException fromOperationResult(
21+
List<String?>? operationCodes,
22+
String? resultXdr,
23+
) {
24+
if (operationCodes?.contains('op_low_reserve') == true) {
25+
return StellarBalanceException(
26+
message: 'Transaction failed due to low reserve',
27+
resultXdr: resultXdr,
28+
isLowReserve: true,
29+
);
30+
}
31+
return StellarBalanceException(
32+
message: 'Transaction failed',
33+
resultXdr: resultXdr,
34+
);
35+
}
36+
}

packages/stellar_client/lib/src/client.dart

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -667,16 +667,15 @@ class Client {
667667
final SubmitTransactionResponse response =
668668
await _sdk.submitTransaction(transaction);
669669
if (!response.success) {
670-
if (response.extras?.resultCodes?.operationsResultCodes
671-
?.contains('op_low_reserve') ==
672-
true) {
673-
throw Exception('Transaction failed due to low reserve.');
674-
}
675-
logger.e('Transaction failed with result: ${response.resultXdr}');
676-
return false;
670+
throw StellarBalanceException.fromOperationResult(
671+
response.extras?.resultCodes?.operationsResultCodes,
672+
response.resultXdr,
673+
);
677674
}
678675

679676
return true;
677+
} on StellarBalanceException catch (_) {
678+
rethrow;
680679
} catch (error) {
681680
throw Exception('Transaction failed due to: ${error.toString()}');
682681
}
@@ -712,15 +711,14 @@ class Client {
712711
final SubmitTransactionResponse response =
713712
await _sdk.submitTransaction(transaction);
714713
if (!response.success) {
715-
if (response.extras?.resultCodes?.operationsResultCodes
716-
?.contains('op_low_reserve') ==
717-
true) {
718-
throw Exception('Transaction failed due to low reserve.');
719-
}
720-
logger.e('Transaction failed with result: ${response.resultXdr}');
721-
return false;
714+
throw StellarBalanceException.fromOperationResult(
715+
response.extras?.resultCodes?.operationsResultCodes,
716+
response.resultXdr,
717+
);
722718
}
723719
return true;
720+
} on StellarBalanceException catch (_) {
721+
rethrow;
724722
} catch (error) {
725723
throw Exception('Transaction failed due to: ${error.toString()}');
726724
}
@@ -766,15 +764,14 @@ class Client {
766764
final SubmitTransactionResponse response =
767765
await _sdk.submitTransaction(transaction);
768766
if (!response.success) {
769-
if (response.extras?.resultCodes?.operationsResultCodes
770-
?.contains('op_low_reserve') ==
771-
true) {
772-
throw Exception('Transaction failed due to low reserve.');
773-
}
774-
logger.e('Transaction failed with result: ${response.resultXdr}');
775-
return false;
767+
throw StellarBalanceException.fromOperationResult(
768+
response.extras?.resultCodes?.operationsResultCodes,
769+
response.resultXdr,
770+
);
776771
}
777772
return true;
773+
} on StellarBalanceException catch (_) {
774+
rethrow;
778775
} catch (error) {
779776
throw Exception('Transaction failed due to: ${error.toString()}');
780777
}

packages/stellar_client/lib/stellar_client.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'dart:typed_data';
55

66
import 'package:stellar_client/models/balance.dart';
77
import 'package:stellar_client/models/currency.dart' as currency;
8+
import 'package:stellar_client/models/exceptions.dart';
89
import 'package:stellar_client/models/transaction_data.dart';
910
import 'package:stellar_client/models/vesting_account.dart';
1011
import 'package:stellar_client/models/transaction.dart';

0 commit comments

Comments
 (0)