Skip to content

Commit

Permalink
use x/xerrors to create new errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Steinke committed May 31, 2019
1 parent b3315ee commit 76f8316
Show file tree
Hide file tree
Showing 36 changed files with 115 additions and 102 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.11
require (
golang.org/x/net v0.0.0-20190311183353-d8887717615a
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEha
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522 h1:bhOzK9QyoD0ogCnFro1m2mz41+Ib0oOhfJnBp5MR4K4=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2 changes: 1 addition & 1 deletion internal/lsp/browser/README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This package is a copy of cmd/internal/browser from the go distribution
This package is a copy of cmd/internal/browser from the go distribution
6 changes: 3 additions & 3 deletions internal/lsp/cache/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cache

import (
"context"
"fmt"
"go/ast"
"go/scanner"
"go/token"
Expand All @@ -15,6 +14,7 @@ import (
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/internal/span"
"golang.org/x/xerrors"
)

type importer struct {
Expand All @@ -38,7 +38,7 @@ func (imp *importer) Import(pkgPath string) (*types.Package, error) {

func (imp *importer) getPkg(pkgPath string) (*pkg, error) {
if _, ok := imp.seen[pkgPath]; ok {
return nil, fmt.Errorf("circular import detected")
return nil, xerrors.Errorf("circular import detected")
}
imp.view.pcache.mu.Lock()
e, ok := imp.view.pcache.packages[pkgPath]
Expand Down Expand Up @@ -67,7 +67,7 @@ func (imp *importer) getPkg(pkgPath string) (*pkg, error) {
func (imp *importer) typeCheck(pkgPath string) (*pkg, error) {
meta, ok := imp.view.mcache.packages[pkgPath]
if !ok {
return nil, fmt.Errorf("no metadata for %v", pkgPath)
return nil, xerrors.Errorf("no metadata for %v", pkgPath)
}
// Use the default type information for the unsafe package.
var typ *types.Package
Expand Down
9 changes: 5 additions & 4 deletions internal/lsp/cache/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"golang.org/x/tools/go/packages"
"golang.org/x/tools/internal/span"
"golang.org/x/xerrors"
)

func (v *view) loadParseTypecheck(ctx context.Context, f *goFile) ([]packages.Error, error) {
Expand All @@ -29,7 +30,7 @@ func (v *view) loadParseTypecheck(ctx context.Context, f *goFile) ([]packages.Er
return errs, err
}
if f.meta == nil {
return nil, fmt.Errorf("no metadata found for %v", f.filename())
return nil, xerrors.Errorf("no metadata found for %v", f.filename())
}
imp := &importer{
view: v,
Expand All @@ -48,7 +49,7 @@ func (v *view) loadParseTypecheck(ctx context.Context, f *goFile) ([]packages.Er
}
// If we still have not found the package for the file, something is wrong.
if f.pkg == nil {
return nil, fmt.Errorf("parse: no package found for %v", f.filename())
return nil, xerrors.Errorf("parse: no package found for %v", f.filename())
}
return nil, nil
}
Expand All @@ -59,7 +60,7 @@ func (v *view) checkMetadata(ctx context.Context, f *goFile) ([]packages.Error,
pkgs, err := packages.Load(cfg, fmt.Sprintf("file=%s", f.filename()))
if len(pkgs) == 0 {
if err == nil {
err = fmt.Errorf("%s: no packages found", f.filename())
err = xerrors.Errorf("%s: no packages found", f.filename())
}
// Return this error as a diagnostic to the user.
return []packages.Error{
Expand All @@ -73,7 +74,7 @@ func (v *view) checkMetadata(ctx context.Context, f *goFile) ([]packages.Error,
// If the package comes back with errors from `go list`, don't bother
// type-checking it.
if len(pkg.Errors) > 0 {
return pkg.Errors, fmt.Errorf("package %s has errors, skipping type-checking", pkg.PkgPath)
return pkg.Errors, xerrors.Errorf("package %s has errors, skipping type-checking", pkg.PkgPath)
}
v.link(ctx, pkg.PkgPath, pkg, nil)
}
Expand Down
16 changes: 8 additions & 8 deletions internal/lsp/cache/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cache

import (
"context"
"fmt"
"go/ast"
"go/parser"
"go/scanner"
Expand All @@ -17,6 +16,7 @@ import (
"sync"

"golang.org/x/tools/internal/span"
"golang.org/x/xerrors"
)

func parseFile(fset *token.FileSet, filename string, src []byte) (*ast.File, error) {
Expand Down Expand Up @@ -54,7 +54,7 @@ func (imp *importer) parseFiles(filenames []string) ([]*ast.File, []error) {
}
gof, ok := f.(*goFile)
if !ok {
parsed[i], errors[i] = nil, fmt.Errorf("Non go file in parse call: %v", filename)
parsed[i], errors[i] = nil, xerrors.Errorf("Non go file in parse call: %v", filename)
continue
}

Expand All @@ -72,7 +72,7 @@ func (imp *importer) parseFiles(filenames []string) ([]*ast.File, []error) {
}
src := gof.fc.Data
if src == nil {
parsed[i], errors[i] = nil, fmt.Errorf("No source for %v", filename)
parsed[i], errors[i] = nil, xerrors.Errorf("No source for %v", filename)
} else {
// ParseFile may return both an AST and an error.
parsed[i], errors[i] = parseFile(imp.fset, filename, src)
Expand Down Expand Up @@ -175,7 +175,7 @@ func (v *view) parseDeferOrGoStmt(bad *ast.BadStmt, parent ast.Node, tok *token.
var lit string
for {
if tkn == token.EOF {
return fmt.Errorf("reached the end of the file")
return xerrors.Errorf("reached the end of the file")
}
if pos >= bad.From {
break
Expand All @@ -193,7 +193,7 @@ func (v *view) parseDeferOrGoStmt(bad *ast.BadStmt, parent ast.Node, tok *token.
Go: pos,
}
default:
return fmt.Errorf("no defer or go statement found")
return xerrors.Errorf("no defer or go statement found")
}

// The expression after the "defer" or "go" starts at this position.
Expand All @@ -218,15 +218,15 @@ FindTo:
to = curr
}
if !from.IsValid() || tok.Offset(from) >= len(src) {
return fmt.Errorf("invalid from position")
return xerrors.Errorf("invalid from position")
}
if !to.IsValid() || tok.Offset(to)+1 >= len(src) {
return fmt.Errorf("invalid to position")
return xerrors.Errorf("invalid to position")
}
exprstr := string(src[tok.Offset(from) : tok.Offset(to)+1])
expr, err := parser.ParseExpr(exprstr)
if expr == nil {
return fmt.Errorf("no expr in %s: %v", exprstr, err)
return xerrors.Errorf("no expr in %s: %v", exprstr, err)
}
// parser.ParseExpr returns undefined positions.
// Adjust them for the current file.
Expand Down
4 changes: 2 additions & 2 deletions internal/lsp/cache/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ package cache

import (
"context"
"fmt"
"os"
"strings"
"sync"

"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/lsp/xlog"
"golang.org/x/tools/internal/span"
"golang.org/x/xerrors"
)

type session struct {
Expand Down Expand Up @@ -150,7 +150,7 @@ func (s *session) removeView(ctx context.Context, view *view) error {
return nil
}
}
return fmt.Errorf("view %s for %v not found", view.Name(), view.Folder())
return xerrors.Errorf("view %s for %v not found", view.Name(), view.Folder())
}

func (s *session) Logger() xlog.Logger {
Expand Down
4 changes: 2 additions & 2 deletions internal/lsp/cache/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cache

import (
"context"
"fmt"
"go/ast"
"go/parser"
"go/types"
Expand All @@ -17,6 +16,7 @@ import (
"golang.org/x/tools/go/packages"
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/span"
"golang.org/x/xerrors"
)

type view struct {
Expand Down Expand Up @@ -340,7 +340,7 @@ func (v *view) getFile(uri span.URI) (viewFile, error) {
},
}
default:
return nil, fmt.Errorf("unsupported file extension: %s", ext)
return nil, xerrors.Errorf("unsupported file extension: %s", ext)
}
v.mapFile(uri, f)
return f, nil
Expand Down
5 changes: 3 additions & 2 deletions internal/lsp/cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"golang.org/x/tools/internal/span"
"golang.org/x/xerrors"
)

// check implements the check verb for gopls.
Expand Down Expand Up @@ -60,14 +61,14 @@ func (c *check) Run(ctx context.Context, args ...string) error {
select {
case <-file.hasDiagnostics:
case <-time.Tick(30 * time.Second):
return fmt.Errorf("timed out waiting for results from %v", file.uri)
return xerrors.Errorf("timed out waiting for results from %v", file.uri)
}
file.diagnosticsMu.Lock()
defer file.diagnosticsMu.Unlock()
for _, d := range file.diagnostics {
spn, err := file.mapper.RangeSpan(d.Range)
if err != nil {
return fmt.Errorf("Could not convert position %v for %q", d.Range, d.Message)
return xerrors.Errorf("Could not convert position %v for %q", d.Range, d.Message)
}
fmt.Printf("%v: %v\n", spn, d.Message)
}
Expand Down
7 changes: 4 additions & 3 deletions internal/lsp/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/span"
"golang.org/x/tools/internal/tool"
"golang.org/x/xerrors"
)

// Application is the main application as passed to tool.Main
Expand Down Expand Up @@ -320,12 +321,12 @@ func (c *cmdClient) getFile(ctx context.Context, uri span.URI) *cmdFile {
if file.mapper == nil {
fname, err := uri.Filename()
if err != nil {
file.err = fmt.Errorf("%v: %v", uri, err)
file.err = xerrors.Errorf("%v: %v", uri, err)
return file
}
content, err := ioutil.ReadFile(fname)
if err != nil {
file.err = fmt.Errorf("%v: %v", uri, err)
file.err = xerrors.Errorf("%v: %v", uri, err)
return file
}
f := c.fset.AddFile(fname, -1, len(content))
Expand All @@ -345,7 +346,7 @@ func (c *connection) AddFile(ctx context.Context, uri span.URI) *cmdFile {
p.TextDocument.URI = string(uri)
p.TextDocument.Text = string(file.mapper.Content)
if err := c.Server.DidOpen(ctx, p); err != nil {
file.err = fmt.Errorf("%v: %v", uri, err)
file.err = xerrors.Errorf("%v: %v", uri, err)
}
}
return file
Expand Down
17 changes: 9 additions & 8 deletions internal/lsp/cmd/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/span"
"golang.org/x/tools/internal/tool"
"golang.org/x/xerrors"
)

// A Definition is the result of a 'definition' query.
Expand Down Expand Up @@ -79,26 +80,26 @@ func (d *definition) Run(ctx context.Context, args ...string) error {
}
locs, err := conn.Definition(ctx, &p)
if err != nil {
return fmt.Errorf("%v: %v", from, err)
return xerrors.Errorf("%v: %v", from, err)
}

if len(locs) == 0 {
return fmt.Errorf("%v: not an identifier", from)
return xerrors.Errorf("%v: not an identifier", from)
}
hover, err := conn.Hover(ctx, &p)
if err != nil {
return fmt.Errorf("%v: %v", from, err)
return xerrors.Errorf("%v: %v", from, err)
}
if hover == nil {
return fmt.Errorf("%v: not an identifier", from)
return xerrors.Errorf("%v: not an identifier", from)
}
file = conn.AddFile(ctx, span.NewURI(locs[0].URI))
if file.err != nil {
return fmt.Errorf("%v: %v", from, file.err)
return xerrors.Errorf("%v: %v", from, file.err)
}
definition, err := file.mapper.Span(locs[0])
if err != nil {
return fmt.Errorf("%v: %v", from, err)
return xerrors.Errorf("%v: %v", from, err)
}
description := strings.TrimSpace(hover.Contents.Value)
var result interface{}
Expand All @@ -115,7 +116,7 @@ func (d *definition) Run(ctx context.Context, args ...string) error {
Desc: description,
}
default:
return fmt.Errorf("unknown emulation for definition: %s", d.query.Emulate)
return xerrors.Errorf("unknown emulation for definition: %s", d.query.Emulate)
}
if err != nil {
return err
Expand All @@ -131,7 +132,7 @@ func (d *definition) Run(ctx context.Context, args ...string) error {
case *guru.Definition:
fmt.Printf("%s: defined here as %s", d.ObjPos, d.Desc)
default:
return fmt.Errorf("no printer for type %T", result)
return xerrors.Errorf("no printer for type %T", result)
}
return nil
}
7 changes: 4 additions & 3 deletions internal/lsp/cmd/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/span"
"golang.org/x/xerrors"
)

// format implements the format verb for gopls.
Expand Down Expand Up @@ -68,18 +69,18 @@ func (f *format) Run(ctx context.Context, args ...string) error {
return err
}
if loc.Range.Start != loc.Range.End {
return fmt.Errorf("only full file formatting supported")
return xerrors.Errorf("only full file formatting supported")
}
p := protocol.DocumentFormattingParams{
TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
}
edits, err := conn.Formatting(ctx, &p)
if err != nil {
return fmt.Errorf("%v: %v", spn, err)
return xerrors.Errorf("%v: %v", spn, err)
}
sedits, err := lsp.FromProtocolEdits(file.mapper, edits)
if err != nil {
return fmt.Errorf("%v: %v", spn, err)
return xerrors.Errorf("%v: %v", spn, err)
}
ops := source.EditsToDiff(sedits)
lines := diff.SplitLines(string(file.mapper.Content))
Expand Down
3 changes: 2 additions & 1 deletion internal/lsp/cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"golang.org/x/tools/internal/lsp"
"golang.org/x/tools/internal/lsp/debug"
"golang.org/x/tools/internal/tool"
"golang.org/x/xerrors"
)

// Serve is a struct that exposes the configurable parts of the LSP server as
Expand Down Expand Up @@ -65,7 +66,7 @@ func (s *Serve) Run(ctx context.Context, args ...string) error {
}
f, err := os.Create(filename)
if err != nil {
return fmt.Errorf("Unable to create log file: %v", err)
return xerrors.Errorf("Unable to create log file: %v", err)
}
defer f.Close()
log.SetOutput(io.MultiWriter(os.Stderr, f))
Expand Down
Loading

0 comments on commit 76f8316

Please sign in to comment.