Skip to content

Commit

Permalink
feat(rpc): throw exception when cln returns an error
Browse files Browse the repository at this point in the history
  • Loading branch information
swaptr authored and vincenzopalazzo committed Aug 7, 2022
1 parent 8f94e62 commit 8d2aaf8
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 21 deletions.
4 changes: 2 additions & 2 deletions melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ scripts:
select-package:

rpc_analyze:
run: melos exec -c 1 --scope="rpc" -- "dart format --set-exit-if-changed . && dart analyze . --fatal-infos"
run: melos exec -c 1 --scope="clightning_rpc" -- "dart format --set-exit-if-changed . && dart analyze . --fatal-infos"
description: Run dart analyzer in a specific package.

plugin_analyze:
Expand Down Expand Up @@ -48,7 +48,7 @@ scripts:

rpc_test:
description: Run tests in a specific package.
run: melos exec --concurrency=2 --scope="rpc" -- "dart pub get && dart pub run test"
run: melos exec --concurrency=2 --scope="clightning_rpc" -- "dart pub get && dart pub run test"
env:
MELOS_TEST: true

Expand Down
4 changes: 3 additions & 1 deletion packages/rpc/lib/src/clightning_rpc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class RPCClient implements LightningClient {
}

@override
void close() async {}
void close() async {
return;
}

@override
Future<T> call<R extends Serializable, T>(
Expand Down
7 changes: 7 additions & 0 deletions packages/rpc/lib/src/utils/exception/ln_client_error.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class LNClientException implements Exception {
String message;
int code;
String? data;

LNClientException(this.code, this.message, this.data);
}
24 changes: 19 additions & 5 deletions packages/rpc/lib/src/utils/unix_rpc_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:io';

import 'package:cln_common/cln_common.dart';
import 'package:jsonrpc2/jsonrpc2.dart';
import 'exception/ln_client_error.dart';

class UnixRPCClient extends ServerProxyBase {
UnixRPCClient(String path) : super(path);
Expand All @@ -17,12 +18,25 @@ class UnixRPCClient extends ServerProxyBase {
var socket = await Socket.connect(address, 0);
socket.add(utf8.encode(package));

socket.listen((event) {
LogManager.getInstance.debug('Event received is ${utf8.decode(event)}');
completer.complete(utf8.decode(event));
},
socket.listen(
(event) {
LogManager.getInstance
.debug('Event received is ${utf8.decode(event)}');
Map<String, dynamic> eventMap = jsonDecode(utf8.decode(event));
if (eventMap.containsKey("error")) {
var error = eventMap["error"];
var exception = LNClientException(
error["code"], error["message"], error["data"]);
completer.completeError(exception);
} else {
completer.complete(utf8.decode(event));
}
},
onDone: () => LogManager.getInstance.debug('End JSON RPC Stream'),
onError: (err) => LogManager.getInstance.error(err));
onError: (error) {
LogManager.getInstance.error(error);
completer.completeError(error);
});
return completer.future;
}
}
42 changes: 29 additions & 13 deletions packages/rpc/test/clightning_rpc_test.dart
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
import 'dart:io';
import 'package:test/test.dart';
import 'package:clightning_rpc/clightning_rpc.dart';
import 'package:clightning_rpc/src/utils/exception/ln_client_error.dart';

void main() {
var env = Platform.environment;
var rpcPath = env['RPC_PATH']!;

group('A group of test to connect the unix socket', () {
setUp(() {});
var client = RPCClient();
client.connect(rpcPath);

test('Connection test', () {
var client = RPCClient();
client.connect(rpcPath);
expect(client, isNotNull);
client.close();
});
setUp(() {});

test('Call getinfo method test', () async {
var client = RPCClient();
client.connect(rpcPath);
expect(client, isNotNull);
var response = await client.simpleCall('getinfo');
expect(response['network'], 'regtest');
client.close();
});

test('Call invoice method test', () async {
var client = RPCClient();
client.connect(rpcPath);
test('Make a loop payment and looking for exception', () async {
expect(client, isNotNull);
// Docs of the parametes
// Docs of the parameters
// https://lightning.readthedocs.io/lightning-invoice.7.html
var params = <String, dynamic>{};
params['msatoshi'] = '100000msat';
Expand All @@ -43,5 +37,27 @@ void main() {
await client.simpleCall('delinvoice', params: params);
client.close();
});

test('Pay invoice method test', () async {
expect(client, isNotNull);

var params = <String, dynamic>{};
params['msatoshi'] = '100000msat';
params['label'] = 'from-dart-v2';
params['description'] = 'This is a unit test';

var response = await client.simpleCall('invoice', params: params);
params = <String, dynamic>{};
params['bolt11'] = response["bolt11"];

expect(() async => await client.simpleCall('pay', params: params),
throwsA(isA<LNClientException>()));

params = <String, dynamic>{};
params['label'] = 'from-dart-v2';
params['status'] = 'unpaid';
await client.simpleCall('delinvoice', params: params);
client.close();
});
});
}

0 comments on commit 8d2aaf8

Please sign in to comment.