module System.IO.Error ( IOException(..), ioException, -- * I\/O errors IOError, IOErrorType(..), userError, mkIOError, annotateIOError, -- ** Classifying I\/O errors isAlreadyExistsError, isDoesNotExistError, isAlreadyInUseError, isFullError, isEOFError, isIllegalOperation, isPermissionError, isUserError, isResourceVanishedError, -- ** Attributes of I\/O errors ioeGetErrorType, ioeGetLocation, ioeGetErrorString, ioeGetHandle, ioeGetFileName, ioeSetErrorType, ioeSetErrorString, ioeSetLocation, ioeSetHandle, ioeSetFileName, -- * Types of I\/O error alreadyExistsErrorType, doesNotExistErrorType, alreadyInUseErrorType, fullErrorType, eofErrorType, illegalOperationErrorType, permissionErrorType, userErrorType, resourceVanishedErrorType, -- ** 'IOErrorType' predicates isAlreadyExistsErrorType, isDoesNotExistErrorType, isAlreadyInUseErrorType, isFullErrorType, isEOFErrorType, isIllegalOperationErrorType, isPermissionErrorType, isUserErrorType, isResourceVanishedErrorType, -- * Throwing and catching I\/O errors ioError, catchIOError, tryIOError, modifyIOError, ) whereimport qualified Prelude()import Primitives(IO)import Control.Exception.Internalimport Control.Monadimport Data.Boolimport Data.Char_Typeimport Data.Eitherimport Data.Eqimport Data.Functionimport Data.Functorimport Data.Int.Intimport Data.Listimport Data.Maybeimport Data.Records -- needed since we don't import Mhs.Builtinimport Data.Stringimport {-# SOURCE #-} Data.Typeableimport Foreign.C.Types (CInt)import System.IO.Internalimport Text.ShowioException :: IOException -> IO aioException err = throw errioError :: IOError -> IO aioError = ioExceptiontype IOError = IOExceptiondata 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_errno :: Maybe CInt, -- ^ errno leading to this error, if any. ioe_filename :: Maybe FilePath -- ^ filename the error is related to -- (some libraries may assume different encodings -- when constructing this field from e.g. 'ByteString' -- or other types) } deriving (Typeable)instance Exception IOExceptioninstance Eq IOException where (IOError h1 e1 loc1 str1 en1 fn1) == (IOError h2 e2 loc2 str2 en2 fn2) = e1==e2 && str1==str2 && h1==h2 && loc1==loc2 && en1==en2 && fn1==fn2-- | An abstract type that contains a value for each variant of 'IOError'.data IOErrorType -- Haskell 2010: = AlreadyExists | NoSuchThing | ResourceBusy | ResourceExhausted | EOF | IllegalOperation | PermissionDenied | UserError -- GHC only: | UnsatisfiedConstraints | SystemError | ProtocolError | OtherError | InvalidArgument | InappropriateType | HardwareFault | UnsupportedOperation | TimeExpired | ResourceVanished | Interrupted deriving (Eq)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 -> "unsatisfied constraints" -- ultra-precise! UnsupportedOperation -> "unsupported operation"userError :: String -> IOErroruserError str = IOError Nothing UserError "" str Nothing Nothinginstance 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 ")")-----------tryIOError :: IO a -> IO (Either IOError a)tryIOError f = catch (Right <$> f) (return . Left)mkIOError :: IOErrorType -> String -> Maybe Handle -> Maybe FilePath -> IOErrormkIOError t location maybe_hdl maybe_filename = IOError{ ioe_type = t, ioe_location = location, ioe_description = "", ioe_errno = Nothing, ioe_handle = maybe_hdl, ioe_filename = maybe_filename }isAlreadyExistsError :: IOError -> BoolisAlreadyExistsError = isAlreadyExistsErrorType . ioeGetErrorTypeisDoesNotExistError :: IOError -> BoolisDoesNotExistError = isDoesNotExistErrorType . ioeGetErrorTypeisAlreadyInUseError :: IOError -> BoolisAlreadyInUseError = isAlreadyInUseErrorType . ioeGetErrorTypeisFullError :: IOError -> BoolisFullError = isFullErrorType . ioeGetErrorTypeisEOFError :: IOError -> BoolisEOFError = isEOFErrorType . ioeGetErrorTypeisIllegalOperation :: IOError -> BoolisIllegalOperation = isIllegalOperationErrorType . ioeGetErrorTypeisPermissionError :: IOError -> BoolisPermissionError = isPermissionErrorType . ioeGetErrorTypeisUserError :: IOError -> BoolisUserError = isUserErrorType . ioeGetErrorTypeisResourceVanishedError :: IOError -> BoolisResourceVanishedError = isResourceVanishedErrorType . ioeGetErrorTypeisUnsupportedOperation :: IOError -> BoolisUnsupportedOperation = isUnsupportedOperationType . ioeGetErrorTypealreadyExistsErrorType :: IOErrorTypealreadyExistsErrorType = AlreadyExistsdoesNotExistErrorType :: IOErrorTypedoesNotExistErrorType = NoSuchThingalreadyInUseErrorType :: IOErrorTypealreadyInUseErrorType = ResourceBusyfullErrorType :: IOErrorTypefullErrorType = ResourceExhaustedeofErrorType :: IOErrorTypeeofErrorType = EOFillegalOperationErrorType :: IOErrorTypeillegalOperationErrorType = IllegalOperationpermissionErrorType :: IOErrorTypepermissionErrorType = PermissionDenieduserErrorType :: IOErrorTypeuserErrorType = UserErrorresourceVanishedErrorType :: IOErrorTyperesourceVanishedErrorType = ResourceVanishedisAlreadyExistsErrorType :: IOErrorType -> BoolisAlreadyExistsErrorType AlreadyExists = TrueisAlreadyExistsErrorType _ = FalseisDoesNotExistErrorType :: IOErrorType -> BoolisDoesNotExistErrorType NoSuchThing = TrueisDoesNotExistErrorType _ = FalseisAlreadyInUseErrorType :: IOErrorType -> BoolisAlreadyInUseErrorType ResourceBusy = TrueisAlreadyInUseErrorType _ = FalseisFullErrorType :: IOErrorType -> BoolisFullErrorType ResourceExhausted = TrueisFullErrorType _ = FalseisEOFErrorType :: IOErrorType -> BoolisEOFErrorType EOF = TrueisEOFErrorType _ = FalseisIllegalOperationErrorType :: IOErrorType -> BoolisIllegalOperationErrorType IllegalOperation = TrueisIllegalOperationErrorType _ = FalseisPermissionErrorType :: IOErrorType -> BoolisPermissionErrorType PermissionDenied = TrueisPermissionErrorType _ = FalseisUserErrorType :: IOErrorType -> BoolisUserErrorType UserError = TrueisUserErrorType _ = FalseisResourceVanishedErrorType :: IOErrorType -> BoolisResourceVanishedErrorType ResourceVanished = TrueisResourceVanishedErrorType _ = FalseisUnsupportedOperationType :: IOErrorType -> BoolisUnsupportedOperationType UnsupportedOperation = TrueisUnsupportedOperationType _ = FalseioeGetErrorType :: IOError -> IOErrorTypeioeGetErrorString :: IOError -> StringioeGetLocation :: IOError -> StringioeGetHandle :: IOError -> Maybe HandleioeGetFileName :: IOError -> Maybe FilePathioeGetErrorType ioe = ioe_type ioeioeGetErrorString ioe | isUserErrorType (ioe_type ioe) = ioe_description ioe | otherwise = show (ioe_type ioe)ioeGetLocation ioe = ioe_location ioeioeGetHandle ioe = ioe_handle ioeioeGetFileName ioe = ioe_filename ioeioeSetErrorType :: IOError -> IOErrorType -> IOErrorioeSetErrorString :: IOError -> String -> IOErrorioeSetLocation :: IOError -> String -> IOErrorioeSetHandle :: IOError -> Handle -> IOErrorioeSetFileName :: IOError -> FilePath -> IOErrorioeSetErrorType ioe errtype = ioe{ ioe_type = errtype }ioeSetErrorString ioe str = ioe{ ioe_description = str }ioeSetLocation ioe str = ioe{ ioe_location = str }ioeSetHandle ioe hdl = ioe{ ioe_handle = Just hdl }ioeSetFileName ioe filename = ioe{ ioe_filename = Just filename }modifyIOError :: (IOError -> IOError) -> IO a -> IO amodifyIOError f io = catch io (ioError . f)annotateIOError :: IOError -> String -> Maybe Handle -> Maybe FilePath -> IOErrorannotateIOError ioe loc hdl path = ioe{ ioe_handle = hdl `mplus` ioe_handle ioe, ioe_location = loc, ioe_filename = path `mplus` ioe_filename ioe }catchIOError :: IO a -> (IOError -> IO a) -> IO acatchIOError = catch