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 annotateIOError, -- :: IOError -> String -> Maybe FilePath
31 -- -> Maybe Handle -> IOError
33 alreadyExistsErrorType, -- :: IOErrorType
34 doesNotExistErrorType,
35 alreadyInUseErrorType,
38 illegalOperationErrorType,
42 isAlreadyExistsErrorType, -- :: IOErrorType -> Bool
43 isDoesNotExistErrorType,
44 isAlreadyInUseErrorType,
47 isIllegalOperationErrorType,
48 isPermissionErrorType,
50 #endif /* __GLASGOW_HASKELL__ */
52 isAlreadyExistsError, -- :: IOError -> Bool
61 #ifdef __GLASGOW_HASKELL__
62 ioeGetErrorType, -- :: IOError -> IOErrorType
64 ioeGetErrorString, -- :: IOError -> String
65 ioeGetHandle, -- :: IOError -> Maybe Handle
66 ioeGetFileName, -- :: IOError -> Maybe FilePath
71 #ifdef __GLASGOW_HASKELL__
87 , isAlreadyExistsError -- :: IOError -> Bool
95 , ioeGetErrorString -- :: IOError -> String
96 , ioeGetHandle -- :: IOError -> Maybe Handle
97 , ioeGetFileName -- :: IOError -> Maybe FilePath
99 --import Data.Maybe (fromJust)
100 --import Control.Monad (MonadPlus(mplus))
103 #ifdef __GLASGOW_HASKELL__
104 -- -----------------------------------------------------------------------------
105 -- Constructing an IOError
107 mkIOError :: IOErrorType -> String -> Maybe Handle -> Maybe FilePath -> IOError
108 mkIOError t location maybe_hdl maybe_filename =
109 IOException IOError{ ioe_type = t,
110 ioe_location = location,
112 ioe_handle = maybe_hdl,
113 ioe_filename = maybe_filename
116 mkIOError EOF location maybe_hdl maybe_filename =
117 EOFError location (fromJust maybe_hdl)
118 mkIOError UserError location maybe_hdl maybe_filename =
119 UserError location ""
120 mkIOError t location maybe_hdl maybe_filename =
121 NHC.FFI.mkIOError location maybe_filename maybe_handle (ioeTypeToInt t)
123 ioeTypeToInt AlreadyExists = fromEnum EEXIST
124 ioeTypeToInt NoSuchThing = fromEnum ENOENT
125 ioeTypeToInt ResourceBusy = fromEnum EBUSY
126 ioeTypeToInt ResourceExhausted = fromEnum ENOSPC
127 ioeTypeToInt IllegalOperation = fromEnum EPERM
128 ioeTypeToInt PermissionDenied = fromEnum EACCES
131 -- -----------------------------------------------------------------------------
134 isAlreadyExistsError, isDoesNotExistError, isAlreadyInUseError,
135 isFullError, isEOFError, isIllegalOperation, isPermissionError,
136 isUserError :: IOError -> Bool
138 isAlreadyExistsError = isAlreadyExistsErrorType . ioeGetErrorType
139 isDoesNotExistError = isDoesNotExistErrorType . ioeGetErrorType
140 isAlreadyInUseError = isAlreadyInUseErrorType . ioeGetErrorType
141 isFullError = isFullErrorType . ioeGetErrorType
142 isEOFError = isEOFErrorType . ioeGetErrorType
143 isIllegalOperation = isIllegalOperationErrorType . ioeGetErrorType
144 isPermissionError = isPermissionErrorType . ioeGetErrorType
145 isUserError = isUserErrorType . ioeGetErrorType
148 -- -----------------------------------------------------------------------------
152 data IOErrorType = AlreadyExists | NoSuchThing | ResourceBusy
153 | ResourceExhausted | EOF | IllegalOperation
154 | PermissionDenied | UserError
157 #ifdef __GLASGOW_HASKELL__
158 alreadyExistsErrorType, doesNotExistErrorType, alreadyInUseErrorType,
159 fullErrorType, eofErrorType, illegalOperationErrorType,
160 permissionErrorType, userErrorType :: IOErrorType
162 alreadyExistsErrorType = AlreadyExists
163 doesNotExistErrorType = NoSuchThing
164 alreadyInUseErrorType = ResourceBusy
165 fullErrorType = ResourceExhausted
167 illegalOperationErrorType = IllegalOperation
168 permissionErrorType = PermissionDenied
169 userErrorType = UserError
172 -- -----------------------------------------------------------------------------
173 -- IOErrorType predicates
175 #ifdef __GLASGOW_HASKELL__
176 isAlreadyExistsErrorType, isDoesNotExistErrorType, isAlreadyInUseErrorType,
177 isFullErrorType, isEOFErrorType, isIllegalOperationErrorType,
178 isPermissionErrorType, isUserErrorType :: IOErrorType -> Bool
181 #ifdef __GLASGOW_HASKELL__
182 isAlreadyExistsErrorType AlreadyExists = True
183 isAlreadyExistsErrorType _ = False
185 isDoesNotExistErrorType NoSuchThing = True
186 isDoesNotExistErrorType _ = False
188 isAlreadyInUseErrorType ResourceBusy = True
189 isAlreadyInUseErrorType _ = False
191 isFullErrorType ResourceExhausted = True
192 isFullErrorType _ = False
194 isEOFErrorType EOF = True
195 isEOFErrorType _ = False
197 isIllegalOperationErrorType IllegalOperation = True
198 isIllegalOperationErrorType _ = False
200 isPermissionErrorType PermissionDenied = True
201 isPermissionErrorType _ = False
203 isUserErrorType UserError = True
204 isUserErrorType _ = False
207 -- -----------------------------------------------------------------------------
210 #ifdef __GLASGOW_HASKELL__
211 ioeGetErrorType :: IOError -> IOErrorType
212 ioeGetHandle :: IOError -> Maybe Handle
213 ioeGetErrorString :: IOError -> String
214 ioeGetFileName :: IOError -> Maybe FilePath
216 ioeGetErrorType (IOException ioe) = ioe_type ioe
217 ioeGetErrorType _ = error "System.IO.Error.ioeGetErrorType: not an IO error"
219 ioeGetHandle (IOException ioe) = ioe_handle ioe
220 ioeGetHandle _ = error "System.IO.Error.ioeGetHandle: not an IO error"
222 ioeGetErrorString (IOException ioe)
223 | isUserErrorType (ioe_type ioe) = ioe_descr ioe
224 | otherwise = show (ioe_type ioe)
225 ioeGetErrorString _ = error "System.IO.Error.ioeGetErrorString: not an IO error"
227 ioeGetFileName (IOException ioe) = ioe_filename ioe
228 ioeGetFileName _ = error "System.IO.Error.ioeGetFileName: not an IO error"
231 -- -----------------------------------------------------------------------------
232 -- annotating an IOError
234 #ifdef __GLASGOW_HASKELL__
235 annotateIOError :: IOError
240 annotateIOError (IOException (IOError hdl errTy _ str path)) loc opath ohdl =
241 IOException (IOError (hdl `mplus` ohdl) errTy loc str (path `mplus` opath))
243 Nothing `mplus` ys = ys
245 annotateIOError exc _ _ _ =
250 annotateIOError (IOError msg file hdl code) msg' file' hdl' =
251 IOError (msg++'\n':msg') (file`mplus`file') (hdl`mplus`hdl') code
252 annotateIOError (EOFError msg hdl) msg' file' hdl' =
253 EOFError (msg++'\n':msg') (hdl`mplus`hdl')
254 annotateIOError (UserError loc msg) msg' file' hdl' =
255 UserError loc (msg++'\n':msg')
256 annotateIOError (PatternError loc) msg' file' hdl' =
257 PatternError (loc++'\n':msg')