Skip to content

Commit 9ee05fc

Browse files
committed
feat: panic2error
1 parent ab2b573 commit 9ee05fc

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

lang/golang/parser/parser.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,12 @@ func (p *GoParser) ParseModule(mod *Module, dir string) (err error) {
185185
return nil
186186
}
187187
}
188-
if err := p.parsePackage(p.pkgPathFromABS(path)); err != nil {
188+
pkgPath, err := p.pkgPathFromABS(path)
189+
if err == nil {
190+
if err := p.parsePackage(pkgPath); err != nil {
191+
errs = append(errs, err)
192+
}
193+
} else {
189194
errs = append(errs, err)
190195
}
191196
return nil
@@ -281,14 +286,24 @@ func (p *GoParser) searchName(name string) (ids []Identity, err error) {
281286
if e != nil || info.IsDir() || shouldIgnoreFile(path) || shouldIgnoreDir(filepath.Dir(path)) || !strings.HasSuffix(path, ".go") {
282287
return nil
283288
}
284-
mod := p.pkgPathFromABS(path)
289+
mod, e := p.pkgPathFromABS(path)
290+
if e != nil {
291+
fmt.Fprintf(os.Stderr, "get mod from pkgPathFromABS failed, err: %v", e)
292+
err = e
293+
return nil
294+
}
285295
m := p.repo.Modules[mod]
286296
if m == nil {
287297
dir, _ := filepath.Rel(p.homePageDir, path)
288298
m = newModule(mod, dir)
289299
p.repo.Modules[mod] = m
290300
}
291-
pkg := p.pkgPathFromABS(filepath.Dir(path))
301+
pkg, e := p.pkgPathFromABS(filepath.Dir(path))
302+
if e != nil {
303+
fmt.Fprintf(os.Stderr, "get pkg from pkgPathFromABS failed, err: %v", e)
304+
err = e
305+
return nil
306+
}
292307
// go AST parse file
293308
fset := token.NewFileSet()
294309
fcontent := p.getFileBytes(path)
@@ -475,14 +490,15 @@ func (p *GoParser) getModuleFromPath(path string) (name string, dir string, rel
475490
}
476491

477492
// FromABS converts an absolute path to local mod path
478-
func (p *GoParser) pkgPathFromABS(path string) PkgPath {
493+
func (p *GoParser) pkgPathFromABS(path string) (PkgPath, error) {
479494
mod, _, rel := p.getModuleFromPath(path)
480495
if mod == "" {
481-
panic("not found package from " + path)
496+
fmt.Fprintf(os.Stderr, "not found package from %s \n", path)
497+
return mod, fmt.Errorf("not found package from %s ", path)
482498
}
483499
if rel != "" && rel != "." {
484-
return mod + "/" + rel
500+
return mod + "/" + rel, nil
485501
} else {
486-
return mod
502+
return mod, nil
487503
}
488504
}

0 commit comments

Comments
 (0)