internal/lsp/protocol: ignore reply values with non-nil errors in jsonrpc2_v2 adapters

The documentation for jsonrpc2.Replier states that
“[i]f err is set then result will be ignored.”

jsonrpc2_v2 documents no such affordance, and the JSON-RPC 2.0
protocol explicitly requires that the result “MUST NOT exist if there
was an error invoking the method.”

Although CL 388600 already avoids returning values in case of error
(which may also help with escape analysis and/or allocation
efficiency), it seems simplest and least confusing to make the
semantic difference between the jsonrpc2.Handler and
jsonrpc2_v2.Handler explicit in the code.

For golang/go#46520.

Change-Id: If13eb842505d42cbc51c01f5f5e699a549a3a28b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/400054
Run-TryBot: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
diff --git a/internal/lsp/protocol/protocol.go b/internal/lsp/protocol/protocol.go
index a8b3354..a7b156a 100644
--- a/internal/lsp/protocol/protocol.go
+++ b/internal/lsp/protocol/protocol.go
@@ -134,7 +134,11 @@
 			resErr error
 		)
 		replier := func(_ context.Context, res interface{}, err error) error {
-			result, resErr = res, err
+			if err != nil {
+				resErr = err
+				return nil
+			}
+			result = res
 			return nil
 		}
 		_, err := clientDispatch(ctx, client, replier, req1)
@@ -179,7 +183,11 @@
 			resErr error
 		)
 		replier := func(_ context.Context, res interface{}, err error) error {
-			result, resErr = res, err
+			if err != nil {
+				resErr = err
+				return nil
+			}
+			result = res
 			return nil
 		}
 		_, err := serverDispatch(ctx, server, replier, req1)