Skip to content

Commit

Permalink
Correctly forward RPC errors that are not 404
Browse files Browse the repository at this point in the history
  • Loading branch information
willyfromtheblock committed Nov 6, 2022
1 parent b911506 commit 1897242
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.1.5
- Correctly forward RPC errors that are not 404
- disable loggint to print

## 2.1.4
- Rebase HTTP calls on Dio lib

Expand Down
18 changes: 14 additions & 4 deletions lib/src/rpcclient.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class RPCClient {
_dioClient!.interceptors.add(
RetryInterceptor(
dio: _dioClient!,
logPrint: print,
logPrint: null,
retries: 5,
),
);
Expand Down Expand Up @@ -124,6 +124,8 @@ class RPCClient {
}
} on DioError catch (e) {
if (e.type == DioErrorType.response) {
var errorResponseBody = e.response!.data;

switch (e.error) {
case "Http status error [401]":
throw HTTPException(
Expand All @@ -132,9 +134,8 @@ class RPCClient {
);

case "Http status error [404]":
var body = e.response!.data;
if (body['error'] != null) {
var error = body['error'];
if (errorResponseBody['error'] != null) {
var error = errorResponseBody['error'];
throw RPCException(
errorCode: error['code'],
errorMsg: error['message'],
Expand All @@ -147,6 +148,15 @@ class RPCClient {
message: 'Internal Server Error',
);
default:
if (errorResponseBody['error'] != null) {
var error = errorResponseBody['error'];
throw RPCException(
errorCode: error['code'],
errorMsg: error['message'],
method: methodName,
params: params,
);
}
throw HTTPException(
code: 500,
message: 'Internal Server Error',
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: dart_coin_rpc
description: A simple RPC client for bitcoin-like coins created in dart.
version: 2.1.4
version: 2.1.5
homepage: https://github.com/Vesta-wallet/dart-coin-rpc

environment:
Expand Down
16 changes: 15 additions & 1 deletion test/connection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void main() async {
'success',
() {
test(
'sucess: try call with parameters',
'try call with parameters',
() async {
var res = await client.call(
'validateaddress',
Expand Down Expand Up @@ -81,6 +81,20 @@ void main() async {
);
},
);
test(
'try sendrawtransaction with invalid hex',
() async {
expect(
() async => await client.call(
'sendrawtransaction',
["asdf"],
),
throwsA(
isA<RPCException>(),
),
);
},
);
},
);
}

0 comments on commit 1897242

Please sign in to comment.