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 (
20 IOErrorType, -- abstract
23 ioError, -- :: IOError -> IO a
24 userError, -- :: String -> IOError
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,
49 isAlreadyExistsError, -- :: IOError -> Bool
59 ioeGetErrorType, -- :: IOError -> IOErrorType
61 ioeGetErrorString, -- :: IOError -> String
62 ioeGetHandle, -- :: IOError -> Maybe Handle
63 ioeGetFileName, -- :: IOError -> Maybe FilePath
68 #ifdef __GLASGOW_HASKELL__
80 -- -----------------------------------------------------------------------------
81 -- Constructing an IOError
83 mkIOError :: IOErrorType -> String -> Maybe Handle -> Maybe FilePath -> IOError
84 mkIOError t location maybe_hdl maybe_filename =
85 IOException IOError{ ioe_type = t,
86 ioe_location = location,
88 ioe_handle = maybe_hdl,
89 ioe_filename = maybe_filename
92 -- -----------------------------------------------------------------------------
95 isAlreadyExistsError, isDoesNotExistError, isAlreadyInUseError,
96 isFullError, isEOFError, isIllegalOperation, isPermissionError,
97 isUserError :: IOError -> Bool
99 isAlreadyExistsError = isAlreadyExistsErrorType . ioeGetErrorType
100 isDoesNotExistError = isDoesNotExistErrorType . ioeGetErrorType
101 isAlreadyInUseError = isAlreadyInUseErrorType . ioeGetErrorType
102 isFullError = isFullErrorType . ioeGetErrorType
103 isEOFError = isEOFErrorType . ioeGetErrorType
104 isIllegalOperation = isIllegalOperationErrorType . ioeGetErrorType
105 isPermissionError = isPermissionErrorType . ioeGetErrorType
106 isUserError = isUserErrorType . ioeGetErrorType
109 -- -----------------------------------------------------------------------------
112 #ifdef __GLASGOW_HASKELL__
113 alreadyExistsErrorType, doesNotExistErrorType, alreadyInUseErrorType,
114 fullErrorType, eofErrorType, illegalOperationErrorType,
115 permissionErrorType, userErrorType :: IOErrorType
117 alreadyExistsErrorType = AlreadyExists
118 doesNotExistErrorType = NoSuchThing
119 alreadyInUseErrorType = ResourceBusy
120 fullErrorType = ResourceExhausted
122 illegalOperationErrorType = IllegalOperation
123 permissionErrorType = PermissionDenied
124 userErrorType = UserError
127 -- -----------------------------------------------------------------------------
128 -- IOErrorType predicates
131 isAlreadyExistsErrorType, isDoesNotExistErrorType, isAlreadyInUseErrorType,
132 isFullErrorType, isEOFErrorType, isIllegalOperationErrorType,
133 isPermissionErrorType, isUserErrorType :: IOErrorType -> Bool
136 #ifdef __GLASGOW_HASKELL__
137 isAlreadyExistsErrorType AlreadyExists = True
138 isAlreadyExistsErrorType _ = False
140 isDoesNotExistErrorType NoSuchThing = True
141 isDoesNotExistErrorType _ = False
143 isAlreadyInUseErrorType ResourceBusy = True
144 isAlreadyInUseErrorType _ = False
146 isFullErrorType ResourceExhausted = True
147 isFullErrorType _ = False
149 isEOFErrorType EOF = True
150 isEOFErrorType _ = False
152 isIllegalOperationErrorType IllegalOperation = True
153 isIllegalOperationErrorType _ = False
155 isPermissionErrorType PermissionDenied = True
156 isPermissionErrorType _ = False
158 isUserErrorType UserError = True
159 isUserErrorType _ = False
162 -- -----------------------------------------------------------------------------
165 #ifdef __GLASGOW_HASKELL__
166 ioeGetErrorType :: IOError -> IOErrorType
167 ioeGetHandle :: IOError -> Maybe Handle
168 ioeGetErrorString :: IOError -> String
169 ioeGetFileName :: IOError -> Maybe FilePath
171 ioeGetErrorType (IOException ioe) = ioe_type ioe
172 ioeGetErrorType _ = error "System.IO.Error.ioeGetHandle: not an IO error"
174 ioeGetHandle (IOException ioe) = ioe_handle ioe
175 ioeGetHandle _ = error "System.IO.Error.ioeGetHandle: not an IO error"
177 ioeGetErrorString (IOException ioe)
178 | isUserErrorType (ioe_type ioe) = show (ioe_descr ioe)
179 | otherwise = show (ioe_type ioe)
180 ioeGetErrorString _ = error "System.IO.Error.ioeGetErrorString: not an IO error"
182 ioeGetFileName (IOException ioe) = ioe_filename ioe
183 ioeGetFileName _ = error "System.IO.Error.ioeGetFileName: not an IO error"
186 -- -----------------------------------------------------------------------------
187 -- annotating an IOError
189 #ifdef __GLASGOW_HASKELL__
190 annotateIOError :: IOError
195 annotateIOError (IOException (IOError hdl errTy _ str path)) loc opath ohdl =
196 IOException (IOError (hdl `mplus` ohdl) errTy loc str (path `mplus` opath))
198 Nothing `mplus` ys = ys
200 annotateIOError exc _ _ _ =