From 43eaa6bf4e94561d2a53dc69df17bf80d43a0ed5 Mon Sep 17 00:00:00 2001 From: Kristof Bastiaensen 3e Date: Fri, 4 Jun 2021 14:42:05 +0200 Subject: [PATCH] improve error messages for `lookup` --- CHANGES.md | 4 ++++ cassava.cabal | 2 +- src/Data/Csv/Conversion.hs | 5 ++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index e97cf00..fbd3f76 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,7 @@ +## Version 0.5.3.0 + + * improve error messages for `lookup` and NamedRecord parsers. + ## Version 0.5.2.0 * Add `FromField`/`ToField` instances for `Identity` and `Const` (#158) diff --git a/cassava.cabal b/cassava.cabal index 5607650..775e448 100644 --- a/cassava.cabal +++ b/cassava.cabal @@ -1,6 +1,6 @@ cabal-version: 1.12 Name: cassava -Version: 0.5.2.0 +Version: 0.5.3.0 Synopsis: A CSV parsing and encoding library Description: { diff --git a/src/Data/Csv/Conversion.hs b/src/Data/Csv/Conversion.hs index 1f2ab84..574e9e2 100644 --- a/src/Data/Csv/Conversion.hs +++ b/src/Data/Csv/Conversion.hs @@ -1148,8 +1148,11 @@ unsafeIndex v idx = parseField (V.unsafeIndex v idx) -- 'empty' if the field is missing or if the value cannot be converted -- to the desired type. lookup :: FromField a => NamedRecord -> B.ByteString -> Parser a -lookup m name = maybe (fail err) parseField $ HM.lookup name m +lookup m name = maybe (fail err) parseField' $ HM.lookup name m where err = "no field named " ++ show (B8.unpack name) + parseField' fld = case runParser (parseField fld) of + Left e -> fail $ "in named field " ++ show (B8.unpack name) ++ ": " ++ e + Right res -> pure res {-# INLINE lookup #-} -- | Alias for 'lookup'.