[project @ 2002-04-26 13:34:05 by simonmar]
[packages/old-locale.git] / System / IO / Error.hs
1 {-# OPTIONS -fno-implicit-prelude #-}
2
3 -----------------------------------------------------------------------------
4 -- |
5 -- Module : System.IO.Error
6 -- Copyright : (c) The University of Glasgow 2001
7 -- License : BSD-style (see the file libraries/core/LICENSE)
8 --
9 -- Maintainer : libraries@haskell.org
10 -- Stability : provisional
11 -- Portability : portable
12 --
13 -- Standard IO Errors.
14 --
15 -----------------------------------------------------------------------------
16
17 module System.IO.Error (
18 IOError, -- abstract
19 IOErrorType, -- abstract
20
21 ioError, -- :: IOError -> IO a
22 userError, -- :: String -> IOError
23
24 mkIOError, -- :: IOErrorType -> String -> Maybe Handle
25 -- -> Maybe FilePath -> IOError
26
27 alreadyExistsErrorType, -- :: IOErrorType
28 doesNotExistErrorType,
29 alreadyInUseErrorType,
30 fullErrorType,
31 eofErrorType,
32 illegalOperationErrorType,
33 permissionErrorType,
34 userErrorType,
35
36 isAlreadyExistsErrorType, -- :: IOErrorType -> Bool
37 isDoesNotExistErrorType,
38 isAlreadyInUseErrorType,
39 isFullErrorType,
40 isEOFErrorType,
41 isIllegalOperationErrorType,
42 isPermissionErrorType,
43 isUserErrorType,
44
45 isAlreadyExistsError, -- :: IOError -> Bool
46 isDoesNotExistError,
47 isAlreadyInUseError,
48 isFullError,
49 isEOFError,
50 isIllegalOperation,
51 isPermissionError,
52 isUserError,
53
54 ioeGetErrorType, -- :: IOError -> IOErrorType
55 ioeGetErrorString, -- :: IOError -> String
56 ioeGetHandle, -- :: IOError -> Maybe Handle
57 ioeGetFileName, -- :: IOError -> Maybe FilePath
58
59 ) where
60
61
62 #ifdef __GLASGOW_HASKELL__
63 import GHC.Base
64 import Data.Maybe
65 import GHC.IOBase
66 import Text.Show
67 #endif
68
69 -- -----------------------------------------------------------------------------
70 -- Constructing an IOError
71
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,
76 ioe_descr = "",
77 ioe_handle = maybe_hdl,
78 ioe_filename = maybe_filename
79 }
80
81 -- -----------------------------------------------------------------------------
82 -- IOErrorType
83
84 isAlreadyExistsError, isDoesNotExistError, isAlreadyInUseError,
85 isFullError, isEOFError, isIllegalOperation, isPermissionError,
86 isUserError :: IOError -> Bool
87
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
96
97 -- -----------------------------------------------------------------------------
98 -- IOErrorTypes
99
100 #ifdef __GLASGOW_HASKELL__
101 alreadyExistsErrorType, doesNotExistErrorType, alreadyInUseErrorType,
102 fullErrorType, eofErrorType, illegalOperationErrorType,
103 permissionErrorType, userErrorType :: IOErrorType
104
105 alreadyExistsErrorType = AlreadyExists
106 doesNotExistErrorType = NoSuchThing
107 alreadyInUseErrorType = ResourceBusy
108 fullErrorType = ResourceExhausted
109 eofErrorType = EOF
110 illegalOperationErrorType = IllegalOperation
111 permissionErrorType = PermissionDenied
112 userErrorType = UserError
113 #endif
114
115 -- -----------------------------------------------------------------------------
116 -- IOErrorType predicates
117
118 isAlreadyExistsErrorType, isDoesNotExistErrorType, isAlreadyInUseErrorType,
119 isFullErrorType, isEOFErrorType, isIllegalOperationErrorType,
120 isPermissionErrorType, isUserErrorType :: IOErrorType -> Bool
121
122 #ifdef __GLASGOW_HASKELL__
123 isAlreadyExistsErrorType AlreadyExists = True
124 isAlreadyExistsErrorType _ = False
125
126 isDoesNotExistErrorType NoSuchThing = True
127 isDoesNotExistErrorType _ = False
128
129 isAlreadyInUseErrorType ResourceBusy = True
130 isAlreadyInUseErrorType _ = False
131
132 isFullErrorType ResourceExhausted = True
133 isFullErrorType _ = False
134
135 isEOFErrorType EOF = True
136 isEOFErrorType _ = False
137
138 isIllegalOperationErrorType IllegalOperation = True
139 isIllegalOperationErrorType _ = False
140
141 isPermissionErrorType PermissionDenied = True
142 isPermissionErrorType _ = False
143
144 isUserErrorType UserError = True
145 isUserErrorType _ = False
146 #endif
147
148 -- -----------------------------------------------------------------------------
149 -- Miscellaneous
150
151 #ifdef __GLASGOW_HASKELL__
152 ioeGetErrorType :: IOError -> IOErrorType
153 ioeGetHandle :: IOError -> Maybe Handle
154 ioeGetErrorString :: IOError -> String
155 ioeGetFileName :: IOError -> Maybe FilePath
156
157 ioeGetErrorType (IOException ioe) = ioe_type ioe
158 ioeGetErrorType _ = error "System.IO.Error.ioeGetHandle: not an IO error"
159
160 ioeGetHandle (IOException ioe) = ioe_handle ioe
161 ioeGetHandle _ = error "System.IO.Error.ioeGetHandle: not an IO error"
162
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"
167
168 ioeGetFileName (IOException ioe) = ioe_filename ioe
169 ioeGetFileName _ = error "System.IO.Error.ioeGetFileName: not an IO error"
170 #endif
171
172 -- -----------------------------------------------------------------------------
173 -- annotating an IOError
174
175 #ifdef __GLASGOW_HASKELL__
176 annotateIOError :: IOError
177 -> String
178 -> Maybe FilePath
179 -> Maybe Handle
180 -> IOError
181 annotateIOError (IOException (IOError hdl errTy _ str path)) loc opath ohdl =
182 IOException (IOError (hdl `mplus` ohdl) errTy loc str (path `mplus` opath))
183 where
184 Nothing `mplus` ys = ys
185 xs `mplus` _ = xs
186 annotateIOError exc _ _ _ =
187 exc
188 #endif