Skip to content

Commit

Permalink
Merge pull request #166 from nabbar/error_interface
Browse files Browse the repository at this point in the history
Rework Error interface

Package Errors:
- add function to check & cast error interface into golib Error interface
- update CodeError type to simplify management & error creation
- add function to simplify call of Error function from a generic error interface
- remove some useless function from Error interface

All Other Packages:
- apply change of package Errors into all other packages
  • Loading branch information
nabbar authored Aug 28, 2023
2 parents a672f6e + 984ba51 commit bca7577
Show file tree
Hide file tree
Showing 189 changed files with 1,278 additions and 1,430 deletions.
36 changes: 18 additions & 18 deletions archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,47 +68,47 @@ func ExtractFile(src, dst libfpg.Progress, fileNameContain, fileNameRegex string
}()

if tmp, e = libfpg.Temp(""); e != nil {
return ErrorFileOpen.ErrorParent(e)
return ErrorFileOpen.Error(e)
} else {
dst.SetRegisterProgress(tmp)
}

if _, e = src.Seek(0, io.SeekStart); e != nil {
return ErrorFileSeek.ErrorParent(e)
return ErrorFileSeek.Error(e)
// #nosec
}

if err = libbz2.GetFile(src, tmp); err == nil {
//logger.DebugLevel.Log("try another archive...")
return ExtractFile(tmp, dst, fileNameContain, fileNameRegex)
} else if err.IsCodeError(libbz2.ErrorIOCopy) {
} else if err.IsCode(libbz2.ErrorIOCopy) {
return err
}

if err = libgzp.GetFile(src, tmp); err == nil {
//logger.DebugLevel.Log("try another archive...")
return ExtractFile(tmp, dst, fileNameContain, fileNameRegex)
} else if !err.IsCodeError(libgzp.ErrorGZReader) {
} else if !err.IsCode(libgzp.ErrorGZReader) {
return err
}

if err = libtar.GetFile(src, tmp, fileNameContain, fileNameRegex); err == nil {
//logger.DebugLevel.Log("try another archive...")
return ExtractFile(tmp, dst, fileNameContain, fileNameRegex)
} else if !err.IsCodeError(libtar.ErrorTarNext) {
} else if !err.IsCode(libtar.ErrorTarNext) {
return err
}

if err = libzip.GetFile(src, tmp, fileNameContain, fileNameRegex); err == nil {
//logger.DebugLevel.Log("try another archive...")
return ExtractFile(tmp, dst, fileNameContain, fileNameRegex)
} else if !err.IsCodeError(libzip.ErrorZipOpen) {
} else if !err.IsCode(libzip.ErrorZipOpen) {
return err
}

if _, e = dst.ReadFrom(src); e != nil {
//logger.ErrorLevel.LogErrorCtx(logger.DebugLevel, "reopening file", err)
return ErrorIOCopy.ErrorParent(e)
return ErrorIOCopy.Error(e)
}

return nil
Expand Down Expand Up @@ -137,20 +137,20 @@ func ExtractAll(src libfpg.Progress, originalName, outputPath string, defaultDir
}()

if tmp, e = libfpg.Temp(""); e != nil {
return ErrorFileOpen.ErrorParent(e)
return ErrorFileOpen.Error(e)
} else {
src.SetRegisterProgress(tmp)
}

if err = libbz2.GetFile(src, tmp); err == nil {
return ExtractAll(tmp, originalName, outputPath, defaultDirPerm)
} else if !err.IsCodeError(libbz2.ErrorIOCopy) {
} else if !err.IsCode(libbz2.ErrorIOCopy) {
return err
}

if err = libgzp.GetFile(src, tmp); err == nil {
return ExtractAll(tmp, originalName, outputPath, defaultDirPerm)
} else if !err.IsCodeError(libgzp.ErrorGZReader) {
} else if !err.IsCode(libgzp.ErrorGZReader) {
return err
}

