[project @ 2003-01-23 18:06:01 by panne]
[ghc-base.git] / System / IO / Error.hs
index 9ac35a0..a5296cc 100644 (file)
 
 module System.IO.Error (
     IOError,                   -- abstract
-#ifdef __GLASGOW_HASKELL__
     IOErrorType,               -- abstract
-#endif
+
+    catch,                     -- :: IO a -> (IOError -> IO a) -> IO a
+    try,                       -- :: IO a -> IO (Either IOError a)
 
     ioError,                   -- :: IOError -> IO a
     userError,                 -- :: String  -> IOError
 
-#ifdef __GLASGOW_HASKELL__
+#ifndef __NHC__
     mkIOError,                 -- :: IOErrorType -> String -> Maybe Handle
                                --    -> Maybe FilePath -> IOError
 
+    annotateIOError,           -- :: IOError -> String -> Maybe Handle
+                               --    -> Maybe FilePath -> IOError
+#endif
+
     alreadyExistsErrorType,    -- :: IOErrorType
     doesNotExistErrorType,
     alreadyInUseErrorType,
@@ -44,7 +49,6 @@ module System.IO.Error (
     isIllegalOperationErrorType, 
     isPermissionErrorType,
     isUserErrorType, 
-#endif  /* __GLASGOW_HASKELL__ */
 
     isAlreadyExistsError,      -- :: IOError -> Bool
     isDoesNotExistError,
@@ -55,7 +59,7 @@ module System.IO.Error (
     isPermissionError,
     isUserError,
 
-#ifdef __GLASGOW_HASKELL__
+#ifndef __NHC__
     ioeGetErrorType,           -- :: IOError -> IOErrorType
 #endif
     ioeGetErrorString,         -- :: IOError -> String
@@ -64,21 +68,24 @@ module System.IO.Error (
 
   ) where
 
+import Data.Either
+import Data.Maybe
 
 #ifdef __GLASGOW_HASKELL__
 import GHC.Base
-import Data.Maybe
 import GHC.IOBase
+import GHC.Exception
 import Text.Show
 #endif
 
 #ifdef __HUGS__
-import Hugs.IO
+import Hugs.Prelude(Handle, IOException(..), IOErrorType(..))
 #endif
 
 #ifdef __NHC__
 import IO
   ( IOError ()
+  , try
   , ioError
   , userError
   , isAlreadyExistsError       -- :: IOError -> Bool
@@ -97,15 +104,27 @@ import IO
 --import Control.Monad (MonadPlus(mplus))
 #endif
 
-#ifdef __GLASGOW_HASKELL__
+-- | The construct @try comp@ exposes IO errors which occur within a
+-- computation, and which are not fully handled.
+-- Other exceptions are not caught by this variant;
+-- to catch all exceptions, use @try@ from "Control.Exception".
+
+#ifndef __NHC__
+try            :: IO a -> IO (Either IOError a)
+try f          =  catch (do r <- f
+                            return (Right r))
+                        (return . Left)
+#endif
+
+#if defined(__GLASGOW_HASKELL__) || defined(__HUGS__)
 -- -----------------------------------------------------------------------------
 -- Constructing an IOError
 
 mkIOError :: IOErrorType -> String -> Maybe Handle -> Maybe FilePath -> IOError
 mkIOError t location maybe_hdl maybe_filename =
-   IOException IOError{ ioe_type = t, 
+               IOError{ ioe_type = t, 
                        ioe_location = location,
-                       ioe_descr = "",
+                       ioe_description = "",
                        ioe_handle = maybe_hdl, 
                        ioe_filename = maybe_filename
                        }
@@ -124,7 +143,9 @@ mkIOError t         location maybe_hdl maybe_filename =
     ioeTypeToInt IllegalOperation  = fromEnum EPERM
     ioeTypeToInt PermissionDenied  = fromEnum EACCES
 #endif
+#endif /* __GLASGOW_HASKELL__ || __HUGS__ */
 
+#ifndef __NHC__
 -- -----------------------------------------------------------------------------
 -- IOErrorType
 
@@ -140,7 +161,7 @@ isEOFError           = isEOFErrorType              . ioeGetErrorType
 isIllegalOperation   = isIllegalOperationErrorType . ioeGetErrorType
 isPermissionError    = isPermissionErrorType       . ioeGetErrorType
 isUserError          = isUserErrorType             . ioeGetErrorType
-#endif
+#endif /* __NHC__ */
 
 -- -----------------------------------------------------------------------------
 -- IOErrorTypes
@@ -151,7 +172,6 @@ data IOErrorType = AlreadyExists | NoSuchThing | ResourceBusy
                 | PermissionDenied | UserError
 #endif
 
-#ifdef __GLASGOW_HASKELL__
 alreadyExistsErrorType, doesNotExistErrorType, alreadyInUseErrorType,
  fullErrorType, eofErrorType, illegalOperationErrorType,
  permissionErrorType, userErrorType :: IOErrorType
@@ -164,18 +184,14 @@ eofErrorType              = EOF
 illegalOperationErrorType = IllegalOperation
 permissionErrorType       = PermissionDenied
 userErrorType            = UserError
-#endif
 
 -- -----------------------------------------------------------------------------
 -- IOErrorType predicates
 
-#ifdef __GLASGOW_HASKELL__
 isAlreadyExistsErrorType, isDoesNotExistErrorType, isAlreadyInUseErrorType,
   isFullErrorType, isEOFErrorType, isIllegalOperationErrorType, 
   isPermissionErrorType, isUserErrorType :: IOErrorType -> Bool
-#endif
 
-#ifdef __GLASGOW_HASKELL__
 isAlreadyExistsErrorType AlreadyExists = True
 isAlreadyExistsErrorType _ = False
 
@@ -199,49 +215,40 @@ isPermissionErrorType _ = False
 
 isUserErrorType UserError = True
 isUserErrorType _ = False
-#endif
 
 -- -----------------------------------------------------------------------------
 -- Miscellaneous
 
-#ifdef __GLASGOW_HASKELL__
+#if defined(__GLASGOW_HASKELL__) || defined(__HUGS__)
 ioeGetErrorType              :: IOError -> IOErrorType
 ioeGetHandle          :: IOError -> Maybe Handle
 ioeGetErrorString     :: IOError -> String
 ioeGetFileName        :: IOError -> Maybe FilePath
 
-ioeGetErrorType (IOException ioe) = ioe_type ioe
-ioeGetErrorType _ = error "System.IO.Error.ioeGetErrorType: not an IO error"
+ioeGetErrorType ioe = ioe_type ioe
 
-ioeGetHandle (IOException ioe) = ioe_handle ioe
-ioeGetHandle _ = error "System.IO.Error.ioeGetHandle: not an IO error"
+ioeGetHandle ioe = ioe_handle ioe
 
-ioeGetErrorString (IOException ioe) 
-   | isUserErrorType (ioe_type ioe) = ioe_descr ioe
+ioeGetErrorString ioe
+   | isUserErrorType (ioe_type ioe) = ioe_description ioe
    | otherwise                      = show (ioe_type ioe)
-ioeGetErrorString _ = error "System.IO.Error.ioeGetErrorString: not an IO error"
 
-ioeGetFileName (IOException ioe) = ioe_filename ioe
-ioeGetFileName _ = error "System.IO.Error.ioeGetFileName: not an IO error"
-#endif
+ioeGetFileName ioe = ioe_filename ioe
 
 -- -----------------------------------------------------------------------------
 -- annotating an IOError
 
-#ifdef __GLASGOW_HASKELL__
 annotateIOError :: IOError 
               -> String 
-              -> Maybe FilePath 
               -> Maybe Handle 
+              -> Maybe FilePath 
               -> IOError 
-annotateIOError (IOException (IOError hdl errTy _ str path)) loc opath ohdl = 
-  IOException (IOError (hdl `mplus` ohdl) errTy loc str (path `mplus` opath))
+annotateIOError (IOError ohdl errTy _ str opath) loc hdl path = 
+  IOError (hdl `mplus` ohdl) errTy loc str (path `mplus` opath)
   where
     Nothing `mplus` ys = ys
     xs      `mplus` _  = xs
-annotateIOError exc _ _ _ = 
-  exc
-#endif
+#endif /* __GLASGOW_HASKELL__ || __HUGS__ */
 
 #ifdef 0 /*__NHC__*/
 annotateIOError (IOError msg file hdl code) msg' file' hdl' =