1
0
mirror of https://github.com/golang/go synced 2024-11-26 18:16:48 -07:00

os: delete code duplications in error files

Change-Id: I1ec2fcf81a7a9e45a2fae8c02c8adabc7841b4fa
Reviewed-on: https://go-review.googlesource.com/29013
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
bogem 2016-09-10 18:34:07 +05:00 committed by Ian Lance Taylor
parent ca993d6797
commit 3d562de8e3
4 changed files with 35 additions and 92 deletions

View File

@ -63,3 +63,16 @@ func IsNotExist(err error) bool {
func IsPermission(err error) bool {
return isPermission(err)
}
// underlyingError returns the underlying error for known os error types.
func underlyingError(err error) error {
switch err := err.(type) {
case *PathError:
return err.Err
case *LinkError:
return err.Err
case *SyscallError:
return err.Err
}
return err
}

View File

@ -5,46 +5,30 @@
package os
func isExist(err error) bool {
switch pe := err.(type) {
case nil:
return false
case *PathError:
err = pe.Err
case *LinkError:
err = pe.Err
case *SyscallError:
err = pe.Err
}
return contains(err.Error(), " exists")
return checkErrMessageContent(err, " exists")
}
func isNotExist(err error) bool {
switch pe := err.(type) {
case nil:
return false
case *PathError:
err = pe.Err
case *LinkError:
err = pe.Err
case *SyscallError:
err = pe.Err
}
return contains(err.Error(), "does not exist") || contains(err.Error(), "not found") ||
contains(err.Error(), "has been removed") || contains(err.Error(), "no parent")
return checkErrMessageContent(err, "does not exist", "not found",
"has been removed", "no parent")
}
func isPermission(err error) bool {
switch pe := err.(type) {
case nil:
return checkErrMessageContent(err, "permission denied")
}
// checkErrMessageContent checks if err message contains one of msgs.
func checkErrMessageContent(err error, msgs ...string) bool {
if err == nil {
return false
case *PathError:
err = pe.Err
case *LinkError:
err = pe.Err
case *SyscallError:
err = pe.Err
}
return contains(err.Error(), "permission denied")
err = underlyingError(err)
for _, msg := range msgs {
if contains(err.Error(), msg) {
return true
}
}
return false
}
// contains is a local version of strings.Contains. It knows len(sep) > 1.

View File

@ -9,43 +9,16 @@ package os
import "syscall"
func isExist(err error) bool {
switch pe := err.(type) {
case nil:
return false
case *PathError:
err = pe.Err
case *LinkError:
err = pe.Err
case *SyscallError:
err = pe.Err
}
err = underlyingError(err)
return err == syscall.EEXIST || err == syscall.ENOTEMPTY || err == ErrExist
}
func isNotExist(err error) bool {
switch pe := err.(type) {
case nil:
return false
case *PathError:
err = pe.Err
case *LinkError:
err = pe.Err
case *SyscallError:
err = pe.Err
}
err = underlyingError(err)
return err == syscall.ENOENT || err == ErrNotExist
}
func isPermission(err error) bool {
switch pe := err.(type) {
case nil:
return false
case *PathError:
err = pe.Err
case *LinkError:
err = pe.Err
case *SyscallError:
err = pe.Err
}
err = underlyingError(err)
return err == syscall.EACCES || err == syscall.EPERM || err == ErrPermission
}

View File

@ -7,16 +7,7 @@ package os
import "syscall"
func isExist(err error) bool {
switch pe := err.(type) {
case nil:
return false
case *PathError:
err = pe.Err
case *LinkError:
err = pe.Err
case *SyscallError:
err = pe.Err
}
err = underlyingError(err)
return err == syscall.ERROR_ALREADY_EXISTS ||
err == syscall.ERROR_FILE_EXISTS || err == ErrExist
}
@ -24,31 +15,13 @@ func isExist(err error) bool {
const _ERROR_BAD_NETPATH = syscall.Errno(53)
func isNotExist(err error) bool {
switch pe := err.(type) {
case nil:
return false
case *PathError:
err = pe.Err
case *LinkError:
err = pe.Err
case *SyscallError:
err = pe.Err
}
err = underlyingError(err)
return err == syscall.ERROR_FILE_NOT_FOUND ||
err == _ERROR_BAD_NETPATH ||
err == syscall.ERROR_PATH_NOT_FOUND || err == ErrNotExist
}
func isPermission(err error) bool {
switch pe := err.(type) {
case nil:
return false
case *PathError:
err = pe.Err
case *LinkError:
err = pe.Err
case *SyscallError:
err = pe.Err
}
err = underlyingError(err)
return err == syscall.ERROR_ACCESS_DENIED || err == ErrPermission
}