Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
feat(server): ignore error in production (#58)
Browse files Browse the repository at this point in the history
Signed-off-by: Vasek - Tom C <[email protected]>
  • Loading branch information
TomChv authored Jul 20, 2022
1 parent 163133d commit 652763b
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 19 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func main() {
s := server.New()
s := server.New(server.DEV)

if err := s.Run(); err != nil {
panic(err)
Expand Down
6 changes: 3 additions & 3 deletions server/handler/documentDefinition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions server/handler/documentDidOpen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions server/handler/documentDidSave.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions server/handler/documentHover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions server/handler/documentSemanticTokensFull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions server/handler/error.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 11 additions & 1 deletion server/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion server/handler/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
13 changes: 10 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 652763b

Please sign in to comment.