Expand All @@ -162,36 +162,36 @@ func ExtractAll(src libfpg.Progress, originalName, outputPath string, defaultDir
//nolint #nosec
/* #nosec */
if e = os.MkdirAll(outputPath, permDir); e != nil {
return ErrorDirCreate.ErrorParent(e)
return ErrorDirCreate.Error(e)
}
} else if e != nil {
return ErrorDirStat.ErrorParent(e)
return ErrorDirStat.Error(e)
} else if !i.IsDir() {
return ErrorDirNotDir.Error(nil)
}

if err = libtar.GetAll(src, outputPath, defaultDirPerm); err == nil {
return nil
} else if !err.IsCodeError(libtar.ErrorTarNext) {
} else if !err.IsCode(libtar.ErrorTarNext) {
return err
}

if err = libzip.GetAll(src, outputPath, defaultDirPerm); err == nil {
return nil
} else if !err.IsCodeError(libzip.ErrorZipOpen) {
} else if !err.IsCode(libzip.ErrorZipOpen) {
return err
}

if dst, e = libfpg.New(filepath.Join(outputPath, originalName), os.O_RDWR|os.O_CREATE|os.O_TRUNC, permFile); e != nil {
return ErrorFileOpen.ErrorParent(e)
return ErrorFileOpen.Error(e)
} else {
src.SetRegisterProgress(dst)
}

if _, e = src.Seek(0, io.SeekStart); e != nil {
return ErrorFileSeek.ErrorParent(e)
return ErrorFileSeek.Error(e)
} else if _, e = dst.ReadFrom(src); e != nil {
return ErrorIOCopy.ErrorParent(e)
return ErrorIOCopy.Error(e)
}

