[project @ 2002-02-04 09:05:45 by chak]
[ghc-hetmet.git] / ghc / lib / std / PrelMarshalError.lhs
1 % -----------------------------------------------------------------------------
2 % $Id: PrelMarshalError.lhs,v 1.3 2002/02/04 09:05:46 chak Exp $
3 %
4 % (c) The FFI task force, [2000..2002]
5 %
6
7 Marshalling support: Handling of common error conditions
8
9 \begin{code}
10 {-# OPTIONS -fno-implicit-prelude #-}
11
12 module PrelMarshalError (
13
14   -- I/O errors
15   -- ----------
16
17   IOErrorType,            -- abstract data type
18
19   mkIOError,              -- :: IOErrorType 
20                           -- -> String 
21                           -- -> Maybe FilePath 
22                           -- -> Maybe Handle
23                           -- -> IOError
24   
25   alreadyExistsErrorType, -- :: IOErrorType 
26   doesNotExistErrorType,  -- :: IOErrorType 
27   alreadyInUseErrorType,  -- :: IOErrorType 
28   fullErrorType,          -- :: IOErrorType 
29   eofErrorType,           -- :: IOErrorType 
30   illegalOperationType,   -- :: IOErrorType 
31   permissionErrorType,    -- :: IOErrorType 
32   userErrorType,          -- :: IOErrorType 
33
34   annotateIOError,        -- :: IOError 
35                           -- -> String 
36                           -- -> Maybe FilePath 
37                           -- -> Maybe Handle 
38                           -- -> IOError 
39
40   -- Result value checks
41   -- -------------------
42
43   -- throw an exception on specific return values
44   --
45   throwIf,       -- :: (a -> Bool) -> (a -> String) -> IO a       -> IO a
46   throwIf_,      -- :: (a -> Bool) -> (a -> String) -> IO a       -> IO ()
47   throwIfNeg,    -- :: (Ord a, Num a) 
48                  -- =>                (a -> String) -> IO a       -> IO a
49   throwIfNeg_,   -- :: (Ord a, Num a)
50                  -- =>                (a -> String) -> IO a       -> IO ()
51   throwIfNull,   -- ::                String        -> IO (Ptr a) -> IO (Ptr a)
52
53   -- discard return value
54   --
55   void           -- IO a -> IO ()
56 ) where
57
58 import PrelPtr
59 import PrelIOBase
60 import PrelMaybe
61 import PrelNum
62 import PrelBase
63
64
65 -- I/O errors
66 -- ----------
67
68 -- construct an IO error
69 --
70 mkIOError :: IOErrorType -> String -> Maybe FilePath -> Maybe Handle -> IOError
71 mkIOError errTy loc path hdl =
72   IOException $ IOError hdl errTy loc "" path
73
74 -- pre-defined error types corresponding to the predicates in the standard
75 -- library `IO'
76 --
77 alreadyExistsErrorType, doesNotExistErrorType, alreadyInUseErrorType,
78   fullErrorType, eofErrorType, illegalOperationType, permissionErrorType, 
79   userErrorType :: IOErrorType 
80 alreadyExistsErrorType = AlreadyExists
81 doesNotExistErrorType  = NoSuchThing
82 alreadyInUseErrorType  = ResourceBusy
83 fullErrorType          = ResourceExhausted
84 eofErrorType           = EOF
85 illegalOperationType   = IllegalOperation
86 permissionErrorType    = PermissionDenied
87 userErrorType          = OtherError
88
89 -- add location information and possibly a path and handle to an existing I/O
90 -- error 
91 --
92 -- * if no file path or handle is given, the corresponding value that's in the
93 --   error is left unaltered
94 --
95 annotateIOError :: IOError 
96                 -> String 
97                 -> Maybe FilePath 
98                 -> Maybe Handle 
99                 -> IOError 
100 annotateIOError (IOException (IOError hdl errTy _ str path)) loc opath ohdl = 
101   IOException (IOError (hdl `mplus` ohdl) errTy loc str (path `mplus` opath))
102   where
103     Nothing `mplus` ys = ys
104     xs      `mplus` _  = xs
105 annotateIOError exc                                          _   _     _    = 
106   exc
107
108
109 -- Result value checks
110 -- -------------------
111
112 -- guard an IO operation and throw an exception if the result meets the given
113 -- predicate 
114 --
115 -- * the second argument computes an error message from the result of the IO
116 --   operation
117 --
118 throwIf                 :: (a -> Bool) -> (a -> String) -> IO a -> IO a
119 throwIf pred msgfct act  = 
120   do
121     res <- act
122     (if pred res then ioError . userError . msgfct else return) res
123
124 -- like `throwIf', but discarding the result
125 --
126 throwIf_                 :: (a -> Bool) -> (a -> String) -> IO a -> IO ()
127 throwIf_ pred msgfct act  = void $ throwIf pred msgfct act
128
129 -- guards against negative result values
130 --
131 throwIfNeg :: (Ord a, Num a) => (a -> String) -> IO a -> IO a
132 throwIfNeg  = throwIf (< 0)
133
134 -- like `throwIfNeg', but discarding the result
135 --
136 throwIfNeg_ :: (Ord a, Num a) => (a -> String) -> IO a -> IO ()
137 throwIfNeg_  = throwIf_ (< 0)
138
139 -- guards against null pointers
140 --
141 throwIfNull :: String -> IO (Ptr a) -> IO (Ptr a)
142 throwIfNull  = throwIf (== nullPtr) . const
143
144 -- discard the return value of an IO action
145 --
146 void     :: IO a -> IO ()
147 void act  = act >> return ()
148
149 \end{code}