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/core/LICENSE)
9 -- Maintainer : libraries@haskell.org
10 -- Stability : provisional
11 -- Portability : portable
13 -- Standard IO Errors.
15 -----------------------------------------------------------------------------
17 module System.IO.Error (
19 IOErrorType, -- abstract
21 ioError, -- :: IOError -> IO a
22 userError, -- :: String -> IOError
24 mkIOError, -- :: IOErrorType -> String -> Maybe Handle
25 -- -> Maybe FilePath -> IOError
27 alreadyExistsErrorType, -- :: IOErrorType
28 doesNotExistErrorType,
29 alreadyInUseErrorType,
32 illegalOperationErrorType,
36 isAlreadyExistsErrorType, -- :: IOErrorType -> Bool
37 isDoesNotExistErrorType,
38 isAlreadyInUseErrorType,
41 isIllegalOperationErrorType,
42 isPermissionErrorType,
45 isAlreadyExistsError, -- :: IOError -> Bool
54 ioeGetErrorType, -- :: IOError -> IOErrorType
55 ioeGetErrorString, -- :: IOError -> String
56 ioeGetHandle, -- :: IOError -> Maybe Handle
57 ioeGetFileName, -- :: IOError -> Maybe FilePath
62 #ifdef __GLASGOW_HASKELL__
69 -- -----------------------------------------------------------------------------
70 -- Constructing an IOError
72 mkIOError :: IOErrorType -> String -> Maybe Handle -> Maybe FilePath -> IOError
73 mkIOError t location maybe_hdl maybe_filename =
74 IOException IOError{ ioe_type = t,
75 ioe_location = location,
77 ioe_handle = maybe_hdl,
78 ioe_filename = maybe_filename
81 -- -----------------------------------------------------------------------------
84 isAlreadyExistsError, isDoesNotExistError, isAlreadyInUseError,
85 isFullError, isEOFError, isIllegalOperation, isPermissionError,
86 isUserError :: IOError -> Bool
88 isAlreadyExistsError = isAlreadyExistsErrorType . ioeGetErrorType
89 isDoesNotExistError = isDoesNotExistErrorType . ioeGetErrorType
90 isAlreadyInUseError = isAlreadyInUseErrorType . ioeGetErrorType
91 isFullError = isFullErrorType . ioeGetErrorType
92 isEOFError = isEOFErrorType . ioeGetErrorType
93 isIllegalOperation = isIllegalOperationErrorType . ioeGetErrorType
94 isPermissionError = isPermissionErrorType . ioeGetErrorType
95 isUserError = isUserErrorType . ioeGetErrorType
97 -- -----------------------------------------------------------------------------
100 #ifdef __GLASGOW_HASKELL__
101 alreadyExistsErrorType, doesNotExistErrorType, alreadyInUseErrorType,
102 fullErrorType, eofErrorType, illegalOperationErrorType,
103 permissionErrorType, userErrorType :: IOErrorType
105 alreadyExistsErrorType = AlreadyExists
106 doesNotExistErrorType = NoSuchThing
107 alreadyInUseErrorType = ResourceBusy
108 fullErrorType = ResourceExhausted
110 illegalOperationErrorType = IllegalOperation
111 permissionErrorType = PermissionDenied
112 userErrorType = UserError
115 -- -----------------------------------------------------------------------------
116 -- IOErrorType predicates
118 isAlreadyExistsErrorType, isDoesNotExistErrorType, isAlreadyInUseErrorType,
119 isFullErrorType, isEOFErrorType, isIllegalOperationErrorType,
120 isPermissionErrorType, isUserErrorType :: IOErrorType -> Bool
122 #ifdef __GLASGOW_HASKELL__
123 isAlreadyExistsErrorType AlreadyExists = True
124 isAlreadyExistsErrorType _ = False
126 isDoesNotExistErrorType NoSuchThing = True
127 isDoesNotExistErrorType _ = False
129 isAlreadyInUseErrorType ResourceBusy = True
130 isAlreadyInUseErrorType _ = False
132 isFullErrorType ResourceExhausted = True
133 isFullErrorType _ = False
135 isEOFErrorType EOF = True
136 isEOFErrorType _ = False
138 isIllegalOperationErrorType IllegalOperation = True
139 isIllegalOperationErrorType _ = False
141 isPermissionErrorType PermissionDenied = True
142 isPermissionErrorType _ = False
144 isUserErrorType UserError = True
145 isUserErrorType _ = False
148 -- -----------------------------------------------------------------------------
151 #ifdef __GLASGOW_HASKELL__
152 ioeGetErrorType :: IOError -> IOErrorType
153 ioeGetHandle :: IOError -> Maybe Handle
154 ioeGetErrorString :: IOError -> String
155 ioeGetFileName :: IOError -> Maybe FilePath
157 ioeGetErrorType (IOException ioe) = ioe_type ioe
158 ioeGetErrorType _ = error "System.IO.Error.ioeGetHandle: not an IO error"
160 ioeGetHandle (IOException ioe) = ioe_handle ioe
161 ioeGetHandle _ = error "System.IO.Error.ioeGetHandle: not an IO error"
163 ioeGetErrorString (IOException ioe)
164 | isUserErrorType (ioe_type ioe) = show (ioe_descr ioe)
165 | otherwise = show (ioe_type ioe)
166 ioeGetErrorString _ = error "System.IO.Error.ioeGetErrorString: not an IO error"
168 ioeGetFileName (IOException ioe) = ioe_filename ioe
169 ioeGetFileName _ = error "System.IO.Error.ioeGetFileName: not an IO error"
172 -- -----------------------------------------------------------------------------
173 -- annotating an IOError
175 #ifdef __GLASGOW_HASKELL__
176 annotateIOError :: IOError
181 annotateIOError (IOException (IOError hdl errTy _ str path)) loc opath ohdl =
182 IOException (IOError (hdl `mplus` ohdl) errTy loc str (path `mplus` opath))
184 Nothing `mplus` ys = ys
186 annotateIOError exc _ _ _ =