Skip to content

Commit

Permalink
filesystem: Replace string errors with custom types
Browse files Browse the repository at this point in the history
This should make comparison easier in tests and elsewhere
  • Loading branch information
radeksimko committed Mar 9, 2020
1 parent 087fadd commit 4f1c1c4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
16 changes: 16 additions & 0 deletions internal/filesystem/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
9 changes: 4 additions & 5 deletions internal/filesystem/filesystem.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package filesystem

import (
"fmt"
"io/ioutil"
"log"
"sync"
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 4f1c1c4

Please sign in to comment.