error.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package os
  5. import (
  6. "errors"
  7. )
  8. // Portable analogs of some common system call errors.
  9. var (
  10. ErrInvalid = errors.New("invalid argument")
  11. ErrPermission = errors.New("permission denied")
  12. ErrExist = errors.New("file already exists")
  13. ErrNotExist = errors.New("file does not exist")
  14. )
  15. // PathError records an error and the operation and file path that caused it.
  16. type PathError struct {
  17. Op string
  18. Path string
  19. Err error
  20. }
  21. func (e *PathError) Error() string { return e.Op + " " + e.Path + ": " + e.Err.Error() }
  22. // SyscallError records an error from a specific system call.
  23. type SyscallError struct {
  24. Syscall string
  25. Err error
  26. }
  27. func (e *SyscallError) Error() string { return e.Syscall + ": " + e.Err.Error() }
  28. // NewSyscallError returns, as an error, a new SyscallError
  29. // with the given system call name and error details.
  30. // As a convenience, if err is nil, NewSyscallError returns nil.
  31. func NewSyscallError(syscall string, err error) error {
  32. if err == nil {
  33. return nil
  34. }
  35. return &SyscallError{syscall, err}
  36. }
  37. // IsExist returns a boolean indicating whether the error is known to report
  38. // that a file or directory already exists. It is satisfied by ErrExist as
  39. // well as some syscall errors.
  40. func IsExist(err error) bool {
  41. return isExist(err)
  42. }
  43. // IsNotExist returns a boolean indicating whether the error is known to
  44. // report that a file or directory does not exist. It is satisfied by
  45. // ErrNotExist as well as some syscall errors.
  46. func IsNotExist(err error) bool {
  47. return isNotExist(err)
  48. }
  49. // IsPermission returns a boolean indicating whether the error is known to
  50. // report that permission is denied. It is satisfied by ErrPermission as well
  51. // as some syscall errors.
  52. func IsPermission(err error) bool {
  53. return isPermission(err)
  54. }