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 catch, -- :: IO a -> (IOError -> IO a) -> IO a
24 try, -- :: IO a -> IO (Either IOError a)
26 ioError, -- :: IOError -> IO a
27 userError, -- :: String -> IOError
29 #ifdef __GLASGOW_HASKELL__
30 mkIOError, -- :: IOErrorType -> String -> Maybe Handle
31 -- -> Maybe FilePath -> IOError
33 annotateIOError, -- :: IOError -> String -> Maybe FilePath
34 -- -> Maybe Handle -> IOError
36 alreadyExistsErrorType, -- :: IOErrorType
37 doesNotExistErrorType,
38 alreadyInUseErrorType,
41 illegalOperationErrorType,
45 isAlreadyExistsErrorType, -- :: IOErrorType -> Bool
46 isDoesNotExistErrorType,
47 isAlreadyInUseErrorType,
50 isIllegalOperationErrorType,
51 isPermissionErrorType,
53 #endif /* __GLASGOW_HASKELL__ */
55 isAlreadyExistsError, -- :: IOError -> Bool
64 #ifdef __GLASGOW_HASKELL__
65 ioeGetErrorType, -- :: IOError -> IOErrorType
67 ioeGetErrorString, -- :: IOError -> String
68 ioeGetHandle, -- :: IOError -> Maybe Handle
69 ioeGetFileName, -- :: IOError -> Maybe FilePath
75 #ifdef __GLASGOW_HASKELL__
93 , isAlreadyExistsError -- :: IOError -> Bool
101 , ioeGetErrorString -- :: IOError -> String
102 , ioeGetHandle -- :: IOError -> Maybe Handle
103 , ioeGetFileName -- :: IOError -> Maybe FilePath
105 --import Data.Maybe (fromJust)
106 --import Control.Monad (MonadPlus(mplus))
109 -- | The construct @try comp@ exposes IO errors which occur within a
110 -- computation, and which are not fully handled.
111 -- Other exceptions are not caught by this variant;
112 -- to catch all exceptions, use @try@ from "Control.Exception".
115 try :: IO a -> IO (Either IOError a)
116 try f = catch (do r <- f
121 #ifdef __GLASGOW_HASKELL__
122 -- -----------------------------------------------------------------------------
123 -- Constructing an IOError
125 mkIOError :: IOErrorType -> String -> Maybe Handle -> Maybe FilePath -> IOError
126 mkIOError t location maybe_hdl maybe_filename =
127 IOError{ ioe_type = t,
128 ioe_location = location,
130 ioe_handle = maybe_hdl,
131 ioe_filename = maybe_filename
134 mkIOError EOF location maybe_hdl maybe_filename =
135 EOFError location (fromJust maybe_hdl)
136 mkIOError UserError location maybe_hdl maybe_filename =
137 UserError location ""
138 mkIOError t location maybe_hdl maybe_filename =
139 NHC.FFI.mkIOError location maybe_filename maybe_handle (ioeTypeToInt t)
141 ioeTypeToInt AlreadyExists = fromEnum EEXIST
142 ioeTypeToInt NoSuchThing = fromEnum ENOENT
143 ioeTypeToInt ResourceBusy = fromEnum EBUSY
144 ioeTypeToInt ResourceExhausted = fromEnum ENOSPC
145 ioeTypeToInt IllegalOperation = fromEnum EPERM
146 ioeTypeToInt PermissionDenied = fromEnum EACCES
149 -- -----------------------------------------------------------------------------
152 isAlreadyExistsError, isDoesNotExistError, isAlreadyInUseError,
153 isFullError, isEOFError, isIllegalOperation, isPermissionError,
154 isUserError :: IOError -> Bool
156 isAlreadyExistsError = isAlreadyExistsErrorType . ioeGetErrorType
157 isDoesNotExistError = isDoesNotExistErrorType . ioeGetErrorType
158 isAlreadyInUseError = isAlreadyInUseErrorType . ioeGetErrorType
159 isFullError = isFullErrorType . ioeGetErrorType
160 isEOFError = isEOFErrorType . ioeGetErrorType
161 isIllegalOperation = isIllegalOperationErrorType . ioeGetErrorType
162 isPermissionError = isPermissionErrorType . ioeGetErrorType
163 isUserError = isUserErrorType . ioeGetErrorType
166 -- -----------------------------------------------------------------------------
170 data IOErrorType = AlreadyExists | NoSuchThing | ResourceBusy
171 | ResourceExhausted | EOF | IllegalOperation
172 | PermissionDenied | UserError
175 #ifdef __GLASGOW_HASKELL__
176 alreadyExistsErrorType, doesNotExistErrorType, alreadyInUseErrorType,
177 fullErrorType, eofErrorType, illegalOperationErrorType,
178 permissionErrorType, userErrorType :: IOErrorType
180 alreadyExistsErrorType = AlreadyExists
181 doesNotExistErrorType = NoSuchThing
182 alreadyInUseErrorType = ResourceBusy
183 fullErrorType = ResourceExhausted
185 illegalOperationErrorType = IllegalOperation
186 permissionErrorType = PermissionDenied
187 userErrorType = UserError
190 -- -----------------------------------------------------------------------------
191 -- IOErrorType predicates
193 #ifdef __GLASGOW_HASKELL__
194 isAlreadyExistsErrorType, isDoesNotExistErrorType, isAlreadyInUseErrorType,
195 isFullErrorType, isEOFErrorType, isIllegalOperationErrorType,
196 isPermissionErrorType, isUserErrorType :: IOErrorType -> Bool
199 #ifdef __GLASGOW_HASKELL__
200 isAlreadyExistsErrorType AlreadyExists = True
201 isAlreadyExistsErrorType _ = False
203 isDoesNotExistErrorType NoSuchThing = True
204 isDoesNotExistErrorType _ = False
206 isAlreadyInUseErrorType ResourceBusy = True
207 isAlreadyInUseErrorType _ = False
209 isFullErrorType ResourceExhausted = True
210 isFullErrorType _ = False
212 isEOFErrorType EOF = True
213 isEOFErrorType _ = False
215 isIllegalOperationErrorType IllegalOperation = True
216 isIllegalOperationErrorType _ = False
218 isPermissionErrorType PermissionDenied = True
219 isPermissionErrorType _ = False
221 isUserErrorType UserError = True
222 isUserErrorType _ = False
225 -- -----------------------------------------------------------------------------
228 #ifdef __GLASGOW_HASKELL__
229 ioeGetErrorType :: IOError -> IOErrorType
230 ioeGetHandle :: IOError -> Maybe Handle
231 ioeGetErrorString :: IOError -> String
232 ioeGetFileName :: IOError -> Maybe FilePath
234 ioeGetErrorType ioe = ioe_type ioe
236 ioeGetHandle ioe = ioe_handle ioe
238 ioeGetErrorString ioe
239 | isUserErrorType (ioe_type ioe) = ioe_descr ioe
240 | otherwise = show (ioe_type ioe)
242 ioeGetFileName ioe = ioe_filename ioe
245 -- -----------------------------------------------------------------------------
246 -- annotating an IOError
248 #ifdef __GLASGOW_HASKELL__
249 annotateIOError :: IOError
254 annotateIOError (IOError hdl errTy _ str path) loc opath ohdl =
255 IOError (hdl `mplus` ohdl) errTy loc str (path `mplus` opath)
257 Nothing `mplus` ys = ys
262 annotateIOError (IOError msg file hdl code) msg' file' hdl' =
263 IOError (msg++'\n':msg') (file`mplus`file') (hdl`mplus`hdl') code
264 annotateIOError (EOFError msg hdl) msg' file' hdl' =
265 EOFError (msg++'\n':msg') (hdl`mplus`hdl')
266 annotateIOError (UserError loc msg) msg' file' hdl' =
267 UserError loc (msg++'\n':msg')
268 annotateIOError (PatternError loc) msg' file' hdl' =
269 PatternError (loc++'\n':msg')