From 652763b267a7d81c1f3aeb45f7582750872ee408 Mon Sep 17 00:00:00 2001 From: Vasek - Tom C Date: Wed, 20 Jul 2022 22:23:07 +0200 Subject: [PATCH] feat(server): ignore error in production (#58) Signed-off-by: Vasek - Tom C --- main.go | 2 +- server/handler/documentDefinition.go | 6 +++--- server/handler/documentDidOpen.go | 4 ++-- server/handler/documentDidSave.go | 6 +++--- server/handler/documentHover.go | 6 +++--- server/handler/documentSemanticTokensFull.go | 4 ++-- server/handler/error.go | 12 ++++++++++++ server/handler/handler.go | 12 +++++++++++- server/handler/initialize.go | 2 +- server/server.go | 13 ++++++++++--- 10 files changed, 48 insertions(+), 19 deletions(-) create mode 100644 server/handler/error.go diff --git a/main.go b/main.go index 1440147..c37f270 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,7 @@ import ( ) func main() { - s := server.New() + s := server.New(server.DEV) if err := s.Run(); err != nil { panic(err) diff --git a/server/handler/documentDefinition.go b/server/handler/documentDefinition.go index 974c8e0..1e6c3bd 100644 --- a/server/handler/documentDefinition.go +++ b/server/handler/documentDefinition.go @@ -20,12 +20,12 @@ func (h *Handler) documentDefinition(_ *glsp.Context, params *protocol.Definitio _uri, err := uri.Parse(params.TextDocument.URI) if err != nil { - return nil, err + return nil, h.wrapError(err) } p := h.workspace.GetPlan(_uri.Filename()) if p == nil { - return nil, fmt.Errorf("plan not found") + return nil, h.wrapError(fmt.Errorf("plan not found")) } h.log.Debugf("Pos {%x, %x}", params.Position.Line, params.Position.Character) @@ -36,7 +36,7 @@ func (h *Handler) documentDefinition(_ *glsp.Context, params *protocol.Definitio utils.UIntToInt(params.Position.Character), ) if err != nil { - return nil, err + return nil, h.wrapError(err) } h.log.Debugf("Position: %#v", location.Pos().Position()) diff --git a/server/handler/documentDidOpen.go b/server/handler/documentDidOpen.go index 2b23751..280f114 100644 --- a/server/handler/documentDidOpen.go +++ b/server/handler/documentDidOpen.go @@ -14,11 +14,11 @@ func (h *Handler) documentDidOpen(_ *glsp.Context, params *protocol.DidOpenTextD _uri, err := uri.Parse(params.TextDocument.URI) if err != nil { - return err + return h.wrapError(err) } if err := h.workspace.AddPlan(_uri.Filename()); err != nil { - return err + return h.wrapError(err) } return nil diff --git a/server/handler/documentDidSave.go b/server/handler/documentDidSave.go index 2278076..bf6d724 100644 --- a/server/handler/documentDidSave.go +++ b/server/handler/documentDidSave.go @@ -19,16 +19,16 @@ func (h *Handler) documentDidSave(context *glsp.Context, params *protocol.DidSav _uri, err := uri.Parse(params.TextDocument.URI) if err != nil { - return err + return h.wrapError(err) } p := h.workspace.GetPlan(_uri.Filename()) if p == nil { - return fmt.Errorf("plan not found") + return h.wrapError(fmt.Errorf("plan not found")) } if err := p.Reload(); err != nil { - return err + return h.wrapError(err) } return nil diff --git a/server/handler/documentHover.go b/server/handler/documentHover.go index 5abc8d7..bb03064 100644 --- a/server/handler/documentHover.go +++ b/server/handler/documentHover.go @@ -18,12 +18,12 @@ func (h *Handler) documentHover(_ *glsp.Context, params *protocol.HoverParams) ( _uri, err := uri.Parse(params.TextDocument.URI) if err != nil { - return nil, err + return nil, h.wrapError(err) } p := h.workspace.GetPlan(_uri.Filename()) if p == nil { - return nil, fmt.Errorf("plan not found") + return nil, h.wrapError(fmt.Errorf("plan not found")) } h.log.Debugf("Pos {%x, %x}", params.Position.Line, params.Position.Character) @@ -34,7 +34,7 @@ func (h *Handler) documentHover(_ *glsp.Context, params *protocol.HoverParams) ( utils.UIntToInt(params.Position.Character), ) if err != nil { - return nil, err + return nil, h.wrapError(err) } return &protocol.Hover{ diff --git a/server/handler/documentSemanticTokensFull.go b/server/handler/documentSemanticTokensFull.go index 1f01691..d39d3ae 100644 --- a/server/handler/documentSemanticTokensFull.go +++ b/server/handler/documentSemanticTokensFull.go @@ -9,13 +9,13 @@ import ( // documentDidOpen register a new plan in the workspace // Spec: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_didOpen -func (h *Handler) documentSemanticTokensFull(context *glsp.Context, params *protocol.SemanticTokensParams) (*protocol.SemanticTokens, error) { +func (h *Handler) documentSemanticTokensFull(_ *glsp.Context, params *protocol.SemanticTokensParams) (*protocol.SemanticTokens, error) { h.log.Debugf("Semantic Tokens: %s", params.TextDocument.URI) h.log.Debugf("params: %#v", params) _uri, err := uri.Parse(params.TextDocument.URI) if err != nil { - return nil, err + return nil, h.wrapError(err) } // TODO: Hightlight errors diff --git a/server/handler/error.go b/server/handler/error.go new file mode 100644 index 0000000..5a6cac2 --- /dev/null +++ b/server/handler/error.go @@ -0,0 +1,12 @@ +package handler + +// wrapError is a utility function to manage any error returned by a handler +// depending on a context +// For now, it ignores error on production mode to do not annoying users +func (h *Handler) wrapError(err error) error { + if h.mode == PROD { + return nil + } + + return err +} diff --git a/server/handler/handler.go b/server/handler/handler.go index a98bcb7..cca90b6 100644 --- a/server/handler/handler.go +++ b/server/handler/handler.go @@ -9,6 +9,13 @@ import ( "github.com/tliron/kutil/logging" ) +type Mode int + +const ( + DEV Mode = iota + PROD +) + // Handler is the storage for any handler of the server.LSP. // It also handles a single workspace for now which represent a VSCode project type Handler struct { @@ -21,15 +28,18 @@ type Handler struct { lsName string lsVersion string + + mode Mode } // New creates a Handler instance that contains all methods supported by // the LSP -func New(lsName, lsVersion string, log logging.Logger) *Handler { +func New(lsName, lsVersion string, log logging.Logger, mode Mode) *Handler { h := &Handler{ lsName: lsName, lsVersion: lsVersion, log: logging.NewScopeLogger(log, "workspace"), + mode: mode, } h.handler = &protocol.Handler{ diff --git a/server/handler/initialize.go b/server/handler/initialize.go index 0bf5531..ec95d88 100644 --- a/server/handler/initialize.go +++ b/server/handler/initialize.go @@ -22,7 +22,7 @@ func (h *Handler) initialize(_ *glsp.Context, params *protocol.InitializeParams) } if err := h.initWorkspace(params.WorkspaceFolders, params.RootURI, params.RootPath); err != nil { - return nil, err + return nil, h.wrapError(err) } return protocol.InitializeResult{ diff --git a/server/server.go b/server/server.go index 86fc79a..ec8c0b0 100644 --- a/server/server.go +++ b/server/server.go @@ -23,16 +23,23 @@ const ( Version = "0.0.1" ) +type Mode handler.Mode + +const ( + DEV Mode = iota + PROD +) + // New initializes a new language protocol server that contains his logger // and his handler -func New() *LSP { +func New(mode Mode) *LSP { // This increases logging verbosity (optional) // logTo := "/tmp/daggerlsp.log" // logging.Configure(2, &logTo) - logging.Configure(2, nil) + logging.Configure(0, nil) log := logging.GetLogger(Name) - h := handler.New(Name, Version, log) + h := handler.New(Name, Version, log, handler.Mode(mode)) return &LSP{ log: log, handler: h,