From b76d1ecdcd63649db3583c021fcdfcd0196b7512 Mon Sep 17 00:00:00 2001 From: hallgren Date: Fri, 15 Jun 2012 10:15:16 +0000 Subject: [PATCH] Add file name to error message when reading a bad .gfo file (in some cases) This turns error messages like gf: too few bytes. Failed reading at byte position 1 gf: /some/path/somefile.gfo: too few bytes. Failed reading at byte position 1 but a better fix would be to ignore bad .gfo files and compile from source. The problem is the way this decision is made in GF.Compile.ReadFiles.selectFormat... --- src/compiler/GF/Grammar/Binary.hs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/compiler/GF/Grammar/Binary.hs b/src/compiler/GF/Grammar/Binary.hs index f2653450f..ae0c72809 100644 --- a/src/compiler/GF/Grammar/Binary.hs +++ b/src/compiler/GF/Grammar/Binary.hs @@ -9,6 +9,9 @@ module GF.Grammar.Binary where +import Prelude hiding (catch) +import Control.Exception(catch,ErrorCall(..),throwIO) + import Data.Char import Data.Binary import Control.Monad @@ -291,10 +294,9 @@ putGFOVersion = mapM_ (putWord8 . fromIntegral . ord) gfoVersion getGFOVersion = replicateM (length gfoVersion) (fmap (chr . fromIntegral) getWord8) decodeModule :: FilePath -> IO SourceModule -decodeModule fpath = - decodeFile_ fpath (getGFOVersion >> get) +decodeModule fpath = decodeFile' fpath (getGFOVersion >> get) -decodeModuleHeader fpath = decodeFile_ fpath getVersionedMod +decodeModuleHeader fpath = decodeFile' fpath getVersionedMod where getVersionedMod = do ver <- getGFOVersion @@ -306,3 +308,12 @@ decodeModuleHeader fpath = decodeFile_ fpath getVersionedMod encodeModule :: FilePath -> SourceModule -> IO () encodeModule fpath mo = encodeFile_ fpath (putGFOVersion >> put mo) + +-- | like decodeFile_ but adds file name to error message if there was an error +decodeFile' fpath get = addFPath fpath (decodeFile_ fpath get) + +-- | Adds file name to error message if there was an error, +-- | but laziness can cause errors to slip through +addFPath fpath m = m `catch` handle + where + handle (ErrorCall msg) = throwIO (ErrorCall (fpath++": "++msg))