1
0
mirror of https://github.com/golang/go synced 2024-10-01 08:28:43 -06:00
go/internal/lsp/protocol/context.go
Rebecca Stambler a7c0594f4e internal/lsp: avoid logging context cancellation
This change adds a helper function that checks if the context is
canceled, and if so, doesn't log the error. Tried to use it everywhere
in internal/lsp where it fits, which resulted in changing a few pieces
of error handling throughout.

Updates golang/go#37875

Change-Id: I59cbc6f893e3b70cf84524d9944ff7f4b4febd78
Reviewed-on: https://go-review.googlesource.com/c/tools/+/226371
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-03-31 01:46:33 +00:00

39 lines
734 B
Go

package protocol
import (
"context"
"fmt"
"golang.org/x/tools/internal/telemetry/event"
"golang.org/x/tools/internal/xcontext"
)
type contextKey int
const (
clientKey = contextKey(iota)
)
func WithClient(ctx context.Context, client Client) context.Context {
return context.WithValue(ctx, clientKey, client)
}
func LogEvent(ctx context.Context, ev event.Event, tags event.TagMap) context.Context {
if ctx.Err() != nil {
return ctx
}
if !ev.IsLog() {
return ctx
}
client, ok := ctx.Value(clientKey).(Client)
if !ok {
return ctx
}
msg := &LogMessageParams{Type: Info, Message: fmt.Sprint(ev)}
if event.Err.Get(tags) != nil {
msg.Type = Error
}
go client.LogMessage(xcontext.Detach(ctx), msg)
return ctx
}