https://github.com/motemen/go-iferr
Smartly inserts idiomatic "err" handling code in Go
https://github.com/motemen/go-iferr
Last synced: 2 months ago
JSON representation
Smartly inserts idiomatic "err" handling code in Go
- Host: GitHub
- URL: https://github.com/motemen/go-iferr
- Owner: motemen
- Created: 2015-12-16T14:47:59.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-04-15T04:12:28.000Z (about 9 years ago)
- Last Synced: 2025-03-26T18:43:48.741Z (3 months ago)
- Language: Go
- Size: 9.77 KB
- Stars: 35
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# goiferr
## Installation
go get -u github.com/motemen/go-iferr/cmd/goiferr
## Description
`goiferr` automatically inserts idiomatic error handling to given Go source code.
If there are an assign statement which involves variables with `error` type
and an empty line right after that, `goiferr` inserts `if err != nil { ... }` code there.The error handling code will be:
- `return nil, err`-like statement if the scope is inside a function whose return types include `error`
- `log.Fatal(err)` if the log package can be referred from the scope
- `t.Fatal(err)` if a variable named `t` whose type of `*testing.T` can be referred from the scope
- `panic(err.Error())` otherwiseSee examples for more information.
## Usage
Usage: goiferr [-w] ...
-w rewrite input files in place## Examples
The `return` case:
```diff
func GoVersion() (string, error) {
path, err := exec.LookPath("go")
+ if err != nil {
+ return "", err
+ }cmd := exec.Command(path, "version")
b, err := cmd.Output()
+ if err != nil {
+ return "", err
+ }return string(b), nil
}
````log` case:
```diff
import "log"func main() {
_, err := GoVersion()
+ if err != nil {
+ log.Fatal(err)
+ }
+
}
````testing` case:
```diff
import "testing"func TestFoo(t *testing.T) {
_, err := GoVersion()
+ if err != nil {
+ t.Fatal(err)
+ }
+
}```
Otherwise:
```diff
func main() {
_, err := GoVersion()
+ if err != nil {
+ panic(err.Error())
+ }
+
}
```## TODO
- Generate error handling code correctly inside a function with named return values
- Don't guess the variable name to "err"
- Make error handling code custamizable## Author
motemen