src/goDebugFactory.ts: send the first error when starting dlv-dap

For every message that the thin adapter needs to forward to dlv-dap,
the thin adapter needs to make sure it can connect to the server. If
there was an error connecting to the server it will send an error
message and a terminated event so the client knows the debug session
is not active. If multiple messages are sent to the server, the user
gets shown multiple error messages. This change records whether we
have already sent a terminated event because of an error, and will
not send duplicate error messages.

Fixes golang/vscode-go#1426

Change-Id: I8b81959b529f3802c9aa2cbebdbe8008af5902b3
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/310517
Trust: Suzy Mueller <suzmue@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/src/goDebugFactory.ts b/src/goDebugFactory.ts
index 47c79be..656de92 100644
--- a/src/goDebugFactory.ts
+++ b/src/goDebugFactory.ts
@@ -168,6 +168,7 @@
 	private dlvDapServer: ChildProcess;
 	private port: number;
 	private socket: net.Socket;
+	private terminatedOnError = false;
 
 	protected async sendMessageToServer(message: vscode.DebugProtocolMessage): Promise<void> {
 		if (!this.connected) {
@@ -177,14 +178,19 @@
 			await this.connected;
 			super.sendMessageToServer(message);
 		} catch (err) {
+			if (this.terminatedOnError) {
+				return;
+			}
+			this.terminatedOnError = true;
 			// If there was an error connecting, show an error message
 			// and send a terminated event, since we cannot start.
 			if (err) {
-				const errMsg = `connect to server error: ${err}`;
-				this.sendMessageToClient(new OutputEvent(errMsg));
+				const errMsg = `Debug Error: ${err}`;
+				this.outputEvent(errMsg, 'stderr');
 				vscode.window.showErrorMessage(errMsg);
 			}
 			this.sendMessageToClient(new TerminatedEvent());
+			return;
 		}
 	}