1 % -----------------------------------------------------------------------------
2 % $Id: PrelMarshalError.lhs,v 1.3 2002/02/04 09:05:46 chak Exp $
4 % (c) The FFI task force, [2000..2002]
7 Marshalling support: Handling of common error conditions
10 {-# OPTIONS -fno-implicit-prelude #-}
12 module PrelMarshalError (
17 IOErrorType, -- abstract data type
19 mkIOError, -- :: IOErrorType
25 alreadyExistsErrorType, -- :: IOErrorType
26 doesNotExistErrorType, -- :: IOErrorType
27 alreadyInUseErrorType, -- :: IOErrorType
28 fullErrorType, -- :: IOErrorType
29 eofErrorType, -- :: IOErrorType
30 illegalOperationType, -- :: IOErrorType
31 permissionErrorType, -- :: IOErrorType
32 userErrorType, -- :: IOErrorType
34 annotateIOError, -- :: IOError
40 -- Result value checks
41 -- -------------------
43 -- throw an exception on specific return values
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)
53 -- discard return value
68 -- construct an IO error
70 mkIOError :: IOErrorType -> String -> Maybe FilePath -> Maybe Handle -> IOError
71 mkIOError errTy loc path hdl =
72 IOException $ IOError hdl errTy loc "" path
74 -- pre-defined error types corresponding to the predicates in the standard
77 alreadyExistsErrorType, doesNotExistErrorType, alreadyInUseErrorType,
78 fullErrorType, eofErrorType, illegalOperationType, permissionErrorType,
79 userErrorType :: IOErrorType
80 alreadyExistsErrorType = AlreadyExists
81 doesNotExistErrorType = NoSuchThing
82 alreadyInUseErrorType = ResourceBusy
83 fullErrorType = ResourceExhausted
85 illegalOperationType = IllegalOperation
86 permissionErrorType = PermissionDenied
87 userErrorType = OtherError
89 -- add location information and possibly a path and handle to an existing I/O
92 -- * if no file path or handle is given, the corresponding value that's in the
93 -- error is left unaltered
95 annotateIOError :: IOError
100 annotateIOError (IOException (IOError hdl errTy _ str path)) loc opath ohdl =
101 IOException (IOError (hdl `mplus` ohdl) errTy loc str (path `mplus` opath))
103 Nothing `mplus` ys = ys
105 annotateIOError exc _ _ _ =
109 -- Result value checks
110 -- -------------------
112 -- guard an IO operation and throw an exception if the result meets the given
115 -- * the second argument computes an error message from the result of the IO
118 throwIf :: (a -> Bool) -> (a -> String) -> IO a -> IO a
119 throwIf pred msgfct act =
122 (if pred res then ioError . userError . msgfct else return) res
124 -- like `throwIf', but discarding the result
126 throwIf_ :: (a -> Bool) -> (a -> String) -> IO a -> IO ()
127 throwIf_ pred msgfct act = void $ throwIf pred msgfct act
129 -- guards against negative result values
131 throwIfNeg :: (Ord a, Num a) => (a -> String) -> IO a -> IO a
132 throwIfNeg = throwIf (< 0)
134 -- like `throwIfNeg', but discarding the result
136 throwIfNeg_ :: (Ord a, Num a) => (a -> String) -> IO a -> IO ()
137 throwIfNeg_ = throwIf_ (< 0)
139 -- guards against null pointers
141 throwIfNull :: String -> IO (Ptr a) -> IO (Ptr a)
142 throwIfNull = throwIf (== nullPtr) . const
144 -- discard the return value of an IO action
146 void :: IO a -> IO ()
147 void act = act >> return ()