Skip to content

Commit

Permalink
Merge pull request #2 from xushiwei/q
Browse files Browse the repository at this point in the history
use github.com/qiniu/x/errors
  • Loading branch information
xushiwei authored Jun 17, 2022
2 parents 3c43a68 + 4dd28ed commit c43588b
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 21 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
go.work*
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/goplus/mod

go 1.16

require golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4
require (
github.com/qiniu/x v1.11.9
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/qiniu/x v1.11.9 h1:IfQNdeNcK43Q1+b/LdrcqmWjlhxq051YVBnua8J2qN8=
github.com/qiniu/x v1.11.9/go.mod h1:03Ni9tj+N2h2aKnAz+6N0Xfl8FwMEDRC2PAlxekASDs=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
Expand Down
3 changes: 2 additions & 1 deletion gopmod/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/goplus/mod/modcache"
"github.com/goplus/mod/modfetch"
"github.com/goplus/mod/modload"
"github.com/qiniu/x/errors"
"golang.org/x/mod/module"
)

Expand Down Expand Up @@ -184,7 +185,7 @@ func New(mod modload.Module) *Module {
func Load(dir string, mode mod.Mode) (*Module, error) {
mod, err := modload.Load(dir, mode)
if err != nil {
return nil, err
return nil, errors.NewWith(err, `modload.Load(dir, mode)`, -2, "modload.Load", dir, mode)
}
return New(mod), nil
}
Expand Down
9 changes: 5 additions & 4 deletions modfile/gop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"path/filepath"
"testing"

"github.com/qiniu/x/errors"
"golang.org/x/mod/modfile"
)

