Skip to content

Commit 2fe67fd

Browse files
author
Jan Steinke
committed
use x/xerrors to create new errors
1 parent 72ffa07 commit 2fe67fd

24 files changed

+112
-101
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ go 1.11
55
require (
66
golang.org/x/net v0.0.0-20190620200207-3b0461eec859
77
golang.org/x/sync v0.0.0-20190423024810-112230192c58
8+
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522
89
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEha
55
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
66
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
77
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
8+
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522 h1:bhOzK9QyoD0ogCnFro1m2mz41+Ib0oOhfJnBp5MR4K4=
9+
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

internal/lsp/cache/check.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package cache
66

77
import (
88
"context"
9-
"fmt"
109
"go/ast"
1110
"go/scanner"
1211
"go/token"
@@ -18,6 +17,7 @@ import (
1817
"golang.org/x/tools/internal/lsp/source"
1918
"golang.org/x/tools/internal/lsp/telemetry/trace"
2019
"golang.org/x/tools/internal/span"
20+
"golang.org/x/xerrors"
2121
)
2222

2323
type importer struct {
@@ -38,7 +38,7 @@ func (imp *importer) Import(pkgPath string) (*types.Package, error) {
3838
ctx := imp.ctx
3939
id, ok := imp.view.mcache.ids[packagePath(pkgPath)]
4040
if !ok {
41-
return nil, fmt.Errorf("no known ID for %s", pkgPath)
41+
return nil, xerrors.Errorf("no known ID for %s", pkgPath)
4242
}
4343
pkg, err := imp.getPkg(ctx, id)
4444
if err != nil {
@@ -49,7 +49,7 @@ func (imp *importer) Import(pkgPath string) (*types.Package, error) {
4949

5050
func (imp *importer) getPkg(ctx context.Context, id packageID) (*pkg, error) {
5151
if _, ok := imp.seen[id]; ok {
52-
return nil, fmt.Errorf("circular import detected")
52+
return nil, xerrors.Errorf("circular import detected")
5353
}
5454
imp.view.pcache.mu.Lock()
5555
e, ok := imp.view.pcache.packages[id]
@@ -93,7 +93,7 @@ func (imp *importer) typeCheck(ctx context.Context, id packageID) (*pkg, error)
9393
defer ts.End()
9494
meta, ok := imp.view.mcache.packages[id]
9595
if !ok {
96-
return nil, fmt.Errorf("no metadata for %v", id)
96+
return nil, xerrors.Errorf("no metadata for %v", id)
9797
}
9898
pkg := &pkg{
9999
id: meta.id,
@@ -160,7 +160,7 @@ func (imp *importer) typeCheck(ctx context.Context, id packageID) (*pkg, error)
160160
if meta.pkgPath == "unsafe" {
161161
pkg.types = types.Unsafe
162162
} else if len(files) == 0 { // not the unsafe package, no parsed files
163-
return nil, fmt.Errorf("no parsed files for package %s", pkg.pkgPath)
163+
return nil, xerrors.Errorf("no parsed files for package %s", pkg.pkgPath)
164164
} else {
165165
pkg.types = types.NewPackage(string(meta.pkgPath), meta.name)
166166
}
@@ -202,14 +202,14 @@ func (imp *importer) cachePackage(ctx context.Context, pkg *pkg, meta *metadata,
202202
for _, file := range pkg.files {
203203
f, err := imp.view.getFile(ctx, file.uri)
204204
if err != nil {
205-
return fmt.Errorf("no such file %s: %v", file.uri, err)
205+
return xerrors.Errorf("no such file %s: %v", file.uri, err)
206206
}
207207
gof, ok := f.(*goFile)
208208
if !ok {
209-
return fmt.Errorf("non Go file %s", file.uri)
209+
return xerrors.Errorf("non Go file %s", file.uri)
210210
}
211211
if err := imp.cachePerFile(gof, file, pkg); err != nil {
212-
return fmt.Errorf("failed to cache file %s: %v", gof.URI(), err)
212+
return xerrors.Errorf("failed to cache file %s: %v", gof.URI(), err)
213213
}
214214
}
215215

@@ -240,19 +240,19 @@ func (imp *importer) cachePerFile(gof *goFile, file *astFile, p *pkg) error {
240240
// Get the AST for the file.
241241
gof.ast = file
242242
if gof.ast == nil {
243-
return fmt.Errorf("no AST information for %s", file.uri)
243+
return xerrors.Errorf("no AST information for %s", file.uri)
244244
}
245245
if gof.ast.file == nil {
246-
return fmt.Errorf("no AST for %s", file.uri)
246+
return xerrors.Errorf("no AST for %s", file.uri)
247247
}
248248
// Get the *token.File directly from the AST.
249249
pos := gof.ast.file.Pos()
250250
if !pos.IsValid() {
251-
return fmt.Errorf("AST for %s has an invalid position", file.uri)
251+
return xerrors.Errorf("AST for %s has an invalid position", file.uri)
252252
}
253253
tok := imp.view.session.cache.FileSet().File(pos)
254254
if tok == nil {
255-
return fmt.Errorf("no *token.File for %s", file.uri)
255+
return xerrors.Errorf("no *token.File for %s", file.uri)
256256
}
257257
gof.token = tok
258258
gof.imports = gof.ast.file.Imports

internal/lsp/cache/load.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"golang.org/x/tools/go/packages"
1212
"golang.org/x/tools/internal/lsp/source"
1313
"golang.org/x/tools/internal/span"
14+
"golang.org/x/xerrors"
1415
)
1516

1617
func (v *view) loadParseTypecheck(ctx context.Context, f *goFile) ([]packages.Error, error) {
@@ -51,11 +52,11 @@ func (v *view) loadParseTypecheck(ctx context.Context, f *goFile) ([]packages.Er
5152
return nil, err
5253
}
5354
if pkg == nil || pkg.IsIllTyped() {
54-
return nil, fmt.Errorf("loadParseTypecheck: %s is ill typed", m.pkgPath)
55+
return nil, xerrors.Errorf("loadParseTypecheck: %s is ill typed", m.pkgPath)
5556
}
5657
}
5758
if len(f.pkgs) == 0 {
58-
return nil, fmt.Errorf("no packages found for %s", f.URI())
59+
return nil, xerrors.Errorf("no packages found for %s", f.URI())
5960
}
6061
return nil, nil
6162
}
@@ -87,7 +88,7 @@ func (v *view) checkMetadata(ctx context.Context, f *goFile) (map[packageID]*met
8788
pkgs, err := packages.Load(v.Config(), fmt.Sprintf("file=%s", f.filename()))
8889
if len(pkgs) == 0 {
8990
if err == nil {
90-
err = fmt.Errorf("go/packages.Load: no packages found for %s", f.filename())
91+
err = xerrors.Errorf("go/packages.Load: no packages found for %s", f.filename())
9192
}
9293
// Return this error as a diagnostic to the user.
9394
return nil, []packages.Error{
@@ -103,7 +104,7 @@ func (v *view) checkMetadata(ctx context.Context, f *goFile) (map[packageID]*met
103104
// If the package comes back with errors from `go list`,
104105
// don't bother type-checking it.
105106
if len(pkg.Errors) > 0 {
106-
return nil, pkg.Errors, fmt.Errorf("package %s has errors, skipping type-checking", pkg.PkgPath)
107+
return nil, pkg.Errors, xerrors.Errorf("package %s has errors, skipping type-checking", pkg.PkgPath)
107108
}
108109
// Build the import graph for this package.
109110
if err := v.link(ctx, packagePath(pkg.PkgPath), pkg, nil, missingImports); err != nil {
@@ -123,7 +124,7 @@ func validateMetadata(ctx context.Context, missingImports map[packagePath]struct
123124

124125
// If `go list` failed to get data for the file in question (this should never happen).
125126
if len(f.meta) == 0 {
126-
return nil, fmt.Errorf("loadParseTypecheck: no metadata found for %v", f.filename())
127+
return nil, xerrors.Errorf("loadParseTypecheck: no metadata found for %v", f.filename())
127128
}
128129

129130
// If we have already seen these missing imports before, and we have type information,
@@ -235,7 +236,7 @@ func (v *view) link(ctx context.Context, pkgPath packagePath, pkg *packages.Pack
235236
for importPath, importPkg := range pkg.Imports {
236237
importPkgPath := packagePath(importPath)
237238
if importPkgPath == pkgPath {
238-
return fmt.Errorf("cycle detected in %s", importPath)
239+
return xerrors.Errorf("cycle detected in %s", importPath)
239240
}
240241
// Don't remember any imports with significant errors.
241242
if importPkgPath != "unsafe" && len(pkg.CompiledGoFiles) == 0 {

internal/lsp/cache/parse.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package cache
66

77
import (
88
"context"
9-
"fmt"
109
"go/ast"
1110
"go/parser"
1211
"go/scanner"
@@ -15,6 +14,7 @@ import (
1514
"golang.org/x/tools/internal/lsp/source"
1615
"golang.org/x/tools/internal/lsp/telemetry/trace"
1716
"golang.org/x/tools/internal/memoize"
17+
"golang.org/x/xerrors"
1818
)
1919

2020
// Limits the number of parallel parser calls per process.
@@ -153,7 +153,7 @@ func fix(ctx context.Context, file *ast.File, tok *token.File, src []byte) error
153153
case *ast.BadStmt:
154154
err = parseDeferOrGoStmt(n, parent, tok, src) // don't shadow err
155155
if err != nil {
156-
err = fmt.Errorf("unable to parse defer or go from *ast.BadStmt: %v", err)
156+
err = xerrors.Errorf("unable to parse defer or go from *ast.BadStmt: %v", err)
157157
}
158158
return false
159159
default:
@@ -181,7 +181,7 @@ func parseDeferOrGoStmt(bad *ast.BadStmt, parent ast.Node, tok *token.File, src
181181
var lit string
182182
for {
183183
if tkn == token.EOF {
184-
return fmt.Errorf("reached the end of the file")
184+
return xerrors.Errorf("reached the end of the file")
185185
}
186186
if pos >= bad.From {
187187
break
@@ -199,7 +199,7 @@ func parseDeferOrGoStmt(bad *ast.BadStmt, parent ast.Node, tok *token.File, src
199199
Go: pos,
200200
}
201201
default:
202-
return fmt.Errorf("no defer or go statement found")
202+
return xerrors.Errorf("no defer or go statement found")
203203
}
204204

205205
// The expression after the "defer" or "go" starts at this position.
@@ -224,15 +224,15 @@ FindTo:
224224
to = curr
225225
}
226226
if !from.IsValid() || tok.Offset(from) >= len(src) {
227-
return fmt.Errorf("invalid from position")
227+
return xerrors.Errorf("invalid from position")
228228
}
229229
if !to.IsValid() || tok.Offset(to)+1 >= len(src) {
230-
return fmt.Errorf("invalid to position")
230+
return xerrors.Errorf("invalid to position")
231231
}
232232
exprstr := string(src[tok.Offset(from) : tok.Offset(to)+1])
233233
expr, err := parser.ParseExpr(exprstr)
234234
if expr == nil {
235-
return fmt.Errorf("no expr in %s: %v", exprstr, err)
235+
return xerrors.Errorf("no expr in %s: %v", exprstr, err)
236236
}
237237
// parser.ParseExpr returns undefined positions.
238238
// Adjust them for the current file.

internal/lsp/cache/session.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package cache
66

77
import (
88
"context"
9-
"fmt"
109
"os"
1110
"sort"
1211
"strconv"
@@ -18,6 +17,7 @@ import (
1817
"golang.org/x/tools/internal/lsp/source"
1918
"golang.org/x/tools/internal/lsp/xlog"
2019
"golang.org/x/tools/internal/span"
20+
"golang.org/x/xerrors"
2121
)
2222

2323
type session struct {
@@ -175,7 +175,7 @@ func (s *session) removeView(ctx context.Context, view *view) error {
175175
return nil
176176
}
177177
}
178-
return fmt.Errorf("view %s for %v not found", view.Name(), view.Folder())
178+
return xerrors.Errorf("view %s for %v not found", view.Name(), view.Folder())
179179
}
180180

181181
func (s *session) Logger() xlog.Logger {

internal/lsp/cmd/check.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"golang.org/x/tools/internal/span"
14+
"golang.org/x/xerrors"
1415
)
1516

1617
// check implements the check verb for gopls.
@@ -60,14 +61,14 @@ func (c *check) Run(ctx context.Context, args ...string) error {
6061
select {
6162
case <-file.hasDiagnostics:
6263
case <-time.After(30 * time.Second):
63-
return fmt.Errorf("timed out waiting for results from %v", file.uri)
64+
return xerrors.Errorf("timed out waiting for results from %v", file.uri)
6465
}
6566
file.diagnosticsMu.Lock()
6667
defer file.diagnosticsMu.Unlock()
6768
for _, d := range file.diagnostics {
6869
spn, err := file.mapper.RangeSpan(d.Range)
6970
if err != nil {
70-
return fmt.Errorf("Could not convert position %v for %q", d.Range, d.Message)
71+
return xerrors.Errorf("Could not convert position %v for %q", d.Range, d.Message)
7172
}
7273
fmt.Printf("%v: %v\n", spn, d.Message)
7374
}

internal/lsp/cmd/cmd.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"golang.org/x/tools/internal/lsp/source"
2727
"golang.org/x/tools/internal/span"
2828
"golang.org/x/tools/internal/tool"
29+
"golang.org/x/xerrors"
2930
)
3031

3132
// Application is the main application as passed to tool.Main
@@ -321,7 +322,7 @@ func (c *cmdClient) getFile(ctx context.Context, uri span.URI) *cmdFile {
321322
fname := uri.Filename()
322323
content, err := ioutil.ReadFile(fname)
323324
if err != nil {
324-
file.err = fmt.Errorf("%v: %v", uri, err)
325+
file.err = xerrors.Errorf("%v: %v", uri, err)
325326
return file
326327
}
327328
f := c.fset.AddFile(fname, -1, len(content))
@@ -341,7 +342,7 @@ func (c *connection) AddFile(ctx context.Context, uri span.URI) *cmdFile {
341342
p.TextDocument.URI = string(uri)
342343
p.TextDocument.Text = string(file.mapper.Content)
343344
if err := c.Server.DidOpen(ctx, p); err != nil {
344-
file.err = fmt.Errorf("%v: %v", uri, err)
345+
file.err = xerrors.Errorf("%v: %v", uri, err)
345346
}
346347
}
347348
return file

internal/lsp/cmd/definition.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"golang.org/x/tools/internal/lsp/protocol"
1717
"golang.org/x/tools/internal/span"
1818
"golang.org/x/tools/internal/tool"
19+
"golang.org/x/xerrors"
1920
)
2021

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

8586
if len(locs) == 0 {
86-
return fmt.Errorf("%v: not an identifier", from)
87+
return xerrors.Errorf("%v: not an identifier", from)
8788
}
8889
hover, err := conn.Hover(ctx, &p)
8990
if err != nil {
90-
return fmt.Errorf("%v: %v", from, err)
91+
return xerrors.Errorf("%v: %v", from, err)
9192
}
9293
if hover == nil {
93-
return fmt.Errorf("%v: not an identifier", from)
94+
return xerrors.Errorf("%v: not an identifier", from)
9495
}
9596
file = conn.AddFile(ctx, span.NewURI(locs[0].URI))
9697
if file.err != nil {
97-
return fmt.Errorf("%v: %v", from, file.err)
98+
return xerrors.Errorf("%v: %v", from, file.err)
9899
}
99100
definition, err := file.mapper.Span(locs[0])
100101
if err != nil {
101-
return fmt.Errorf("%v: %v", from, err)
102+
return xerrors.Errorf("%v: %v", from, err)
102103
}
103104
description := strings.TrimSpace(hover.Contents.Value)
104105
var result interface{}
@@ -115,7 +116,7 @@ func (d *definition) Run(ctx context.Context, args ...string) error {
115116
Desc: description,
116117
}
117118
default:
118-
return fmt.Errorf("unknown emulation for definition: %s", d.query.Emulate)
119+
return xerrors.Errorf("unknown emulation for definition: %s", d.query.Emulate)
119120
}
120121
if err != nil {
121122
return err
@@ -131,7 +132,7 @@ func (d *definition) Run(ctx context.Context, args ...string) error {
131132
case *guru.Definition:
132133
fmt.Printf("%s: defined here as %s", d.ObjPos, d.Desc)
133134
default:
134-
return fmt.Errorf("no printer for type %T", result)
135+
return xerrors.Errorf("no printer for type %T", result)
135136
}
136137
return nil
137138
}

internal/lsp/cmd/format.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"golang.org/x/tools/internal/lsp/protocol"
1717
"golang.org/x/tools/internal/lsp/source"
1818
"golang.org/x/tools/internal/span"
19+
"golang.org/x/xerrors"
1920
)
2021

2122
// format implements the format verb for gopls.
@@ -68,18 +69,18 @@ func (f *format) Run(ctx context.Context, args ...string) error {
6869
return err
6970
}
7071
if loc.Range.Start != loc.Range.End {
71-
return fmt.Errorf("only full file formatting supported")
72+
return xerrors.Errorf("only full file formatting supported")
7273
}
7374
p := protocol.DocumentFormattingParams{
7475
TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
7576
}
7677
edits, err := conn.Formatting(ctx, &p)
7778
if err != nil {
78-
return fmt.Errorf("%v: %v", spn, err)
79+
return xerrors.Errorf("%v: %v", spn, err)
7980
}
8081
sedits, err := lsp.FromProtocolEdits(file.mapper, edits)
8182
if err != nil {
82-
return fmt.Errorf("%v: %v", spn, err)
83+
return xerrors.Errorf("%v: %v", spn, err)
8384
}
8485
ops := source.EditsToDiff(sedits)
8586
lines := diff.SplitLines(string(file.mapper.Content))

0 commit comments

Comments
 (0)