Change the representation of IOException: add optional filename.
% -----------------------------------------------------------------------------
-% $Id: CPUTime.lhs,v 1.25 2000/09/14 13:46:42 simonpj Exp $
+% $Id: CPUTime.lhs,v 1.26 2001/01/11 07:04:16 qrczak Exp $
%
% (c) The University of Glasgow, 1995-2000
%
else
ioException (IOError Nothing UnsupportedOperation
"getCPUTime"
- "can't get CPU time")
+ "can't get CPU time"
+ Nothing)
cpuTimePrecision :: Integer
cpuTimePrecision = round ((1000000000000::Integer) %
% -----------------------------------------------------------------------------
-% $Id: Directory.lhs,v 1.20 2000/08/24 10:27:01 simonmar Exp $
+% $Id: Directory.lhs,v 1.21 2001/01/11 07:04:16 qrczak Exp $
%
% (c) The University of Glasgow, 1994-2000
%
rc <- primChmod (primPackString name) mode
if rc == 0
then return ()
- else ioException (IOError Nothing SystemError "setPermissions" "insufficient permissions")
+ else ioException (IOError Nothing SystemError
+ "setPermissions" "insufficient permissions" (Just name))
\end{code}
(Sigh)..copied from Posix.Files to avoid dep. on posix library
#else
then stToIO (unsafeFreezeByteArray bytes)
#endif
- else ioException (IOError Nothing SystemError "getFileStatus" "")
+ else ioException (IOError Nothing SystemError
+ "getFileStatus" "" (Just name))
#ifndef __HUGS__
modificationTime :: FileStatus -> IO ClockTime
% ------------------------------------------------------------------------------
-% $Id: PrelHandle.lhs,v 1.64 2001/01/10 16:28:15 qrczak Exp $
+% $Id: PrelHandle.lhs,v 1.65 2001/01/11 07:04:16 qrczak Exp $
%
% (c) The AQUA Project, Glasgow University, 1994-2000
%
_ -> do
chunk <- malloc sz_in_bytes
if chunk == nullAddr
- then ioException (IOError Nothing ResourceExhausted "mkBuffer__" "not enough virtual memory")
+ then ioException (IOError Nothing ResourceExhausted
+ "mkBuffer__" "not enough virtual memory" Nothing)
else return chunk
setBuf fo chunk sz_in_bytes
\end{code}
(IOError (Just handle)
InvalidArgument
"hSetBuffering"
- ("illegal buffer size " ++ showsPrec 9 n []))
+ ("illegal buffer size " ++ showsPrec 9 n [])
-- 9 => should be parens'ified.
+ Nothing)
_ ->
withHandle__ handle $ \ handle_ -> do
case haType__ handle_ of
ioeGetErrorString :: IOError -> String
ioeGetHandle :: IOError -> Maybe Handle
-ioeGetHandle (IOException (IOError h _ _ _)) = h
+ioeGetHandle (IOException (IOError h _ _ _ _)) = h
ioeGetHandle (UserError _) = Nothing
ioeGetHandle _ = error "IO.ioeGetHandle: not an IO error"
-ioeGetErrorString (IOException (IOError _ iot _ str)) =
+ioeGetErrorString (IOException (IOError _ iot _ str _)) =
case iot of
EOF -> "end of file"
_ -> str
ioeGetErrorString (UserError str) = str
ioeGetErrorString _ = error "IO.ioeGetErrorString: not an IO error"
-ioeGetFileName (IOException (IOError _ _ _ str)) =
- case break (== ':') str of
- (_, []) -> Nothing
- (_, _:' ':fs)-> Just fs
+ioeGetFileName (IOException (IOError _ _ _ _ fn)) = fn
ioeGetFileName (UserError _) = Nothing
ioeGetFileName _ = error "IO.ioeGetFileName: not an IO error"
\end{code}
_ -> act handle_
where
not_readable_error =
- IOError (Just handle) IllegalOperation fun
- ("handle is not open for reading")
+ IOError (Just handle) IllegalOperation fun
+ "handle is not open for reading" Nothing
wantWriteableHandle :: String -> Handle -> (Handle__ -> IO a) -> IO a
wantWriteableHandle fun handle act =
= case haType__ handle_ of
ClosedHandle -> ioe_closedHandle fun handle
SemiClosedHandle -> ioe_closedHandle fun handle
- ReadHandle -> ioError not_writeable_error
+ ReadHandle -> ioException not_writeable_error
_ -> act
where
not_writeable_error =
- IOException (IOError (Just handle) IllegalOperation fun
- ("handle is not open for writing"))
+ IOError (Just handle) IllegalOperation fun
+ "handle is not open for writing" Nothing
wantRWHandle :: String -> Handle -> (Handle__ -> IO a) -> IO a
wantRWHandle fun handle act =
\begin{code}
ioe_closedHandle :: String -> Handle -> IO a
-ioe_closedHandle fun h = ioError (IOException (IOError (Just h) IllegalOperation fun
- "handle is closed"))
+ioe_closedHandle fun h = ioException (IOError (Just h) IllegalOperation fun
+ "handle is closed" Nothing)
\end{code}
Internal helper functions for Concurrent Haskell implementation
% ------------------------------------------------------------------------------
-% $Id: PrelIO.lhs,v 1.16 2000/11/07 10:42:56 simonmar Exp $
+% $Id: PrelIO.lhs,v 1.17 2001/01/11 07:04:16 qrczak Exp $
%
% (c) The University of Glasgow, 1992-2000
%
return (handle_', str)
where
not_readable_error =
- IOError (Just handle) IllegalOperation "hGetContents"
- ("handle is not open for reading")
+ IOError (Just handle) IllegalOperation "hGetContents"
+ "handle is not open for reading" Nothing
\end{code}
Note that someone may close the semi-closed handle (or change its buffering),
% ------------------------------------------------------------------------------
-% $Id: PrelIOBase.lhs,v 1.30 2001/01/10 16:28:15 qrczak Exp $
+% $Id: PrelIOBase.lhs,v 1.31 2001/01/11 07:04:16 qrczak Exp $
%
% (c) The University of Glasgow, 1994-2000
%
malloc sz = do
a <- _malloc sz
if (a == nullAddr)
- then ioException (IOError Nothing ResourceExhausted "malloc" "")
+ then ioException (IOError Nothing ResourceExhausted
+ "malloc" "out of memory" Nothing)
else return a
foreign import "malloc" unsafe _malloc :: Int -> IO Addr
data IOException
= IOError
- (Maybe Handle) -- the handle used by the action flagging the
- -- the error.
- IOErrorType -- what it was.
- String -- location
- String -- error type specific information.
+ (Maybe Handle) -- the handle used by the action flagging the
+ -- the error.
+ IOErrorType -- what it was.
+ String -- location.
+ String -- error type specific information.
+ (Maybe FilePath) -- filename the error is related to.
instance Eq IOException where
- (IOError h1 e1 loc1 str1) == (IOError h2 e2 loc2 str2) =
- e1==e2 && str1==str2 && h1==h2 && loc1 == loc2
+ (IOError h1 e1 loc1 str1 fn1) == (IOError h2 e2 loc2 str2 fn2) =
+ e1==e2 && str1==str2 && h1==h2 && loc1==loc2 && fn1==fn2
data IOErrorType
= AlreadyExists | HardwareFault
\begin{code}
isAlreadyExistsError :: IOError -> Bool
-isAlreadyExistsError (IOException (IOError _ AlreadyExists _ _)) = True
-isAlreadyExistsError _ = False
+isAlreadyExistsError (IOException (IOError _ AlreadyExists _ _ _)) = True
+isAlreadyExistsError _ = False
isAlreadyInUseError :: IOError -> Bool
-isAlreadyInUseError (IOException (IOError _ ResourceBusy _ _)) = True
-isAlreadyInUseError _ = False
+isAlreadyInUseError (IOException (IOError _ ResourceBusy _ _ _)) = True
+isAlreadyInUseError _ = False
isFullError :: IOError -> Bool
-isFullError (IOException (IOError _ ResourceExhausted _ _)) = True
-isFullError _ = False
+isFullError (IOException (IOError _ ResourceExhausted _ _ _)) = True
+isFullError _ = False
isEOFError :: IOError -> Bool
-isEOFError (IOException (IOError _ EOF _ _)) = True
-isEOFError _ = False
+isEOFError (IOException (IOError _ EOF _ _ _)) = True
+isEOFError _ = False
isIllegalOperation :: IOError -> Bool
-isIllegalOperation (IOException (IOError _ IllegalOperation _ _)) = True
-isIllegalOperation _ = False
+isIllegalOperation (IOException (IOError _ IllegalOperation _ _ _)) = True
+isIllegalOperation _ = False
isPermissionError :: IOError -> Bool
-isPermissionError (IOException (IOError _ PermissionDenied _ _)) = True
-isPermissionError _ = False
+isPermissionError (IOException (IOError _ PermissionDenied _ _ _)) = True
+isPermissionError _ = False
isDoesNotExistError :: IOError -> Bool
-isDoesNotExistError (IOException (IOError _ NoSuchThing _ _)) = True
-isDoesNotExistError _ = False
+isDoesNotExistError (IOException (IOError _ NoSuchThing _ _ _)) = True
+isDoesNotExistError _ = False
isUserError :: IOError -> Bool
isUserError (UserError _) = True
-isUserError _ = False
+isUserError _ = False
\end{code}
Showing @IOError@s
-- For now we give a fairly uninformative error message which just happens to
-- be like the ones that Hugs used to give.
instance Show IOException where
- showsPrec p (IOError hdl iot loc s) = showString s . showChar '\n'
+ showsPrec p (IOError _ _ _ s _) = showString s . showChar '\n'
#else
instance Show IOException where
- showsPrec p (IOError hdl iot loc s) =
+ showsPrec p (IOError hdl iot loc s fn) =
showsPrec p iot .
- showChar '\n' .
(case loc of
"" -> id
- _ -> showString "Action: " . showString loc . showChar '\n') .
+ _ -> showString "\nAction: " . showString loc) .
showHdl .
(case s of
"" -> id
- _ -> showString "Reason: " . showString s)
+ _ -> showString "\nReason: " . showString s) .
+ (case fn of
+ Nothing -> id
+ Just name -> showString "\nFile: " . showString name)
where
showHdl =
case hdl of
Nothing -> id
- Just h -> showString "Handle: " . showsPrec p h
+ Just h -> showString "\nHandle: " . showsPrec p h
#endif
\end{code}
ioError (IOException io_error)
constructErrorAndFailWithInfo :: String -> String -> IO a
-constructErrorAndFailWithInfo call_site reason
- = constructErrorMsg call_site (Just reason) >>= \ io_error ->
+constructErrorAndFailWithInfo call_site fn
+ = constructErrorMsg call_site (Just fn) >>= \ io_error ->
ioError (IOException io_error)
\end{code}
constructError call_site = constructErrorMsg call_site Nothing
constructErrorMsg :: String -> Maybe String -> IO IOException
-constructErrorMsg call_site reason =
+constructErrorMsg call_site fn =
getErrType__ >>= \ errtype ->
getErrStr__ >>= \ str ->
let
unpackCString str ++
(case iot of
OtherError -> "(error code: " ++ show errtype ++ ")"
- _ -> "") ++
- (case reason of
- Nothing -> ""
- Just m -> ": "++m)
+ _ -> "")
in
- return (IOError Nothing iot call_site msg)
+ return (IOError Nothing iot call_site msg fn)
\end{code}
% -----------------------------------------------------------------------------
-% $Id: System.lhs,v 1.26 2000/07/07 11:03:58 simonmar Exp $
+% $Id: System.lhs,v 1.27 2001/01/11 07:04:16 qrczak Exp $
%
% (c) The University of Glasgow, 1994-2000
%
if litstring /= nullAddr
then primUnpackCString litstring
else ioException (IOError Nothing NoSuchThing "getEnv"
- ("environment variable: " ++ name))
+ "no environment variable" (Just name))
foreign import ccall "libHS_cbits.so" "getenv" unsafe primGetEnv :: PrimByteArray -> IO Addr
\end{code}
\begin{code}
system :: String -> IO ExitCode
-system "" = ioException (IOError Nothing InvalidArgument "system" "null command")
+system "" = ioException (IOError Nothing InvalidArgument "system" "null command" Nothing)
system cmd = do
status <- primSystem (primPackString cmd)
case status of
exitWith :: ExitCode -> IO a
exitWith ExitSuccess = do
primExit 0
- ioException (IOError Nothing OtherError "exitWith" "exit should not return")
+ ioException (IOError Nothing OtherError "exitWith" "exit should not return" Nothing)
exitWith (ExitFailure n)
- | n == 0 = ioException (IOError Nothing InvalidArgument "exitWith" "ExitFailure 0")
+ | n == 0 = ioException (IOError Nothing InvalidArgument "exitWith" "ExitFailure 0" Nothing)
| otherwise = do
primExit n
- ioException (IOError Nothing OtherError "exitWith" "exit should not return")
+ ioException (IOError Nothing OtherError "exitWith" "exit should not return" Nothing)
-- NOTE: shutdownHaskellAndExit must be called "safe", because it *can*
-- re-enter Haskell land through finalizers.