loch-th 0.2 → 0.2.1
raw patch · 2 files changed
+48/−11 lines, 2 files
Files
- Debug/Trace/LocationTH.hs +45/−8
- loch-th.cabal +3/−3
Debug/Trace/LocationTH.hs view
@@ -20,6 +20,8 @@ , undef , check , checkIO+ , checkTrace+ , checkTraceIO ) where import qualified Control.Exception as C@@ -76,13 +78,12 @@ assert :: Q Exp -> Q Exp assert t = do st <- pprint `fmap` t- [| if' (not $t) $ throw $ AssertionFailed $- $__LOCATION__ ++ ": Assertion `" ++ st ++ "' failed"- |]+ [| assert' $t $__LOCATION__ st |] -if' :: Bool -> a -> a -> a-if' True x _ = x-if' False _ y = y+assert' :: Bool -> String -> String -> a -> a+assert' False loc st _ = throw $ AssertionFailed $+ loc ++ ": Assertion `" ++ st ++ "' failed"+assert' True _ _ x = x -- -- | A location-emitting 'error' call.@@ -93,8 +94,11 @@ -- *** Exception: <interactive>:1:1-8: no such thing. -- failure :: Q Exp-failure = [| error . ($__LOCATION__ ++) . (": " ++) |]+failure = [| failure' $__LOCATION__ |] +failure' :: String -> String -> a+failure' loc t = error $ loc ++ ": " ++ t+ -- -- | A location-emitting 'undefined'. --@@ -141,7 +145,40 @@ -- "*** Exception: <interactive>:1:1-8: /foo: openFile: does not exist (No such file or directory) -- checkIO :: Q Exp-checkIO = [| flip C.catch (return . $failure . showEx) |]+checkIO = [| checkIO' $__LOCATION__ |]++checkIO' :: String -> IO a -> IO a+checkIO' loc a = C.catch a $ \e -> return $ failure' loc (showEx e)++--+-- | 'checkTrace' extends 'check' with the ability to add a custom string+-- to the error message.+--+-- @$checkTrace :: String -> c -> c@+--+-- >>> $checkTrace "XXX" $ head []+-- *** Exception: <interactive>:1:1-6 XXX: Prelude.head: empty list+--+checkTrace :: Q Exp+checkTrace = [| checkTrace' $__LOCATION__ |]++checkTrace' :: String -> String -> a -> a+checkTrace' loc t = unsafePerformIO . checkTraceIO' loc t . C.evaluate++--+-- | 'checkTraceIO' extends 'checkIO' with the ability to add a custom+-- string to the error message.+--+-- @$checkTraceIO :: String -> IO a -> IO a@+--+-- >>> $checkTraceIO "XXX" $ readFile "/foo"+-- "*** Exception: <interactive>:1:1-8 XXX: /foo: openFile: does not exist (No such file or directory)+--+checkTraceIO :: Q Exp+checkTraceIO = [| checkTraceIO' $__LOCATION__ |]++checkTraceIO' :: String -> String -> IO a -> IO a+checkTraceIO' loc t = checkIO' (loc ++ " " ++ t) showEx :: C.SomeException -> String showEx = show
loch-th.cabal view
@@ -1,5 +1,5 @@ Name: loch-th-Version: 0.2+Version: 0.2.1 Synopsis: Support for precise error locations in source files (Template Haskell version) Description: This module provides a Template Haskell based mechanism to tag failures with the location of the failure call. The@@ -12,8 +12,8 @@ Maintainer: tomi@nomi.cz Category: Development Build-type: Simple-Cabal-version: >=1.2+Cabal-version: >=1.6 Library Exposed-modules: Debug.Trace.LocationTH- Build-depends: base >=4 && <5, template-haskell, pretty+ Build-depends: base == 4.*, template-haskell, pretty