PageRenderTime 201ms CodeModel.GetById 64ms app.highlight 8ms RepoModel.GetById 124ms app.codeStats 0ms

/lib/haskell-extras/System/IO/Error.hs

https://github.com/akakcolin/ajhc
Haskell | 100 lines | 48 code | 18 blank | 34 comment | 1 complexity | dfeb206fbdbbf323a38ed0513448150c MD5 | raw file
  1module System.IO.Error (
  2    IOError(),  userError,  mkIOError,  annotateIOError,  isAlreadyExistsError,
  3    isDoesNotExistError,  isAlreadyInUseError,  isFullError,  isEOFError,
  4    isIllegalOperation,  isPermissionError,  isUserError,  ioeGetErrorString,
  5    ioeGetHandle,  ioeGetFileName,  IOErrorType(),  alreadyExistsErrorType,
  6    doesNotExistErrorType,  alreadyInUseErrorType,  fullErrorType,
  7    eofErrorType,  illegalOperationErrorType,  permissionErrorType,
  8    userErrorType,  ioError,  catch,  try
  9  ) where
 10
 11import Jhc.Type.Handle
 12
 13-- | The construct 'try' @comp@ exposes IO errors which occur within a
 14-- computation, and which are not fully handled.
 15--
 16-- Non-I\/O exceptions are not caught by this variant; to catch all
 17-- exceptions, use 'Control.Exception.try' from "Control.Exception".
 18
 19try            :: IO a -> IO (Either IOError a)
 20try f          =  catch (do r <- f
 21                            return (Right r))
 22                        (return . Left)
 23
 24-- -----------------------------------------------------------------------------
 25-- Constructing an IOError
 26
 27-- | Construct an 'IOError' of the given type where the second argument
 28-- describes the error location and the third and fourth argument
 29-- contain the file handle and file path of the file involved in the
 30-- error if applicable.
 31mkIOError :: IOErrorType -> String -> Maybe Handle -> Maybe FilePath -> IOError
 32mkIOError = IOError
 33
 34-- TODO(john): fix
 35annotateIOError :: IOError -> String -> Maybe Handle -> Maybe FilePath -> IOError
 36annotateIOError ioe _s _mh _mfp = ioe
 37
 38check :: IOErrorType -> IOError -> Bool
 39check errorType ioe = ioeGetErrorType ioe == errorType
 40
 41-- | An error indicating that an 'IO' operation failed because
 42-- one of its arguments already exists.
 43isAlreadyExistsError :: IOError -> Bool
 44isAlreadyExistsError = check AlreadyExists
 45
 46-- | An error indicating that an 'IO' operation failed because
 47-- one of its arguments does not exist.
 48isDoesNotExistError :: IOError -> Bool
 49isDoesNotExistError  = check DoesNotExist
 50
 51-- | An error indicating that an 'IO' operation failed because
 52-- one of its arguments is a single-use resource, which is already
 53-- being used (for example, opening the same file twice for writing
 54-- might give this error).
 55isAlreadyInUseError :: IOError -> Bool
 56isAlreadyInUseError  = check AlreadyInUse
 57
 58-- | An error indicating that an 'IO' operation failed because
 59-- the device is full.
 60isFullError         :: IOError -> Bool
 61isFullError          = check Full
 62
 63-- | An error indicating that an 'IO' operation failed because
 64-- the end of file has been reached.
 65isEOFError          :: IOError -> Bool
 66isEOFError           = check EOF
 67
 68-- | An error indicating that an 'IO' operation failed because
 69-- the operation was not possible.
 70-- Any computation which returns an 'IO' result may fail with
 71-- 'isIllegalOperation'.  In some cases, an implementation will not be
 72-- able to distinguish between the possible error causes.  In this case
 73-- it should fail with 'isIllegalOperation'.
 74isIllegalOperation  :: IOError -> Bool
 75isIllegalOperation   = check IllegalOperation
 76
 77-- | An error indicating that an 'IO' operation failed because
 78-- the user does not have sufficient operating system privilege
 79-- to perform that operation.
 80isPermissionError   :: IOError -> Bool
 81isPermissionError    = check Permission
 82
 83-- | A programmer-defined error value constructed using 'userError'.
 84isUserError         :: IOError -> Bool
 85isUserError          = check User
 86
 87deriving instance Eq IOErrorType
 88deriving instance Ord IOErrorType
 89
 90alreadyExistsErrorType = AlreadyExists
 91doesNotExistErrorType = DoesNotExist
 92alreadyInUseErrorType = AlreadyInUse
 93fullErrorType = Full
 94eofErrorType = EOF
 95illegalOperationErrorType = IllegalOperation
 96permissionErrorType = Permission
 97userErrorType = User
 98
 99instance Show IOError where
100    showsPrec _ s = (ioeGetErrorString s ++)