Move some catch definitions around to avoid an import loop
[ghc-base.git] / System / IO / Error.hs
index 7250ef4..372bb15 100644 (file)
@@ -1,4 +1,4 @@
-{-# OPTIONS -fno-implicit-prelude #-}
+{-# OPTIONS_GHC -XNoImplicitPrelude #-}
 
 -----------------------------------------------------------------------------
 -- |
 module System.IO.Error (
 
     -- * I\/O errors
-    IOError,                   -- = IOException
+    IOError,                    -- = IOException
 
-    userError,                 -- :: String  -> IOError
+    userError,                  -- :: String  -> IOError
 
 #ifndef __NHC__
-    mkIOError,                 -- :: IOErrorType -> String -> Maybe Handle
-                               --    -> Maybe FilePath -> IOError
+    mkIOError,                  -- :: IOErrorType -> String -> Maybe Handle
+                                --    -> Maybe FilePath -> IOError
 
-    annotateIOError,           -- :: IOError -> String -> Maybe Handle
-                               --    -> Maybe FilePath -> IOError
+    annotateIOError,            -- :: IOError -> String -> Maybe Handle
+                                --    -> Maybe FilePath -> IOError
 #endif
 
     -- ** Classifying I\/O errors
-    isAlreadyExistsError,      -- :: IOError -> Bool
+    isAlreadyExistsError,       -- :: IOError -> Bool
     isDoesNotExistError,
     isAlreadyInUseError,
     isFullError, 
@@ -41,23 +41,25 @@ module System.IO.Error (
 
     -- ** Attributes of I\/O errors
 #ifndef __NHC__
-    ioeGetErrorType,           -- :: IOError -> IOErrorType
+    ioeGetErrorType,            -- :: IOError -> IOErrorType
+    ioeGetLocation,             -- :: IOError -> String
 #endif
-    ioeGetErrorString,         -- :: IOError -> String
-    ioeGetHandle,              -- :: IOError -> Maybe Handle
-    ioeGetFileName,            -- :: IOError -> Maybe FilePath
+    ioeGetErrorString,          -- :: IOError -> String
+    ioeGetHandle,               -- :: IOError -> Maybe Handle
+    ioeGetFileName,             -- :: IOError -> Maybe FilePath
 
 #ifndef __NHC__
-    ioeSetErrorType,           -- :: IOError -> IOErrorType -> IOError
-    ioeSetErrorString,         -- :: IOError -> String -> IOError
-    ioeSetHandle,              -- :: IOError -> Handle -> IOError
-    ioeSetFileName,            -- :: IOError -> FilePath -> IOError
+    ioeSetErrorType,            -- :: IOError -> IOErrorType -> IOError
+    ioeSetErrorString,          -- :: IOError -> String -> IOError
+    ioeSetLocation,             -- :: IOError -> String -> IOError
+    ioeSetHandle,               -- :: IOError -> Handle -> IOError
+    ioeSetFileName,             -- :: IOError -> FilePath -> IOError
 #endif
 
     -- * Types of I\/O error
-    IOErrorType,               -- abstract
+    IOErrorType,                -- abstract
 
-    alreadyExistsErrorType,    -- :: IOErrorType
+    alreadyExistsErrorType,     -- :: IOErrorType
     doesNotExistErrorType,
     alreadyInUseErrorType,
     fullErrorType,
@@ -67,7 +69,7 @@ module System.IO.Error (
     userErrorType,
 
     -- ** 'IOErrorType' predicates
-    isAlreadyExistsErrorType,  -- :: IOErrorType -> Bool
+    isAlreadyExistsErrorType,   -- :: IOErrorType -> Bool
     isDoesNotExistErrorType,
     isAlreadyInUseErrorType,
     isFullErrorType, 
@@ -78,28 +80,33 @@ module System.IO.Error (
 
     -- * Throwing and catching I\/O errors
 
-    ioError,                   -- :: IOError -> IO a
+    ioError,                    -- :: IOError -> IO a
 
-    catch,                     -- :: IO a -> (IOError -> IO a) -> IO a
-    try,                       -- :: IO a -> IO (Either IOError a)
+    catch,                      -- :: IO a -> (IOError -> IO a) -> IO a
+    try,                        -- :: IO a -> IO (Either IOError a)
 
 #ifndef __NHC__
-    modifyIOError,             -- :: (IOError -> IOError) -> IO a -> IO a
+    modifyIOError,              -- :: (IOError -> IOError) -> IO a -> IO a
 #endif
   ) where
 
+#ifndef __HUGS__
+import qualified Control.Exception.Base as New (catch)
+#endif
+
+#ifndef __HUGS__
 import Data.Either
+#endif
 import Data.Maybe
 
 #ifdef __GLASGOW_HASKELL__
 import GHC.Base
 import GHC.IOBase
-import GHC.Exception
 import Text.Show
 #endif
 
 #ifdef __HUGS__
-import Hugs.Prelude(Handle, IOException(..), IOErrorType(..))
+import Hugs.Prelude(Handle, IOException(..), IOErrorType(..), IO)
 #endif
 
 #ifdef __NHC__
@@ -108,7 +115,7 @@ import IO
   , try
   , ioError
   , userError
-  , isAlreadyExistsError       -- :: IOError -> Bool
+  , isAlreadyExistsError        -- :: IOError -> Bool
   , isDoesNotExistError
   , isAlreadyInUseError
   , isFullError
@@ -148,11 +155,14 @@ try f          =  catch (do r <- f
 mkIOError :: IOErrorType -> String -> Maybe Handle -> Maybe FilePath -> IOError
 mkIOError t location maybe_hdl maybe_filename =
                IOError{ ioe_type = t, 
-                       ioe_location = location,
-                       ioe_description = "",
-                       ioe_handle = maybe_hdl, 
-                       ioe_filename = maybe_filename
-                       }
+                        ioe_location = location,
+                        ioe_description = "",
+#if defined(__GLASGOW_HASKELL__)
+                        ioe_errno = Nothing,
+#endif
+                        ioe_handle = maybe_hdl, 
+                        ioe_filename = maybe_filename
+                        }
 #ifdef __NHC__
 mkIOError EOF       location maybe_hdl maybe_filename =
     EOFError location (fromJust maybe_hdl)
@@ -226,8 +236,8 @@ isUserError          = isUserErrorType             . ioeGetErrorType
 
 #ifdef __NHC__
 data IOErrorType = AlreadyExists | NoSuchThing | ResourceBusy
-                | ResourceExhausted | EOF | IllegalOperation
-                | PermissionDenied | UserError
+                 | ResourceExhausted | EOF | IllegalOperation
+                 | PermissionDenied | UserError
 #endif
 
 -- | I\/O error where the operation failed because one of its arguments
@@ -264,8 +274,8 @@ permissionErrorType      :: IOErrorType
 permissionErrorType       = PermissionDenied
 
 -- | I\/O error that is programmer-defined.
-userErrorType           :: IOErrorType
-userErrorType            = UserError
+userErrorType            :: IOErrorType
+userErrorType             = UserError
 
 -- -----------------------------------------------------------------------------
 -- IOErrorType predicates
@@ -319,8 +329,9 @@ isUserErrorType _ = False
 -- Miscellaneous
 
 #if defined(__GLASGOW_HASKELL__) || defined(__HUGS__)
-ioeGetErrorType              :: IOError -> IOErrorType
+ioeGetErrorType       :: IOError -> IOErrorType
 ioeGetErrorString     :: IOError -> String
+ioeGetLocation        :: IOError -> String
 ioeGetHandle          :: IOError -> Maybe Handle
 ioeGetFileName        :: IOError -> Maybe FilePath
 
@@ -330,17 +341,21 @@ ioeGetErrorString ioe
    | isUserErrorType (ioe_type ioe) = ioe_description ioe
    | otherwise                      = show (ioe_type ioe)
 
+ioeGetLocation ioe = ioe_location ioe
+
 ioeGetHandle ioe = ioe_handle ioe
 
 ioeGetFileName ioe = ioe_filename ioe
 
-ioeSetErrorType                :: IOError -> IOErrorType -> IOError
-ioeSetErrorString      :: IOError -> String      -> IOError
-ioeSetHandle           :: IOError -> Handle      -> IOError
-ioeSetFileName         :: IOError -> FilePath    -> IOError
+ioeSetErrorType   :: IOError -> IOErrorType -> IOError
+ioeSetErrorString :: IOError -> String      -> IOError
+ioeSetLocation    :: IOError -> String      -> IOError
+ioeSetHandle      :: IOError -> Handle      -> IOError
+ioeSetFileName    :: IOError -> FilePath    -> IOError
 
 ioeSetErrorType   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 }
 
@@ -360,8 +375,9 @@ annotateIOError :: IOError
               -> Maybe Handle 
               -> Maybe FilePath 
               -> IOError 
-annotateIOError (IOError ohdl errTy _ str opath) loc hdl path = 
-  IOError (hdl `mplus` ohdl) errTy loc str (path `mplus` opath)
+annotateIOError ioe loc hdl path = 
+  ioe{ ioe_handle = hdl `mplus` ioe_handle ioe,
+       ioe_location = loc, ioe_filename = path `mplus` ioe_filename ioe }
   where
     Nothing `mplus` ys = ys
     xs      `mplus` _  = xs
@@ -377,3 +393,26 @@ annotateIOError (UserError loc msg) msg' file' hdl' =
 annotateIOError (PatternError loc) msg' file' hdl' =
     PatternError (loc++'\n':msg')
 #endif
+
+#ifndef __HUGS__
+-- | The 'catch' function establishes a handler that receives any 'IOError'
+-- raised in the action protected by 'catch'.  An 'IOError' is caught by
+-- the most recent handler established by 'catch'.  These handlers are
+-- not selective: all 'IOError's are caught.  Exception propagation
+-- must be explicitly provided in a handler by re-raising any unwanted
+-- exceptions.  For example, in
+--
+-- > f = catch g (\e -> if IO.isEOFError e then return [] else ioError e)
+--
+-- the function @f@ returns @[]@ when an end-of-file exception
+-- (cf. 'System.IO.Error.isEOFError') occurs in @g@; otherwise, the
+-- exception is propagated to the next outer handler.
+--
+-- When an exception propagates outside the main program, the Haskell
+-- system prints the associated 'IOError' value and exits the program.
+--
+-- Non-I\/O exceptions are not caught by this variant; to catch all
+-- exceptions, use 'Control.Exception.catch' from "Control.Exception".
+catch :: IO a -> (IOError -> IO a) -> IO a
+catch = New.catch
+#endif /* !__HUGS__ */