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.1 2002/02/05 17:32:27 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 #ifdef __GLASGOW_HASKELL__
121 isAlreadyExistsErrorType AlreadyExists = True
122 isAlreadyExistsErrorType _ = False
124 isDoesNotExistErrorType NoSuchThing = True
125 isDoesNotExistErrorType _ = False
127 isAlreadyInUseErrorType ResourceBusy = True
128 isAlreadyInUseErrorType _ = False
130 isFullErrorType ResourceExhausted = True
131 isFullErrorType _ = False
133 isEOFErrorType EOF = True
134 isEOFErrorType _ = False
136 isIllegalOperationErrorType IllegalOperation = True
137 isIllegalOperationErrorType _ = False
139 isPermissionErrorType PermissionDenied = True
140 isPermissionErrorType _ = False
142 isUserErrorType UserError = True
143 isUserErrorType _ = False
146 -- -----------------------------------------------------------------------------
149 #ifdef __GLASGOW_HASKELL__
150 ioeGetErrorType :: IOError -> IOErrorType
151 ioeGetFileName :: IOError -> Maybe FilePath
152 ioeGetErrorString :: IOError -> String
153 ioeGetHandle :: IOError -> Maybe Handle
155 ioeGetErrorType (IOException ioe) = ioe_type ioe
156 ioeGetHandle _ = error "System.IO.Error.ioeGetHandle: not an IO error"
158 ioeGetHandle (IOException ioe) = ioe_handle ioe
159 ioeGetHandle _ = error "System.IO.Error.ioeGetHandle: not an IO error"
161 ioeGetErrorString (IOException ioe)
162 | isUserErrorType (ioe_type ioe) = show (ioe_descr ioe)
163 | otherwise = show (ioe_type ioe)
164 ioeGetErrorString _ = error "System.IO.Error.ioeGetErrorString: not an IO error"
166 ioeGetFileName (IOException ioe) = ioe_filename ioe
167 ioeGetFileName _ = error "System.IO.Error.ioeGetFileName: not an IO error"
170 -- -----------------------------------------------------------------------------
171 -- annotating an IOError
173 #ifdef __GLASGOW_HASKELL__
174 annotateIOError :: IOError
179 annotateIOError (IOException (IOError hdl errTy _ str path)) loc opath ohdl =
180 IOException (IOError (hdl `mplus` ohdl) errTy loc str (path `mplus` opath))
182 Nothing `mplus` ys = ys
184 annotateIOError exc _ _ _ =