Skip to content

Commit

Permalink
refactor: enhance error handle (#206)
Browse files Browse the repository at this point in the history
* refactor: enhance error handle

* add missing return

* use pkg error lib instead of standard error

* fix test
  • Loading branch information
easonlin404 authored and pei0804 committed Sep 15, 2018
1 parent dac684d commit fd1af3a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
35 changes: 27 additions & 8 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package gen

import (
"encoding/json"

"github.com/pkg/errors"

"log"
"os"
"path"
Expand Down Expand Up @@ -29,33 +32,49 @@ func (g *Gen) Build(searchDir, mainAPIFile, swaggerConfDir, propNamingStrategy s
p.ParseAPI(searchDir, mainAPIFile)
swagger := p.GetSwagger()

b, _ := json.MarshalIndent(swagger, "", " ")
b, err := json.MarshalIndent(swagger, "", " ")
if err != nil {
return err
}

os.MkdirAll(path.Join(searchDir, "docs"), os.ModePerm)
docs, _ := os.Create(path.Join(searchDir, "docs", "docs.go"))
docs, err := os.Create(path.Join(searchDir, "docs", "docs.go"))
if err != nil {
return err
}
defer docs.Close()

os.Mkdir(swaggerConfDir, os.ModePerm)
swaggerJSON, _ := os.Create(path.Join(swaggerConfDir, "swagger.json"))
swaggerJSON, err := os.Create(path.Join(swaggerConfDir, "swagger.json"))
if err != nil {
return err
}

defer swaggerJSON.Close()
swaggerJSON.Write(b)

swaggerYAML, _ := os.Create(path.Join(swaggerConfDir, "swagger.yaml"))
swaggerYAML, err := os.Create(path.Join(swaggerConfDir, "swagger.yaml"))
if err != nil {
return err
}

defer swaggerYAML.Close()
y, err := yaml.JSONToYAML(b)
if err != nil {
//TODO: using return error instead of panic
log.Fatalf("can't swagger json covert to yaml err: %s", err)
return errors.Wrap(err, "cannot covert json to yaml")
}

swaggerYAML.Write(y)

packageTemplate.Execute(docs, struct {
if err := packageTemplate.Execute(docs, struct {
Timestamp time.Time
Doc string
}{
Timestamp: time.Now(),
Doc: "`" + string(b) + "`",
})
}); err != nil {
return err
}

log.Printf("create docs.go at %+v", docs.Name())
return nil
Expand Down
21 changes: 14 additions & 7 deletions parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package swag

import (
"github.com/pkg/errors"

"fmt"
"go/ast"
goparser "go/parser"
Expand Down Expand Up @@ -76,9 +78,11 @@ func New() *Parser {
}

// ParseAPI parses general api info for gived searchDir and mainAPIFile
func (parser *Parser) ParseAPI(searchDir string, mainAPIFile string) {
func (parser *Parser) ParseAPI(searchDir string, mainAPIFile string) error {
log.Println("Generate general API Info")
parser.getAllGoFileInfo(searchDir)
if err := parser.getAllGoFileInfo(searchDir); err != nil {
return err
}
parser.ParseGeneralAPIInfo(path.Join(searchDir, mainAPIFile))

for _, astFile := range parser.files {
Expand All @@ -90,15 +94,16 @@ func (parser *Parser) ParseAPI(searchDir string, mainAPIFile string) {
}

parser.ParseDefinitions()

return nil
}

// ParseGeneralAPIInfo parses general api info for gived mainAPIFile path
func (parser *Parser) ParseGeneralAPIInfo(mainAPIFile string) {
func (parser *Parser) ParseGeneralAPIInfo(mainAPIFile string) error {
fileSet := token.NewFileSet()
fileTree, err := goparser.ParseFile(fileSet, mainAPIFile, nil, goparser.ParseComments)

if err != nil {
log.Panicf("ParseGeneralApiInfo occur error:%+v", err)
return errors.Wrap(err, "cannot parse soure files")
}

parser.swagger.Swagger = "2.0"
Expand Down Expand Up @@ -257,6 +262,8 @@ func (parser *Parser) ParseGeneralAPIInfo(mainAPIFile string) {
if len(securityMap) > 0 {
parser.swagger.SecurityDefinitions = securityMap
}

return nil
}

func getScopeScheme(scope string) string {
Expand Down Expand Up @@ -728,8 +735,8 @@ func defineTypeOfExample(schemaType string, exampleValue string) interface{} {
}

// GetAllGoFileInfo gets all Go source files information for given searchDir.
func (parser *Parser) getAllGoFileInfo(searchDir string) {
filepath.Walk(searchDir, parser.visit)
func (parser *Parser) getAllGoFileInfo(searchDir string) error {
return filepath.Walk(searchDir, parser.visit)
}

func (parser *Parser) visit(path string, f os.FileInfo, err error) error {
Expand Down
4 changes: 1 addition & 3 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ func TestParser_ParseGeneralApiInfoFailed(t *testing.T) {
gopath := os.Getenv("GOPATH")
assert.NotNil(t, gopath)
p := New()
assert.Panics(t, func() {
p.ParseGeneralAPIInfo("testdata/noexist.go")
})
assert.Error(t, p.ParseGeneralAPIInfo("testdata/noexist.go"))
}

func TestGetAllGoFileInfo(t *testing.T) {
Expand Down

0 comments on commit fd1af3a

Please sign in to comment.