diff --git a/internal/filesystem/errors.go b/internal/filesystem/errors.go index 13053312a..f954f2c82 100644 --- a/internal/filesystem/errors.go +++ b/internal/filesystem/errors.go @@ -22,3 +22,19 @@ type InvalidPosErr struct { func (e *InvalidPosErr) Error() string { return fmt.Sprintf("invalid position: %s", e.Pos) } + +type InvalidURIErr struct { + URI URI +} + +func (e *InvalidURIErr) Error() string { + return fmt.Sprintf("invalid URI: %s", e.URI) +} + +type FileNotOpenErr struct { + URI URI +} + +func (e *FileNotOpenErr) Error() string { + return fmt.Sprintf("file is not open: %s", e.URI) +} diff --git a/internal/filesystem/filesystem.go b/internal/filesystem/filesystem.go index 6abba1938..78d905252 100644 --- a/internal/filesystem/filesystem.go +++ b/internal/filesystem/filesystem.go @@ -1,7 +1,6 @@ package filesystem import ( - "fmt" "io/ioutil" "log" "sync" @@ -44,7 +43,7 @@ func (fs *fsystem) Open(doc lsp.TextDocumentItem) error { s := []byte(doc.Text) if !u.Valid() { - return fmt.Errorf("invalid URL to open") + return &InvalidURIErr{URI: u} } fullName, dn, fn := u.PathParts() @@ -70,7 +69,7 @@ func (fs *fsystem) Change(doc lsp.VersionedTextDocumentIdentifier, changes []lsp f := fs.file(u) if f == nil || !f.open { - return fmt.Errorf("file %q is not open", u) + return &FileNotOpenErr{u} } for _, change := range changes { f.applyChange(change) @@ -86,7 +85,7 @@ func (fs *fsystem) Close(doc lsp.TextDocumentIdentifier) error { f := fs.file(u) if f == nil || !f.open { - return fmt.Errorf("file %q is not open", u) + return &FileNotOpenErr{u} } _, dn, fn := u.PathParts() delete(fs.dirs[dn].files, fn) @@ -101,7 +100,7 @@ func (fs *fsystem) HclBlockAtDocPosition(params lsp.TextDocumentPositionParams) u := fs.URI(params.TextDocument.URI) f := fs.file(u) if f == nil || !f.open { - return nil, hcl.Pos{}, fmt.Errorf("file %q is not open", u) + return nil, hcl.Pos{}, &FileNotOpenErr{u} } fs.logger.Printf("Converting LSP position %#v into HCL", params.Position)