Skip to content

fromJSONField: Include JSONPath on JSON parse errors #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Database/PostgreSQL/Simple/FromField.hs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ import Control.Applicative ( (<|>), (<$>), pure, (*>), (<*) )
import Control.Concurrent.MVar (MVar, newMVar)
import Control.Exception (Exception)
import qualified Data.Aeson as JSON
import qualified Data.Aeson.Internal as JSON
import qualified Data.Aeson.Parser as JSON (value')
import Data.Attoparsec.ByteString.Char8 hiding (Result)
import Data.ByteString (ByteString)
Expand Down Expand Up @@ -567,7 +568,7 @@ fromFieldJSONByteString f mbs =
Just bs -> pure bs

-- | Parse a field to a JSON 'JSON.Value' and convert that into a
-- Haskell value using 'JSON.fromJSON'.
-- Haskell value using the 'JSON.FromJSON' instance.
--
-- This can be used as the default implementation for the 'fromField'
-- method for Haskell types that have a JSON representation in
Expand All @@ -588,10 +589,10 @@ fromFieldJSONByteString f mbs =
fromJSONField :: (JSON.FromJSON a, Typeable a) => FieldParser a
fromJSONField f mbBs = do
value <- fromField f mbBs
case JSON.fromJSON value of
JSON.Error err -> returnError ConversionFailed f $
"JSON decoding error: " ++ err
JSON.Success x -> pure x
case JSON.ifromJSON value of
JSON.IError path err -> returnError ConversionFailed f $
"JSON decoding error: " ++ (JSON.formatError path err)
JSON.ISuccess x -> pure x

-- | Compatible with the same set of types as @a@. Note that
-- modifying the 'IORef' does not have any effects outside
Expand Down