Language-Haskell

 view release on metacpan or  search on metacpan

hugs98-Nov2003/fptools/libraries/base/GHC/IOBase.lhs  view on Meta::CPAN

-- in pure code, see 'Control.Exception.Exception'.
--
-- In Haskell 98, this is an opaque type.
type IOError = IOException

-- |Exceptions that occur in the @IO@ monad.
-- An @IOException@ records a more specific error type, a descriptive
-- string and maybe the handle that was used when the error was
-- flagged.
data IOException
 = IOError {
     ioe_handle   :: Maybe Handle,   -- the handle used by the action flagging 
				     -- the error.
     ioe_type     :: IOErrorType,    -- what it was.
     ioe_location :: String,	     -- location.
     ioe_description :: String,      -- error type specific information.
     ioe_filename :: Maybe FilePath  -- filename the error is related to.
   }

instance Eq IOException where
  (IOError h1 e1 loc1 str1 fn1) == (IOError h2 e2 loc2 str2 fn2) = 
    e1==e2 && str1==str2 && h1==h2 && loc1==loc2 && fn1==fn2

-- | An abstract type that contains a value for each variant of 'IOError'.
data IOErrorType
  -- Haskell 98:
  = AlreadyExists
  | NoSuchThing
  | ResourceBusy
  | ResourceExhausted
  | EOF
  | IllegalOperation
  | PermissionDenied
  | UserError
  -- GHC only:
  | UnsatisfiedConstraints
  | SystemError
  | ProtocolError
  | OtherError
  | InvalidArgument
  | InappropriateType
  | HardwareFault
  | UnsupportedOperation
  | TimeExpired
  | ResourceVanished
  | Interrupted
  | DynIOError Dynamic -- cheap&cheerful extensible IO error type.

instance Eq IOErrorType where
   x == y = 
     case x of
       DynIOError{} -> False -- from a strictness POV, compatible with a derived Eq inst?
       _ -> getTag x ==# getTag y
 
instance Show IOErrorType where
  showsPrec _ e =
    showString $
    case e of
      AlreadyExists	-> "already exists"
      NoSuchThing       -> "does not exist"
      ResourceBusy      -> "resource busy"
      ResourceExhausted -> "resource exhausted"
      EOF		-> "end of file"
      IllegalOperation	-> "illegal operation"
      PermissionDenied  -> "permission denied"
      UserError		-> "user error"
      HardwareFault	-> "hardware fault"
      InappropriateType -> "inappropriate type"
      Interrupted       -> "interrupted"
      InvalidArgument   -> "invalid argument"
      OtherError        -> "failed"
      ProtocolError     -> "protocol error"
      ResourceVanished  -> "resource vanished"
      SystemError	-> "system error"
      TimeExpired       -> "timeout"
      UnsatisfiedConstraints -> "unsatisified constraints" -- ultra-precise!
      UnsupportedOperation -> "unsupported operation"
      DynIOError{}      -> "unknown IO error"

-- | Construct an 'IOError' value with a string describing the error.
-- The 'fail' method of the 'IO' instance of the 'Monad' class raises a
-- 'userError', thus:
--
-- > instance Monad IO where 
-- >   ...
-- >   fail s = ioError (userError s)
--
userError       :: String  -> IOError
userError str	=  IOError Nothing UserError "" str Nothing

-- ---------------------------------------------------------------------------
-- Showing IOErrors

instance Show IOException where
    showsPrec p (IOError hdl iot loc s fn) =
      (case fn of
	 Nothing -> case hdl of
		        Nothing -> id
			Just h  -> showsPrec p h . showString ": "
	 Just name -> showString name . showString ": ") .
      (case loc of
         "" -> id
	 _  -> showString loc . showString ": ") .
      showsPrec p iot . 
      (case s of
	 "" -> id
	 _  -> showString " (" . showString s . showString ")")

-- -----------------------------------------------------------------------------
-- IOMode type

data IOMode      =  ReadMode | WriteMode | AppendMode | ReadWriteMode
                    deriving (Eq, Ord, Ix, Enum, Read, Show)
\end{code}



( run in 0.525 second using v1.01-cache-2.11-cpan-39bf76dae61 )