Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add source code with updated README and License
- Loading branch information
Showing
6 changed files
with
603 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,61 @@ | ||
# errwrap | ||
Wrap and fix Go errors with the new %w verb directive | ||
|
||
Wrap and fix Go errors with the new %w verb directive. This tool analyzes | ||
`fmt.Errorf()` calls and reports calls that contain a verb directive that is | ||
different than the new `%w` verb directive [introduced in Go v1.13](https://golang.org/doc/go1.13#error_wrapping). It's also capable of rewriting calls to use the new `%w` wrap verb directive. | ||
|
||
|
||
# Install | ||
|
||
```bash | ||
go get github.com/fatih/errwrap/cmd/errwrap | ||
``` | ||
|
||
# Usage | ||
|
||
By default, `errwrap` prints the output of the analyzer to stdout. You can pass | ||
a file, directory or a Go package: | ||
|
||
```sh | ||
$ errwrap foo.go # pass a file | ||
$ errwrap ./... # recursively analyze all files | ||
$ errwrap github.com/fatih/gomodifytags # or pass a package | ||
``` | ||
|
||
When called it displays the error with the line and column: | ||
|
||
``` | ||
gomodifytags@v1.0.1/main.go:200:16: call could wrap the error with error-wrapping directive %w | ||
gomodifytags@v1.0.1/main.go:641:17: call could wrap the error with error-wrapping directive %w | ||
gomodifytags@v1.0.1/main.go:749:15: call could wrap the error with error-wrapping directive %w | ||
``` | ||
|
||
`errwrap` is also able to rewrite your source code to replace any verb | ||
directive used for an `error` type with the `%w` verb directive. Assume we have | ||
the following source code: | ||
|
||
``` | ||
$ cat demo.go | ||
package main | ||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
func main() { | ||
_ = foo() | ||
} | ||
func foo() error { | ||
err := errors.New("bar!") | ||
return fmt.Errorf("foo failed: %s: %w bar ...", "foo", err) | ||
} | ||
``` | ||
|
||
Calling `errwrap` with the `-fix` flag will rewrite the source code: | ||
|
||
``` | ||
$ errwrap -fix example.go | ||
demo.go:14:9: call could wrap the error with error-wrapping directive %w | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/fatih/errwrap/internal/errwrap" | ||
"golang.org/x/tools/go/analysis/singlechecker" | ||
) | ||
|
||
func main() { | ||
singlechecker.Main(errwrap.Analyzer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/fatih/errwrap | ||
|
||
go 1.13 | ||
|
||
require golang.org/x/tools v0.0.0-20191014205221-18e3458ac98b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||
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/tools v0.0.0-20191014205221-18e3458ac98b h1:EsQHTYgcM562dq02r6y2Yt9VpvvLNIyNECx96XQeolA= | ||
golang.org/x/tools v0.0.0-20191014205221-18e3458ac98b/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= | ||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
Oops, something went wrong.