Skip to content

Commit

Permalink
Throw a useful error if there is a problem communicating with the pro…
Browse files Browse the repository at this point in the history
…tocol
  • Loading branch information
kevmoo committed Jun 2, 2015
1 parent 0d8d903 commit 255484e
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions lib/src/devtools.dart
Expand Up @@ -174,6 +174,18 @@ class _VMWebsocketConnection {
return;
}

var completer = _pendingRequests.remove(id);
if (completer == null) {
_log.severe('Failed to pair response with request');
}

var error = json['error'];
if (error != null) {
var errorObj = new JsonRpcError.fromJson(error);
completer.completeError(errorObj);
return;
}

var innerResponse = json['result'];
if (innerResponse == null) {
// Support for 1.9.0 <= vm version < 1.10.0.
Expand All @@ -191,10 +203,38 @@ class _VMWebsocketConnection {
message = JSON.decode(innerResponse);
}
}
var completer = _pendingRequests.remove(id);
if (completer == null) {
_log.severe('Failed to pair response with request');
}
completer.complete(message);
}
}

class JsonRpcError extends Error {
final int code;
final String message;
final data;

// http://www.jsonrpc.org/specification
// -32601 Method not found The method does not exist / is not available.
bool get isMethodNotFound => code == -32601;

JsonRpcError(this.code, this.message, this.data);

factory JsonRpcError.fromJson(Map<String, dynamic> json) =>
new JsonRpcError(json['code'], json['message'], json['data']);

String toString() {
var msg = 'JsonRpcError: $message';
if (isMethodNotFound) {
if (data is Map) {
var request = data['request'];
if (request is Map) {
var method = request['method'];
if (method != null) {
msg = '$msg - "$method"';
}
}
}
}

return '$msg ($code)';
}
}

0 comments on commit 255484e

Please sign in to comment.