src/debugAdapter: adds error handling for remote connections

json-rpc2 module doesn't catch all the errors in its callback. If a remote connection fails to be established, it won't call the callback for errors. So the connection will hang. There are no future plans to handle the error case through the callback: json-rpc2: pocesar/node-jsonrpc2#53. Node.js handles errors in a similar way: https://nodejs.org/api/net.html#net_socket_connect. Errors won't be exposed through the callback, only through the error listener.

Fixes golang/vscode-go#215

Change-Id: I75cfb2a0cf6628a8c35ddccf8c96ec1ade0275f1
GitHub-Last-Rev: 44d9b592e068a1136a0c41c10fcdfab68f64fe0a
GitHub-Pull-Request: golang/vscode-go#216
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/237559
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/src/debugAdapter/goDebug.ts b/src/debugAdapter/goDebug.ts
index 62eafbc..915f00e 100644
--- a/src/debugAdapter/goDebug.ts
+++ b/src/debugAdapter/goDebug.ts
@@ -600,6 +600,7 @@
 						}
 						return resolve(conn);
 					});
+					client.on('error', reject);
 				}, 200);
 			}
 
diff --git a/typings/json-rpc2.d.ts b/typings/json-rpc2.d.ts
index aa02e73..3bee3d0 100644
--- a/typings/json-rpc2.d.ts
+++ b/typings/json-rpc2.d.ts
@@ -6,5 +6,6 @@
 	export class Client {
 		static $create(port: number, addr: string): Client;
 		connectSocket(callback: (err: Error, conn: RPCConnection) => void): void;
+		on(handler: 'error', callback: (err: Error) => void): void;
 	}
 }
\ No newline at end of file