Skip to content

Commit

Permalink
Merge pull request #15 from k-yomo/refactor-error-handling
Browse files Browse the repository at this point in the history
Replace xerrors
  • Loading branch information
k-yomo authored Sep 28, 2021
2 parents 20e0713 + db53266 commit c2e014a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
10 changes: 5 additions & 5 deletions cmd/fixtory/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package main
import (
"flag"
"fmt"
"github.com/fatih/color"
"github.com/k-yomo/fixtory"
"golang.org/x/xerrors"
"io"
"log"
"os"
"path"
"path/filepath"
"strings"

"github.com/fatih/color"
"github.com/k-yomo/fixtory"
)

var version string
Expand Down Expand Up @@ -56,11 +56,11 @@ func main() {
outputDir, _ := path.Split(outputPath)
newWriter := func() (io.Writer, func(), error) {
if err := os.MkdirAll(outputDir, 0755); err != nil {
return nil, nil, xerrors.Errorf("create directory: %w", err)
return nil, nil, fmt.Errorf("create directory: %w", err)
}
writer, err := os.Create(outputPath)
if err != nil {
return nil, nil, xerrors.Errorf("create output file: %w", err)
return nil, nil, fmt.Errorf("create output file: %w", err)
}
return writer, func() { _ = writer.Close() }, nil
}
Expand Down
11 changes: 5 additions & 6 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"text/template"

"github.com/k-yomo/fixtory/pkg/astutil"
"golang.org/x/xerrors"
)

func Generate(targetDir string, outputDir string, types []string, pkgName string, newWriter func() (writer io.Writer, close func(), err error)) error {
Expand Down Expand Up @@ -69,7 +68,7 @@ func Generate(targetDir string, outputDir string, types []string, pkgName string
FieldNames: fieldNames,
}
if err := tpl.Execute(body, params); err != nil {
return xerrors.Errorf("execute factory template: %w", err)
return fmt.Errorf("execute factory template: %w", err)
}
}
if body.Len() == 0 {
Expand Down Expand Up @@ -99,22 +98,22 @@ func Generate(targetDir string, outputDir string, types []string, pkgName string
}
err = template.Must(template.New("fixtoryFile").Parse(fixtoryFileTpl)).Execute(out, params)
if err != nil {
return xerrors.Errorf("execute fixtoryFile template: %w", err)
return fmt.Errorf("execute fixtoryFile template: %w", err)
}

str, err := format.Source(out.Bytes())
if err != nil {
return xerrors.Errorf("format.Source: %w", err)
return fmt.Errorf("format.Source: %w", err)
}

writer, closeWriter, err := newWriter()
if err != nil {
return xerrors.Errorf("initialize writer: %w", err)
return fmt.Errorf("initialize writer: %w", err)
}
defer closeWriter()

if _, err := writer.Write(str); err != nil {
return xerrors.Errorf("write fixtory file: %w", err)
return fmt.Errorf("write fixtory file: %w", err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ require (
github.com/fatih/color v1.13.0
github.com/google/go-cmp v0.5.6
golang.org/x/mod v0.5.1
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
)
13 changes: 7 additions & 6 deletions pkg/astutil/astutil.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package astutil

import (
"errors"
"fmt"
"go/ast"
"go/parser"
"go/token"
"golang.org/x/mod/modfile"
"golang.org/x/xerrors"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"

"golang.org/x/mod/modfile"
)

// AstPkgWalker represents ast package walker
Expand Down Expand Up @@ -68,7 +69,7 @@ func DirToAstWalker(targetDir string) (map[string]AstPkgWalker, error) {
parser.ParseComments,
)
if err != nil {
return nil, xerrors.Errorf("parser.ParseDir: %w", err)
return nil, fmt.Errorf("parser.ParseDir: %w", err)
}

m := make(map[string]AstPkgWalker, len(pkgMap))
Expand All @@ -93,7 +94,7 @@ func ParseAstPkg(fset *token.FileSet, pkg *ast.Package) (AstPkgWalker, error) {
var err error
dir, err = os.Getwd()
if err != nil {
return AstPkgWalker{}, xerrors.Errorf("get current directory: %w", err)
return AstPkgWalker{}, fmt.Errorf("get current directory: %w", err)
}
}
pkgPath, err := packageNameOfDir(dir)
Expand Down Expand Up @@ -183,7 +184,7 @@ func parsePackageImport(srcDir string) (string, error) {
// fall back to GOPATH mode
goPaths := os.Getenv("GOPATH")
if goPaths == "" {
return "", xerrors.New("GOPATH is not set")
return "", errors.New("GOPATH is not set")
}
goPathList := strings.Split(goPaths, string(os.PathListSeparator))
for _, goPath := range goPathList {
Expand All @@ -192,5 +193,5 @@ func parsePackageImport(srcDir string) (string, error) {
return filepath.ToSlash(strings.TrimPrefix(srcDir, sourceRoot)), nil
}
}
return "", xerrors.New("Source directory is outside GOPATH")
return "", errors.New("source directory is outside GOPATH")
}

0 comments on commit c2e014a

Please sign in to comment.