Expand Down Expand Up @@ -224,7 +225,7 @@ register -
classfile .gmx .spx github.com/goplus/spx math
classfile .gmx .spx github.com/goplus/spx math
`)
doTestParseErr(t, `gop.mod:2: usage: classfile projExt workExt [classFilePkgPath ...]`, `
doTestParseErr(t, `gop.mod:2: usage: classfile projExt workExt classFilePkgPath ...`, `
classfile .gmx .spx
`)
doTestParseErr(t, `gop.mod:2: ext . invalid: invalid ext format`, `
Expand All @@ -247,12 +248,12 @@ go 1.x
func doTestParseErr(t *testing.T, errMsg string, gopmod string) {
t.Run(errMsg, func(t *testing.T) {
_, err := Parse("gop.mod", []byte(gopmod), nil)
if err == nil {
if err == nil || err.Error() == "" {
t.Fatal("Parse: no error?")
return
}
if err.Error() != errMsg {
t.Error("Parse got:", err, "\nExpected:", errMsg)
if errRet := errors.Summary(err); errRet != errMsg {
t.Error("Parse got:", errRet, "\nExpected:", errMsg)
}
})
}
Expand Down
46 changes: 37 additions & 9 deletions modfile/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
package modfile

import (
"errors"
"fmt"
"path/filepath"
"runtime"
"strconv"
"strings"

"github.com/qiniu/x/errors"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
)
Expand Down Expand Up @@ -105,6 +106,7 @@ func ParseLax(file string, data []byte, fix VersionFixer) (*File, error) {
func parseToFile(file string, data []byte, fix VersionFixer, strict bool) (parsed *File, err error) {
f, err := modfile.ParseLax(file, data, fix)
if err != nil {
err = errors.NewWith(err, `modfile.ParseLax(file, data, fix)`, -2, "modfile.ParseLax", file, data, fix)
return
}
parsed = &File{File: *f}
Expand All @@ -123,30 +125,37 @@ func parseToFile(file string, data []byte, fix VersionFixer, strict bool) (parse
}
}
if len(errs) > 0 {
return nil, errs
return nil, errors.NewWith(errs, `len(errs) > 0`, -1, ">", len(errs), 0)
}
return
}

func (f *File) parseVerb(errs *ErrorList, verb string, line *Line, args []string, strict bool) {
wrapModPathError := func(modPath string, err error) {
*errs = append(*errs, Error{
errs.Add(&Error{
Filename: f.Syntax.Name,
Pos: line.Start,
ModPath: modPath,
Verb: verb,
Err: err,
})
}
wrapError := func(err error) {
*errs = append(*errs, Error{
wrapError1 := func(err error) {
errs.Add(&Error{
Filename: f.Syntax.Name,
Pos: line.Start,
Err: err,
})
}
wrapError := func(err error) {
file, line := fileLine(2)
e := errors.NewFrame(err, "", file, line, "wrapError", err)
wrapError1(e)
}
errorf := func(format string, args ...interface{}) {
wrapError(fmt.Errorf(format, args...))
file, line := fileLine(2)
e := errors.NewFrame(fmt.Errorf(format, args...), "", file, line, "errorf", format, args)
wrapError1(e)
}
switch verb {
case "require", "exclude", "module", "go", "retract":
Expand Down Expand Up @@ -252,7 +261,7 @@ func (f *File) parseVerb(errs *ErrorList, verb string, line *Line, args []string
return
}
if len(args) < 3 {
errorf("usage: classfile projExt workExt [classFilePkgPath ...]")
errorf("usage: classfile projExt workExt classFilePkgPath ...")
return
}
projExt, err := parseExt(&args[0])
Expand Down Expand Up @@ -280,6 +289,11 @@ func (f *File) parseVerb(errs *ErrorList, verb string, line *Line, args []string
}
}

func fileLine(n int) (file string, line int) {
_, file, line, _ = runtime.Caller(n)
return
}

func parseVersion(verb string, path string, s *string) (string, error) {
t, err := parseString(s)
if err != nil {
Expand Down Expand Up @@ -389,8 +403,22 @@ func (e *InvalidExtError) Error() string {

func (e *InvalidExtError) Unwrap() error { return e.Err }

type ErrorList = modfile.ErrorList
type Error = modfile.Error
type ErrorList = errors.List
type Error modfile.Error

func (p *Error) Error() string {
return (*modfile.Error)(p).Error()
}

func (p *Error) Unwrap() error {
return p.Err
}

func (p *Error) Summary() string {
cpy := *(*modfile.Error)(p)
cpy.Err = errors.New(errors.Summary(p.Unwrap()))
return cpy.Error()
}

// -----------------------------------------------------------------------------

Expand Down
11 changes: 7 additions & 4 deletions modload/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package modload

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand All @@ -26,6 +25,7 @@ import (
"github.com/goplus/mod"
"github.com/goplus/mod/env"
"github.com/goplus/mod/modfile"
"github.com/qiniu/x/errors"
"golang.org/x/mod/module"

gomodfile "golang.org/x/mod/modfile"
Expand Down Expand Up @@ -121,23 +121,26 @@ func fixVersion(fixed *bool) modfile.VersionFixer {
func Load(dir string, mode mod.Mode) (p Module, err error) {
gopmod, err := mod.GOPMOD(dir, mode)
if err != nil {
err = errors.NewWith(err, `mod.GOPMOD(dir, mode)`, -2, "mod.GOPMOD", dir, mode)
return
}

data, err := os.ReadFile(gopmod)
if err != nil {
err = errors.NewWith(err, `os.ReadFile(gopmod)`, -2, "os.ReadFile", gopmod)
return
}

var fixed bool
f, err := modfile.Parse(gopmod, data, fixVersion(&fixed))
fix := fixVersion(&fixed)
f, err := modfile.Parse(gopmod, data, fix)
if err != nil {
// Errors returned by modfile.Parse begin with file:line.
err = errors.NewWith(err, `modfile.Parse(gopmod, data, fix)`, -2, "modfile.Parse", gopmod, data, fix)
return
}
if f.Module == nil {
// No module declaration. Must add module path.
return Module{}, ErrNoModDecl
return Module{}, errors.NewWith(ErrNoModDecl, `f.Module == nil`, -2, "==", f.Module, nil)
}
return Module{File: f}, nil
}
Expand Down

0 comments on commit c43588b

Please sign in to comment.