chore (plg_backend_local): improve error message

This commit is contained in:
MickaelK
2024-06-05 02:34:31 +10:00
parent 5b89af8237
commit 28bf80ae2a
2 changed files with 22 additions and 6 deletions

View File

@ -76,7 +76,7 @@ func SafeOsMkdir(path string, mode os.FileMode) error {
Log.Debug("common::files safeOsMkdir err[%s] path[%s]", err.Error(), path)
return ErrFilesystemError
}
return os.Mkdir(path, mode)
return processError(os.Mkdir(path, mode))
}
func SafeOsRemove(path string) error {
@ -84,7 +84,7 @@ func SafeOsRemove(path string) error {
Log.Debug("common::files safeOsRemove err[%s] path[%s]", err.Error(), path)
return ErrFilesystemError
}
return os.Remove(path)
return processError(os.Remove(path))
}
func SafeOsRemoveAll(path string) error {
@ -92,7 +92,7 @@ func SafeOsRemoveAll(path string) error {
Log.Debug("common::files safeOsRemoveAll err[%s] path[%s]", err.Error(), path)
return ErrFilesystemError
}
return os.RemoveAll(path)
return processError(os.RemoveAll(path))
}
func SafeOsRename(from string, to string) error {
@ -103,7 +103,7 @@ func SafeOsRename(from string, to string) error {
Log.Debug("common::files safeOsRemove err[%s] to[%s]", err.Error(), to)
return ErrFilesystemError
}
return os.Rename(from, to)
return processError(os.Rename(from, to))
}
func safePath(path string) error {
@ -121,3 +121,16 @@ func safePath(path string) error {
}
return nil
}
func processError(err error) error {
if err == nil {
return nil
}
if pe, ok := err.(*os.PathError); ok {
return pe.Err
}
if le, ok := err.(*os.LinkError); ok {
return le.Err
}
return err
}

View File

@ -13,8 +13,11 @@ func SafeOsOpenFile(path string, flag int, perm os.FileMode) (*os.File, error) {
return nil, ErrFilesystemError
}
f, err := os.OpenFile(path, flag|syscall.O_NOFOLLOW, perm)
if err != nil && errors.Is(err, fs.ErrNotExist) {
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil, ErrNotFound
}
return nil, processError(err)
}
return f, err
}