1 {-# OPTIONS -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
5 -- Module : System.IO.Error
6 -- Copyright : (c) The University of Glasgow 2001
7 -- License : BSD-style (see the file libraries/base/LICENSE)
9 -- Maintainer : libraries@haskell.org
10 -- Stability : provisional
11 -- Portability : portable
13 -- Standard IO Errors.
15 -----------------------------------------------------------------------------
17 module System.IO.Error (
19 #ifdef __GLASGOW_HASKELL__
20 IOErrorType, -- abstract
23 ioError, -- :: IOError -> IO a
24 userError, -- :: String -> IOError
26 #ifdef __GLASGOW_HASKELL__
27 mkIOError, -- :: IOErrorType -> String -> Maybe Handle
28 -- -> Maybe FilePath -> IOError
30 alreadyExistsErrorType, -- :: IOErrorType
31 doesNotExistErrorType,
32 alreadyInUseErrorType,
35 illegalOperationErrorType,
39 isAlreadyExistsErrorType, -- :: IOErrorType -> Bool
40 isDoesNotExistErrorType,
41 isAlreadyInUseErrorType,
44 isIllegalOperationErrorType,
45 isPermissionErrorType,
47 #endif /* __GLASGOW_HASKELL__ */
49 isAlreadyExistsError, -- :: IOError -> Bool
58 #ifdef __GLASGOW_HASKELL__
59 ioeGetErrorType, -- :: IOError -> IOErrorType
61 ioeGetErrorString, -- :: IOError -> String
62 ioeGetHandle, -- :: IOError -> Maybe Handle
63 ioeGetFileName, -- :: IOError -> Maybe FilePath
68 #ifdef __GLASGOW_HASKELL__
84 , isAlreadyExistsError -- :: IOError -> Bool
92 , ioeGetErrorString -- :: IOError -> String
93 , ioeGetHandle -- :: IOError -> Maybe Handle
94 , ioeGetFileName -- :: IOError -> Maybe FilePath
96 --import Data.Maybe (fromJust)
97 --import Control.Monad (MonadPlus(mplus))
100 #ifdef __GLASGOW_HASKELL__
101 -- -----------------------------------------------------------------------------
102 -- Constructing an IOError
104 mkIOError :: IOErrorType -> String -> Maybe Handle -> Maybe FilePath -> IOError
105 mkIOError t location maybe_hdl maybe_filename =
106 IOException IOError{ ioe_type = t,
107 ioe_location = location,
109 ioe_handle = maybe_hdl,
110 ioe_filename = maybe_filename
113 mkIOError EOF location maybe_hdl maybe_filename =
114 EOFError location (fromJust maybe_hdl)
115 mkIOError UserError location maybe_hdl maybe_filename =
116 UserError location ""
117 mkIOError t location maybe_hdl maybe_filename =
118 NHC.FFI.mkIOError location maybe_filename maybe_handle (ioeTypeToInt t)
120 ioeTypeToInt AlreadyExists = fromEnum EEXIST
121 ioeTypeToInt NoSuchThing = fromEnum ENOENT
122 ioeTypeToInt ResourceBusy = fromEnum EBUSY
123 ioeTypeToInt ResourceExhausted = fromEnum ENOSPC
124 ioeTypeToInt IllegalOperation = fromEnum EPERM
125 ioeTypeToInt PermissionDenied = fromEnum EACCES
128 -- -----------------------------------------------------------------------------
131 isAlreadyExistsError, isDoesNotExistError, isAlreadyInUseError,
132 isFullError, isEOFError, isIllegalOperation, isPermissionError,
133 isUserError :: IOError -> Bool
135 isAlreadyExistsError = isAlreadyExistsErrorType . ioeGetErrorType
136 isDoesNotExistError = isDoesNotExistErrorType . ioeGetErrorType
137 isAlreadyInUseError = isAlreadyInUseErrorType . ioeGetErrorType
138 isFullError = isFullErrorType . ioeGetErrorType
139 isEOFError = isEOFErrorType . ioeGetErrorType
140 isIllegalOperation = isIllegalOperationErrorType . ioeGetErrorType
141 isPermissionError = isPermissionErrorType . ioeGetErrorType
142 isUserError = isUserErrorType . ioeGetErrorType
145 -- -----------------------------------------------------------------------------
149 data IOErrorType = AlreadyExists | NoSuchThing | ResourceBusy
150 | ResourceExhausted | EOF | IllegalOperation
151 | PermissionDenied | UserError
154 #ifdef __GLASGOW_HASKELL__
155 alreadyExistsErrorType, doesNotExistErrorType, alreadyInUseErrorType,
156 fullErrorType, eofErrorType, illegalOperationErrorType,
157 permissionErrorType, userErrorType :: IOErrorType
159 alreadyExistsErrorType = AlreadyExists
160 doesNotExistErrorType = NoSuchThing
161 alreadyInUseErrorType = ResourceBusy
162 fullErrorType = ResourceExhausted
164 illegalOperationErrorType = IllegalOperation
165 permissionErrorType = PermissionDenied
166 userErrorType = UserError
169 -- -----------------------------------------------------------------------------
170 -- IOErrorType predicates
172 #ifdef __GLASGOW_HASKELL__
173 isAlreadyExistsErrorType, isDoesNotExistErrorType, isAlreadyInUseErrorType,
174 isFullErrorType, isEOFErrorType, isIllegalOperationErrorType,
175 isPermissionErrorType, isUserErrorType :: IOErrorType -> Bool
178 #ifdef __GLASGOW_HASKELL__
179 isAlreadyExistsErrorType AlreadyExists = True
180 isAlreadyExistsErrorType _ = False
182 isDoesNotExistErrorType NoSuchThing = True
183 isDoesNotExistErrorType _ = False
185 isAlreadyInUseErrorType ResourceBusy = True
186 isAlreadyInUseErrorType _ = False
188 isFullErrorType ResourceExhausted = True
189 isFullErrorType _ = False
191 isEOFErrorType EOF = True
192 isEOFErrorType _ = False
194 isIllegalOperationErrorType IllegalOperation = True
195 isIllegalOperationErrorType _ = False
197 isPermissionErrorType PermissionDenied = True
198 isPermissionErrorType _ = False
200 isUserErrorType UserError = True
201 isUserErrorType _ = False
204 -- -----------------------------------------------------------------------------
207 #ifdef __GLASGOW_HASKELL__
208 ioeGetErrorType :: IOError -> IOErrorType
209 ioeGetHandle :: IOError -> Maybe Handle
210 ioeGetErrorString :: IOError -> String
211 ioeGetFileName :: IOError -> Maybe FilePath
213 ioeGetErrorType (IOException ioe) = ioe_type ioe
214 ioeGetErrorType _ = error "System.IO.Error.ioeGetErrorType: not an IO error"
216 ioeGetHandle (IOException ioe) = ioe_handle ioe
217 ioeGetHandle _ = error "System.IO.Error.ioeGetHandle: not an IO error"
219 ioeGetErrorString (IOException ioe)
220 | isUserErrorType (ioe_type ioe) = ioe_descr ioe
221 | otherwise = show (ioe_type ioe)
222 ioeGetErrorString _ = error "System.IO.Error.ioeGetErrorString: not an IO error"
224 ioeGetFileName (IOException ioe) = ioe_filename ioe
225 ioeGetFileName _ = error "System.IO.Error.ioeGetFileName: not an IO error"
228 -- -----------------------------------------------------------------------------
229 -- annotating an IOError
231 #ifdef __GLASGOW_HASKELL__
232 annotateIOError :: IOError
237 annotateIOError (IOException (IOError hdl errTy _ str path)) loc opath ohdl =
238 IOException (IOError (hdl `mplus` ohdl) errTy loc str (path `mplus` opath))
240 Nothing `mplus` ys = ys
242 annotateIOError exc _ _ _ =
247 annotateIOError (IOError msg file hdl code) msg' file' hdl' =
248 IOError (msg++'\n':msg') (file`mplus`file') (hdl`mplus`hdl') code
249 annotateIOError (EOFError msg hdl) msg' file' hdl' =
250 EOFError (msg++'\n':msg') (hdl`mplus`hdl')
251 annotateIOError (UserError loc msg) msg' file' hdl' =
252 UserError loc (msg++'\n':msg')
253 annotateIOError (PatternError loc) msg' file' hdl' =
254 PatternError (loc++'\n':msg')