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