return nil
Expand All @@ -200,7 +200,7 @@ func ExtractAll(src libfpg.Progress, originalName, outputPath string, defaultDir
func CreateArchive(archiveType ArchiveType, archive libfpg.Progress, stripPath string, comment string, pathContent ...string) (created bool, err liberr.Error) {
if len(pathContent) < 1 {
//nolint #goerr113
return false, ErrorParamEmpty.ErrorParent(fmt.Errorf("pathContent is empty"))
return false, ErrorParamEmpty.Error(fmt.Errorf("pathContent is empty"))
}

switch archiveType {
Expand Down
8 changes: 4 additions & 4 deletions archive/bz2/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ import (

func GetFile(src io.ReadSeeker, dst io.WriteSeeker) errors.Error {
if _, e := src.Seek(0, io.SeekStart); e != nil {
return ErrorFileSeek.ErrorParent(e)
return ErrorFileSeek.Error(e)
} else if _, e = dst.Seek(0, io.SeekStart); e != nil {
return ErrorFileSeek.ErrorParent(e)
return ErrorFileSeek.Error(e)
}

r := bzip2.NewReader(src)

//nolint #nosec
/* #nosec */
if _, e := io.Copy(dst, r); e != nil {
return ErrorIOCopy.ErrorParent(e)
return ErrorIOCopy.Error(e)
} else if _, e = dst.Seek(0, io.SeekStart); e != nil {
return ErrorFileSeek.ErrorParent(e)
return ErrorFileSeek.Error(e)
} else {
return nil
}
Expand Down
10 changes: 5 additions & 5 deletions archive/gzip/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ import (

func GetFile(src io.ReadSeeker, dst io.WriteSeeker) errors.Error {
if _, e := src.Seek(0, io.SeekStart); e != nil {
return ErrorFileSeek.ErrorParent(e)
return ErrorFileSeek.Error(e)
} else if _, e = dst.Seek(0, io.SeekStart); e != nil {
return ErrorFileSeek.ErrorParent(e)
return ErrorFileSeek.Error(e)
}

r, e := gz.NewReader(src)
if e != nil {
return ErrorGZReader.ErrorParent(e)
return ErrorGZReader.Error(e)
}

defer func() {
Expand All @@ -51,9 +51,9 @@ func GetFile(src io.ReadSeeker, dst io.WriteSeeker) errors.Error {
//nolint #nosec
/* #nosec */
if _, e = io.Copy(dst, r); e != nil {
return ErrorIOCopy.ErrorParent(e)
return ErrorIOCopy.Error(e)
} else if _, e := dst.Seek(0, io.SeekStart); e != nil {
return ErrorFileSeek.ErrorParent(e)
return ErrorFileSeek.Error(e)
} else {
return nil
}
Expand Down
14 changes: 7 additions & 7 deletions archive/gzip/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ func Create(archive io.WriteSeeker, stripPath string, comment string, content ..

if len(content) != 1 {
//nolint #goerr113
return false, ErrorParamMismatching.ErrorParent(fmt.Errorf("content path must be limited to strictly one contents"))
return false, ErrorParamMismatching.Error(fmt.Errorf("content path must be limited to strictly one contents"))
}

if _, err = archive.Seek(0, io.SeekStart); err != nil {
return false, ErrorFileSeek.ErrorParent(err)
return false, ErrorFileSeek.Error(err)
}

if _, err = os.Stat(content[0]); err != nil {
return false, ErrorParamEmpty.ErrorParent(err)
return false, ErrorParamEmpty.Error(err)
}

w = gzip.NewWriter(archive)
Expand All @@ -66,7 +66,7 @@ func Create(archive io.WriteSeeker, stripPath string, comment string, content ..
}()

if f, err = os.Open(content[0]); err != nil {
return false, ErrorFileOpen.ErrorParent(err)
return false, ErrorFileOpen.Error(err)
}

defer func() {
Expand All @@ -76,15 +76,15 @@ func Create(archive io.WriteSeeker, stripPath string, comment string, content ..
}()

if _, err = io.Copy(w, f); err != nil {
return false, ErrorIOCopy.ErrorParent(err)
return false, ErrorIOCopy.Error(err)
}

if err = w.Close(); err != nil {
return false, ErrorGZCreate.ErrorParent(err)
return false, ErrorGZCreate.Error(err)
}

if _, err = archive.Seek(0, io.SeekStart); err != nil {
return false, ErrorFileSeek.ErrorParent(err)
return false, ErrorFileSeek.Error(err)
}

return true, nil
Expand Down
38 changes: 19 additions & 19 deletions archive/tar/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ import (
func GetFile(src, dst libfpg.Progress, filenameContain, filenameRegex string) liberr.Error {

if _, e := src.Seek(0, io.SeekStart); e != nil {
return ErrorFileSeek.ErrorParent(e)
return ErrorFileSeek.Error(e)
} else if _, e = dst.Seek(0, io.SeekStart); e != nil {
return ErrorFileSeek.ErrorParent(e)
return ErrorFileSeek.Error(e)
}

r := tar.NewReader(src)
Expand All @@ -54,7 +54,7 @@ func GetFile(src, dst libfpg.Progress, filenameContain, filenameRegex string) li
if e != nil && e == io.EOF {
return nil
} else if e != nil {
return ErrorTarNext.ErrorParent(e)
return ErrorTarNext.Error(e)
}

if h.FileInfo().Mode()&os.ModeType == os.ModeType {
Expand All @@ -67,9 +67,9 @@ func GetFile(src, dst libfpg.Progress, filenameContain, filenameRegex string) li
/* #nosec */
if f.MatchingFullPath(filenameContain) || f.RegexFullPath(filenameRegex) {
if _, e = dst.ReadFrom(r); e != nil {
return ErrorIOCopy.ErrorParent(e)
return ErrorIOCopy.Error(e)
} else if _, e = dst.Seek(0, io.SeekStart); e != nil {
return ErrorFileSeek.ErrorParent(e)
return ErrorFileSeek.Error(e)
} else {
return nil
}
Expand All @@ -80,7 +80,7 @@ func GetFile(src, dst libfpg.Progress, filenameContain, filenameRegex string) li
func GetAll(src io.ReadSeeker, outputFolder string, defaultDirPerm os.FileMode) liberr.Error {

if _, e := src.Seek(0, io.SeekStart); e != nil {
return ErrorFileSeek.ErrorParent(e)
return ErrorFileSeek.Error(e)
}

r := tar.NewReader(src)
Expand All @@ -90,7 +90,7 @@ func GetAll(src io.ReadSeeker, outputFolder string, defaultDirPerm os.FileMode)
if e != nil && e == io.EOF {
return nil
} else if e != nil {
return ErrorTarNext.ErrorParent(e)
return ErrorTarNext.Error(e)
}

//nolint #nosec
Expand All @@ -116,8 +116,8 @@ func writeContent(r io.Reader, h *tar.Header, out string, defaultDirPerm os.File
defer func() {
if dst != nil {
if e := dst.Close(); e != nil {
err = ErrorFileClose.ErrorParent(e)
err.AddParentError(err)
err = ErrorFileClose.Error(e)
err.Add(err)
}
}
}()
Expand All @@ -134,11 +134,11 @@ func writeContent(r io.Reader, h *tar.Header, out string, defaultDirPerm os.File
}

if dst, e = libfpg.New(out, os.O_RDWR|os.O_CREATE|os.O_TRUNC, inf.Mode()); e != nil {
return ErrorFileOpen.ErrorParent(e)
return ErrorFileOpen.Error(e)
} else if _, e = io.Copy(dst, r); e != nil {
return ErrorIOCopy.ErrorParent(e)
return ErrorIOCopy.Error(e)
} else if e = dst.Close(); e != nil {
return ErrorFileClose.ErrorParent(e)
return ErrorFileClose.Error(e)
}

return nil
Expand All @@ -147,10 +147,10 @@ func writeContent(r io.Reader, h *tar.Header, out string, defaultDirPerm os.File
func dirIsExistOrCreate(dirname string, dirPerm os.FileMode) liberr.Error {
if i, e := os.Stat(dirname); e != nil && os.IsNotExist(e) {
if e = os.MkdirAll(dirname, dirPerm); e != nil {
return ErrorDirCreate.ErrorParent(e)
return ErrorDirCreate.Error(e)
}
} else if e != nil {
return ErrorDestinationStat.ErrorParent(e)
return ErrorDestinationStat.Error(e)
} else if !i.IsDir() {
return ErrorDestinationIsNotDir.Error(nil)
}
Expand All @@ -170,15 +170,15 @@ func notDirExistCannotClean(filename string, flag byte, targetLink string) liber
if _, e := os.Stat(filename); e != nil && os.IsNotExist(e) {
return nil
} else if e != nil {
return ErrorDestinationStat.ErrorParent(e)
return ErrorDestinationStat.Error(e)
} else if flag&tar.TypeLink == tar.TypeLink || flag&tar.TypeSymlink == tar.TypeSymlink {
if hasFSLink(filename) && compareLinkTarget(filename, targetLink) {
return nil
}
}

if e := os.Remove(filename); e != nil {
err := ErrorDestinationRemove.ErrorParent(e)
err := ErrorDestinationRemove.Error(e)
return err
}

Expand All @@ -201,7 +201,7 @@ func createLink(link, target string, sym bool) liberr.Error {
}

if _, e := os.Stat(link); e != nil && !os.IsNotExist(e) {
return ErrorDestinationStat.ErrorParent(e)
return ErrorDestinationStat.Error(e)
} else if e == nil {
return nil
} else if compareLinkTarget(link, target) {
Expand All @@ -211,12 +211,12 @@ func createLink(link, target string, sym bool) liberr.Error {
if sym {
err := os.Symlink(libarc.CleanPath(target), libarc.CleanPath(link))
if err != nil {
return ErrorLinkCreate.ErrorParent(err)
return ErrorLinkCreate.Error(err)
}
} else {
err := os.Link(libarc.CleanPath(target), libarc.CleanPath(link))
if err != nil {
return ErrorLinkCreate.ErrorParent(err)
return ErrorLinkCreate.Error(err)
}
}

Expand Down
Loading

0 comments on commit bca7577

Please sign in to comment.