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 -- $Id: Error.hs,v 1.4 2002/04/24 16:31:45 simonmar Exp $
15 -- Standard IO Errors.
17 -----------------------------------------------------------------------------
19 module System.IO.Error (
21 IOErrorType, -- abstract
23 ioError, -- :: IOError -> IO a
24 userError, -- :: String -> IOError
26 mkIOError, -- :: IOErrorType -> String -> Maybe Handle
27 -- -> Maybe FilePath -> IOError
29 alreadyExistsErrorType, -- :: IOErrorType
30 doesNotExistErrorType,
31 alreadyInUseErrorType,
34 illegalOperationErrorType,
38 isAlreadyExistsErrorType, -- :: IOErrorType -> Bool
39 isDoesNotExistErrorType,
40 isAlreadyInUseErrorType,
43 isIllegalOperationErrorType,
44 isPermissionErrorType,
47 isAlreadyExistsError, -- :: IOError -> Bool
56 ioeGetErrorType, -- :: IOError -> IOErrorType
57 ioeGetErrorString, -- :: IOError -> String
58 ioeGetHandle, -- :: IOError -> Maybe Handle
59 ioeGetFileName, -- :: IOError -> Maybe FilePath
64 #ifdef __GLASGOW_HASKELL__
71 -- -----------------------------------------------------------------------------
72 -- Constructing an IOError
74 mkIOError :: IOErrorType -> String -> Maybe Handle -> Maybe FilePath -> IOError
75 mkIOError t location maybe_hdl maybe_filename =
76 IOException IOError{ ioe_type = t,
77 ioe_location = location,
79 ioe_handle = maybe_hdl,
80 ioe_filename = maybe_filename
83 -- -----------------------------------------------------------------------------
86 isAlreadyExistsError, isDoesNotExistError, isAlreadyInUseError,
87 isFullError, isEOFError, isIllegalOperation, isPermissionError,
88 isUserError :: IOError -> Bool
90 isAlreadyExistsError = isAlreadyExistsErrorType . ioeGetErrorType
91 isDoesNotExistError = isDoesNotExistErrorType . ioeGetErrorType
92 isAlreadyInUseError = isAlreadyInUseErrorType . ioeGetErrorType
93 isFullError = isFullErrorType . ioeGetErrorType
94 isEOFError = isEOFErrorType . ioeGetErrorType
95 isIllegalOperation = isIllegalOperationErrorType . ioeGetErrorType
96 isPermissionError = isPermissionErrorType . ioeGetErrorType
97 isUserError = isUserErrorType . ioeGetErrorType
99 -- -----------------------------------------------------------------------------
102 #ifdef __GLASGOW_HASKELL__
103 alreadyExistsErrorType, doesNotExistErrorType, alreadyInUseErrorType,
104 fullErrorType, eofErrorType, illegalOperationErrorType,
105 permissionErrorType, userErrorType :: IOErrorType
107 alreadyExistsErrorType = AlreadyExists
108 doesNotExistErrorType = NoSuchThing
109 alreadyInUseErrorType = ResourceBusy
110 fullErrorType = ResourceExhausted
112 illegalOperationErrorType = IllegalOperation
113 permissionErrorType = PermissionDenied
114 userErrorType = UserError
117 -- -----------------------------------------------------------------------------
118 -- IOErrorType predicates
120 isAlreadyExistsErrorType, isDoesNotExistErrorType, isAlreadyInUseErrorType,
121 isFullErrorType, isEOFErrorType, isIllegalOperationErrorType,
122 isPermissionErrorType, isUserErrorType :: IOErrorType -> Bool
124 #ifdef __GLASGOW_HASKELL__
125 isAlreadyExistsErrorType AlreadyExists = True
126 isAlreadyExistsErrorType _ = False
128 isDoesNotExistErrorType NoSuchThing = True
129 isDoesNotExistErrorType _ = False
131 isAlreadyInUseErrorType ResourceBusy = True
132 isAlreadyInUseErrorType _ = False
134 isFullErrorType ResourceExhausted = True
135 isFullErrorType _ = False
137 isEOFErrorType EOF = True
138 isEOFErrorType _ = False
140 isIllegalOperationErrorType IllegalOperation = True
141 isIllegalOperationErrorType _ = False
143 isPermissionErrorType PermissionDenied = True
144 isPermissionErrorType _ = False
146 isUserErrorType UserError = True
147 isUserErrorType _ = False
150 -- -----------------------------------------------------------------------------
153 #ifdef __GLASGOW_HASKELL__
154 ioeGetErrorType :: IOError -> IOErrorType
155 ioeGetHandle :: IOError -> Maybe Handle
156 ioeGetErrorString :: IOError -> String
157 ioeGetFileName :: IOError -> Maybe FilePath
159 ioeGetErrorType (IOException ioe) = ioe_type ioe
160 ioeGetErrorType _ = error "System.IO.Error.ioeGetHandle: not an IO error"
162 ioeGetHandle (IOException ioe) = ioe_handle ioe
163 ioeGetHandle _ = error "System.IO.Error.ioeGetHandle: not an IO error"
165 ioeGetErrorString (IOException ioe)
166 | isUserErrorType (ioe_type ioe) = show (ioe_descr ioe)
167 | otherwise = show (ioe_type ioe)
168 ioeGetErrorString _ = error "System.IO.Error.ioeGetErrorString: not an IO error"
170 ioeGetFileName (IOException ioe) = ioe_filename ioe
171 ioeGetFileName _ = error "System.IO.Error.ioeGetFileName: not an IO error"
174 -- -----------------------------------------------------------------------------
175 -- annotating an IOError
177 #ifdef __GLASGOW_HASKELL__
178 annotateIOError :: IOError
183 annotateIOError (IOException (IOError hdl errTy _ str path)) loc opath ohdl =
184 IOException (IOError (hdl `mplus` ohdl) errTy loc str (path `mplus` opath))
186 Nothing `mplus` ys = ys
188 annotateIOError exc _ _ _ =