[project @ 2002-02-05 17:32:24 by simonmar]
[haskell-directory.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.1 2002/02/05 17:32:27 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 #ifdef __GLASGOW_HASKELL__
121 isAlreadyExistsErrorType AlreadyExists = True
122 isAlreadyExistsErrorType _ = False
123
124 isDoesNotExistErrorType NoSuchThing = True
125 isDoesNotExistErrorType _ = False
126
127 isAlreadyInUseErrorType ResourceBusy = True
128 isAlreadyInUseErrorType _ = False
129
130 isFullErrorType ResourceExhausted = True
131 isFullErrorType _ = False
132
133 isEOFErrorType EOF = True
134 isEOFErrorType _ = False
135
136 isIllegalOperationErrorType IllegalOperation = True
137 isIllegalOperationErrorType _ = False
138
139 isPermissionErrorType PermissionDenied = True
140 isPermissionErrorType _ = False
141
142 isUserErrorType UserError = True
143 isUserErrorType _ = False
144 #endif
145
146 -- -----------------------------------------------------------------------------
147 -- Miscellaneous
148
149 #ifdef __GLASGOW_HASKELL__
150 ioeGetErrorType       :: IOError -> IOErrorType
151 ioeGetFileName        :: IOError -> Maybe FilePath
152 ioeGetErrorString     :: IOError -> String
153 ioeGetHandle          :: IOError -> Maybe Handle
154
155 ioeGetErrorType (IOException ioe) = ioe_type ioe
156 ioeGetHandle _ = error "System.IO.Error.ioeGetHandle: not an IO error"
157
158 ioeGetHandle (IOException ioe) = ioe_handle ioe
159 ioeGetHandle _ = error "System.IO.Error.ioeGetHandle: not an IO error"
160
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"
165
166 ioeGetFileName (IOException ioe) = ioe_filename ioe
167 ioeGetFileName _ = error "System.IO.Error.ioeGetFileName: not an IO error"
168 #endif
169
170 -- -----------------------------------------------------------------------------
171 -- annotating an IOError
172
173 #ifdef __GLASGOW_HASKELL__
174 annotateIOError :: IOError 
175               -> String 
176               -> Maybe FilePath 
177               -> Maybe Handle 
178               -> IOError 
179 annotateIOError (IOException (IOError hdl errTy _ str path)) loc opath ohdl = 
180   IOException (IOError (hdl `mplus` ohdl) errTy loc str (path `mplus` opath))
181   where
182     Nothing `mplus` ys = ys
183     xs      `mplus` _  = xs
184 annotateIOError exc _ _ _ = 
185   exc
186 #endif