Skip to content

Commit

Permalink
Add source code with updated README and License
Browse files Browse the repository at this point in the history
  • Loading branch information
fatih committed Nov 30, 2019
1 parent 8ef4bd1 commit e95f04b
Show file tree
Hide file tree
Showing 6 changed files with 603 additions and 1 deletion.
33 changes: 33 additions & 0 deletions LICENSE
Expand Up @@ -27,3 +27,36 @@ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

This software includes some portions from Go. Go is used under the terms of the
BSD like license.

Copyright (c) 2012 The Go Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The Go gopher was designed by Renee French. http://reneefrench.blogspot.com/ The design is licensed under the Creative Commons 3.0 Attributions license. Read this article for more details: https://blog.golang.org/gopher
61 changes: 60 additions & 1 deletion README.md
@@ -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
```
10 changes: 10 additions & 0 deletions cmd/errwrap/main.go
@@ -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)
}
5 changes: 5 additions & 0 deletions go.mod
@@ -0,0 +1,5 @@
module github.com/fatih/errwrap

go 1.13

require golang.org/x/tools v0.0.0-20191014205221-18e3458ac98b
8 changes: 8 additions & 0 deletions go.sum
@@ -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=

0 comments on commit e95f04b

Please sign in to comment.