snix/users/Profpatsch/my-prelude/src/Debug.hs
Profpatsch 722499d8a9 feat(users/Profpatsch/whatcd-resolver): show json val on parse err
The json parsing library gives us an error path where the parse
failed, which means we can index into the path to show the json value
that failed us.

This can be quite expensive (and large!) of course, but the error
message clarity is worth it methinks.

Change-Id: Icacbd799254aaecd4a939ca13e6070d68a78138d
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12952
Tested-by: BuildkiteCI
Reviewed-by: Profpatsch <mail@profpatsch.de>
2025-01-04 21:47:34 +00:00

30 lines
1.1 KiB
Haskell

{-# LANGUAGE QuasiQuotes #-}
module Debug where
import Data.Text qualified as Text
import Data.Text.IO qualified as Text.IO
import Debug.Trace as Debug
import PossehlAnalyticsPrelude
import Pretty qualified
-- | 'Debug.trace' a showable value when (and only if!) it is evaluated, and pretty print it.
traceShowPretty :: (Show a) => a -> a
traceShowPretty a = Debug.trace (textToString $ Pretty.showPretty a) a
-- | 'Debug.trace' a showable value when (and only if!) it is evaluated, and pretty print it. In addition, the given prefix is put before the value for easier recognition.
traceShowPrettyPrefix :: (Show a) => Text -> a -> a
traceShowPrettyPrefix prefix a = Debug.trace ([fmt|{prefix}: {Pretty.showPretty a}|]) a
-- | Display non-printable characters as their unicode Control Pictures
-- https://en.wikipedia.org/wiki/Unicode_control_characters#Control_pictures
--
-- Not all implemented.
putStrLnShowNPr :: Text -> IO ()
putStrLnShowNPr t =
Text.IO.putStrLn $
-- newlines will actually print a newline for convenience
t
& Text.replace "\n" "\n"
& Text.replace "\r\n" "\n"
& Text.replace "\t" ""