microsoft/typescript-go · error · ErrorCode

-32602

-32602

Error message

%w: %w

What it means

In lspReader.Read (server.go:126-141), an inbound message failed JSON decoding with an error that errors.Is-matches ErrorCodeInvalidParams — i.e. UnmarshalParams already rejected the payload (NoParams violation, non-object params, bad nested fields). The reader re-wraps it as ErrorCodeInvalidParams (-32602) and, notably, still returns the partially populated *lsproto.Message so the dispatcher can send a per-request error response instead of killing the connection.

Source

Thrown at internal/lsp/server.go:135

	err error
}

func (e *messageMarshalError) Error() string { return "failed to marshal message: " + e.err.Error() }

func (e *messageMarshalError) Unwrap() []error {
	return []error{lsproto.ErrorCodeInternalError, e.err}
}

func (r *lspReader) Read() (*lsproto.Message, error) {
	data, err := r.r.Read()
	if err != nil {
		return nil, err
	}

	req := &lsproto.Message{}
	if err := json.Unmarshal(data, req); err != nil {
		if errors.Is(err, lsproto.ErrorCodeInvalidParams) {
			return req, fmt.Errorf("%w: %w", lsproto.ErrorCodeInvalidParams, err)
		}
		return nil, fmt.Errorf("%w: %w", lsproto.ErrorCodeInvalidRequest, err)
	}

	return req, nil
}

func ToReader(r io.Reader) Reader {
	return &lspReader{r: lsproto.NewBaseReader(r)}
}

func (w *lspWriter) Write(msg *lsproto.Message) error {
	data, err := json.Marshal(msg)
	if err != nil {
		return &messageMarshalError{err: err}
	}
	return w.w.Write(data)
}

View on GitHub (pinned to 1bcfa18d79)

Solutions

  1. Inspect the wrapped cause after '-32602:' — it is one of the UnmarshalParams errors (expected no params / must be an object or array / nested field detail); fix the client payload accordingly.
  2. Keep the connection alive: this error path is recoverable per-request; ensure middleware does not treat it as fatal or close the stream.
  3. When testing, use errors.Is(err, lsproto.ErrorCodeInvalidParams) to assert the classification rather than parsing message text.

Example fix

// asserting classification in a Go test
// before
if err == nil { t.Fatal("expected error") }
// after
if !errors.Is(err, lsproto.ErrorCodeInvalidParams) { t.Fatalf("want InvalidParams, got %v", err) }
Defensive patterns

Strategy: try-catch

Type guard

func isInvalidParams(err error) bool {
    return errors.Is(err, lsproto.ErrorCodeInvalidParams)
}

Try / catch

msg, err := reader.Read()
if err != nil {
    if errors.Is(err, lsproto.ErrorCodeInvalidParams) {
        // msg is non-nil: respond per-request with -32602 and keep the connection
        sendError(msg.Request != nil && msg.Request.ID != nil, err)
        continue
    }
    return err // transport-level failure; stop
}

Prevention

When it happens

Trigger: Any inbound request carrying malformed params per UnmarshalParams: params on a NoParams method, scalar/null params, or object params failing nested validation — the error surfaces here when Message.UnmarshalJSON eagerly decodes params for known methods.

Common situations: Malformed client requests in production logs; debugging why a particular request got a -32602 response; test harnesses asserting the server responds Invalid params rather than Invalid request for shape-valid JSON with bad params.

Related errors


AI-assisted analysis of microsoft/typescript-go@1bcfa18d79 (2026-08-16). Data as JSON: /api/errors/83d6385c49c59708. Report an issue: GitHub.