github.com/MangoDowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/src/os/error.go (about) 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 5 package os 6 7 import ( 8 "errors" 9 ) 10 11 // Portable analogs of some common system call errors. 12 var ( 13 ErrInvalid = errors.New("invalid argument") // methods on File will return this error when the receiver is nil 14 ErrPermission = errors.New("permission denied") 15 ErrExist = errors.New("file already exists") 16 ErrNotExist = errors.New("file does not exist") 17 ErrClosed = errors.New("file already closed") 18 ) 19 20 // PathError records an error and the operation and file path that caused it. 21 type PathError struct { 22 Op string 23 Path string 24 Err error 25 } 26 27 func (e *PathError) Error() string { return e.Op + " " + e.Path + ": " + e.Err.Error() } 28 29 // SyscallError records an error from a specific system call. 30 type SyscallError struct { 31 Syscall string 32 Err error 33 } 34 35 func (e *SyscallError) Error() string { return e.Syscall + ": " + e.Err.Error() } 36 37 // NewSyscallError returns, as an error, a new SyscallError 38 // with the given system call name and error details. 39 // As a convenience, if err is nil, NewSyscallError returns nil. 40 func NewSyscallError(syscall string, err error) error { 41 if err == nil { 42 return nil 43 } 44 return &SyscallError{syscall, err} 45 } 46 47 // IsExist returns a boolean indicating whether the error is known to report 48 // that a file or directory already exists. It is satisfied by ErrExist as 49 // well as some syscall errors. 50 func IsExist(err error) bool { 51 return isExist(err) 52 } 53 54 // IsNotExist returns a boolean indicating whether the error is known to 55 // report that a file or directory does not exist. It is satisfied by 56 // ErrNotExist as well as some syscall errors. 57 func IsNotExist(err error) bool { 58 return isNotExist(err) 59 } 60 61 // IsPermission returns a boolean indicating whether the error is known to 62 // report that permission is denied. It is satisfied by ErrPermission as well 63 // as some syscall errors. 64 func IsPermission(err error) bool { 65 return isPermission(err) 66 } 67 68 // underlyingError returns the underlying error for known os error types. 69 func underlyingError(err error) error { 70 switch err := err.(type) { 71 case *PathError: 72 return err.Err 73 case *LinkError: 74 return err.Err 75 case *SyscallError: 76 return err.Err 77 } 78 return err 79 }