Skip to content
Browse files

failWith: generate proper IOExceptions

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...
1 parent 6cf25a7 commit 5ae4ff5ba38ef85f6de97711c82d7e84832e5f5b @simonmar simonmar committed Aug 26, 2008
Showing with 14 additions and 1 deletion.
  1. +14 −1 System/Win32/Types.hs
View
15 System/Win32/Types.hs
@@ -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
@@ -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

0 comments on commit 5ae4ff5

Please sign in to comment.
Something went wrong with that request. Please try again.