Deal with JSON parse errors #511

This commit is contained in:
Michael Snoyman 2013-03-10 09:27:06 +02:00
parent d8af216730
commit 2e8078e461
2 changed files with 21 additions and 3 deletions

View File

@ -45,7 +45,7 @@ import Data.Text.Lazy.Builder (fromLazyText)
import Data.Text.Lazy.Encoding (decodeUtf8)
import Data.Text.Lazy.Builder (toLazyText)
import qualified Blaze.ByteString.Builder.Char.Utf8 as Blaze
import Data.Conduit (($$))
import Data.Conduit
import Network.Wai (requestBody, requestHeaders)
import Network.Wai.Parse (parseHttpAccept)
import qualified Data.ByteString.Char8 as B8
@ -88,7 +88,13 @@ jsonToRepJson = return . RepJson . toContent . J.toJSON
parseJsonBody :: J.FromJSON a => GHandler sub master (J.Result a)
parseJsonBody = do
req <- waiRequest
fmap J.fromJSON $ lift $ requestBody req $$ sinkParser JP.value'
eValue <- lift
$ runExceptionT
$ transPipe lift (requestBody req)
$$ sinkParser JP.value'
return $ case eValue of
Left e -> J.Error $ show e
Right value -> J.fromJSON value
-- | Same as 'parseJsonBody', but return an invalid args response on a parse
-- error.

View File

@ -7,7 +7,7 @@ maintainer: Michael Snoyman <michael@snoyman.com>
synopsis: Generate content for Yesod using the aeson package.
category: Web, Yesod
stability: Stable
cabal-version: >= 1.6
cabal-version: >= 1.8
build-type: Simple
homepage: http://www.yesodweb.com/
description: Generate content for Yesod using the aeson package.
@ -32,6 +32,18 @@ library
exposed-modules: Yesod.Json
ghc-options: -Wall
test-suite tests
type: exitcode-stdio-1.0
main-is: Spec.hs
hs-source-dirs: test
build-depends: base
, wai-test
, hspec
, yesod-json
, yesod-core
, text
, containers
source-repository head
type: git
location: https://github.com/yesodweb/yesod