Skip to content

Commit

Permalink
Merge pull request #61 from xushiwei/req
Browse files Browse the repository at this point in the history
ErrNotFound, IsNotFound(err)
  • Loading branch information
xushiwei authored Feb 11, 2024
2 parents 23b42d8 + 6e6eea5 commit fdc5f8a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
15 changes: 11 additions & 4 deletions gopmod/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
package gopmod

import (
"syscall"

"github.com/goplus/mod"
"github.com/goplus/mod/modcache"
"github.com/goplus/mod/modfetch"
"github.com/goplus/mod/modfile"
Expand Down Expand Up @@ -46,9 +45,17 @@ var (
)

var (
ErrNotFound = mod.ErrNotFound
ErrNotClassFileMod = errors.New("not a classfile module")
)

// IsNotFound returns a boolean indicating whether the error is known to
// report that a module or package does not exist. It is satisfied by
// ErrNotFound.
func IsNotFound(err error) bool {
return errors.Err(err) == ErrNotFound
}

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

// ClassKind checks a fname is a known classfile or not.
Expand Down Expand Up @@ -98,10 +105,10 @@ func (p *Module) ImportClasses(importClass ...func(c *Project)) (err error) {
func (p *Module) importMod(modPath string, imcls func(c *Project)) (err error) {
mod, ok := p.LookupDepMod(modPath)
if !ok {
return syscall.ENOENT
return ErrNotFound
}
err = p.importClassFrom(mod, imcls)
if errors.Err(err) != syscall.ENOENT {
if !IsNotFound(err) {
return
}
mod, err = modfetch.Get(mod.String())
Expand Down
5 changes: 5 additions & 0 deletions gopmod/gopmod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ func TestClassfile(t *testing.T) {
if !mod.IsClass("_yap.gox") {
t.Fatal("mod.IsClass _yap.gox: not ok?")
}

modVer = module.Version{Path: "github.com/unknown-repo/x", Version: "v0.5.0"}
if _, err = LoadMod(modVer); !IsNotFound(err) {
log.Fatal("LoadMod github.com/unknown-repo/x:", err)
}
}

func TestClassfile2(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions gopmod/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"path/filepath"
"runtime"
"strings"
"syscall"

"github.com/goplus/mod/modcache"
"github.com/goplus/mod/modfetch"
Expand Down Expand Up @@ -173,7 +172,7 @@ func LoadFrom(gomod, gopmod string) (*Module, error) {
// If we only want to load a Go modfile, pass env parameter as nil.
func LoadMod(mod module.Version) (p *Module, err error) {
p, err = loadModFrom(mod)
if errors.Err(err) != syscall.ENOENT {
if !IsNotFound(err) {
return
}
mod, err = modfetch.Get(mod.String())
Expand Down
6 changes: 5 additions & 1 deletion mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import (
"syscall"
)

var (
ErrNotFound = syscall.ENOENT
)

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

func FindGoMod(dirFrom string) (dir, file string, err error) {
Expand All @@ -41,7 +45,7 @@ func FindGoMod(dirFrom string) (dir, file string, err error) {
break
}
}
err = syscall.ENOENT
err = ErrNotFound
return
}

Expand Down
14 changes: 7 additions & 7 deletions modfetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
"os/exec"
"path/filepath"
"strings"
"syscall"

xmod "github.com/goplus/mod"
"github.com/goplus/mod/modcache"
"golang.org/x/mod/module"
"golang.org/x/mod/semver"
Expand Down Expand Up @@ -172,7 +172,7 @@ func Get(modPath string, noCache ...bool) (mod module.Version, err error) {
}
if noCache == nil || !noCache[0] {
mod, err = getFromCache(modPath)
if err != syscall.ENOENT {
if err != xmod.ErrNotFound {
return
}
}
Expand All @@ -190,7 +190,7 @@ func Get(modPath string, noCache ...bool) (mod module.Version, err error) {
cmd.Run()
if stderr.Len() > 0 {
mod, err = getResult(stderr.String())
if err != syscall.ENOENT {
if err != xmod.ErrNotFound {
if debugVerbose {
log.Println("modfetch.Get ret:", err)
}
Expand All @@ -212,7 +212,7 @@ func getResult(data string) (mod module.Version, err error) {
}
return getMod(data[len(downloading):], nil)
}
err = syscall.ENOENT
err = xmod.ErrNotFound
return
}

Expand All @@ -227,7 +227,7 @@ func getMod(data string, next *string) (mod module.Version, err error) {
return
}
}
err = syscall.ENOENT
err = xmod.ErrNotFound
return
}

Expand All @@ -252,7 +252,7 @@ func lookupFromCache(modPath string) (modRoot string, mod module.Version, err er
if pos > 0 { // has version
fi, e := os.Stat(modRoot)
if e != nil || !fi.IsDir() {
err = syscall.ENOENT
err = xmod.ErrNotFound
}
return
}
Expand All @@ -262,7 +262,7 @@ func lookupFromCache(modPath string) (modRoot string, mod module.Version, err er
err = errors.Unwrap(err)
return
}
err = syscall.ENOENT
err = xmod.ErrNotFound
for _, fi := range fis {
if fi.IsDir() {
if name := fi.Name(); strings.HasPrefix(name, fname) {
Expand Down

0 comments on commit fdc5f8a

Please sign in to comment.