Navigation Menu

Skip to content

Commit

Permalink
failWith: generate proper IOExceptions
Browse files Browse the repository at this point in the history
I cheated a little by mapping first to an errno, using the existing
Foreign.C.Error.errnoToIOError, and finally adding the real error
message from Win32.  To do this properly we should provide a
Win32-specific version of errnoToIOError.
  • Loading branch information
simonmar committed Aug 26, 2008
1 parent 6cf25a7 commit 5ae4ff5
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion System/Win32/Types.hs
Expand Up @@ -21,6 +21,9 @@ import Data.Maybe
import Foreign
import Foreign.C
import Numeric (showHex)
import Control.Exception
import System.IO.Error
import Data.Char

----------------------------------------------------------------
-- Platform specific definitions
Expand Down Expand Up @@ -202,7 +205,17 @@ failWith fn_name err_code = do
c_msg <- getErrorMessage err_code
msg <- peekTString c_msg
localFree c_msg
fail (fn_name ++ ": " ++ msg ++ " (error code: " ++ showHex err_code ")")
c_maperrno -- turn GetLastError() into errno, which errnoToIOError knows
-- how to convert to an IOException we can throw.
-- XXX we should really do this directly.
errno <- getErrno
let msg' = reverse $ dropWhile isSpace $ reverse msg -- drop trailing \n
ioerror = errnoToIOError fn_name errno Nothing Nothing
`ioeSetErrorString` msg'
throw ioerror

foreign import ccall unsafe "maperrno" -- in base/cbits/Win32Utils.c
c_maperrno :: IO ()

----------------------------------------------------------------
-- Misc helpers
Expand Down

0 comments on commit 5ae4ff5

Please sign in to comment.