Deal with JSON parse errors #511
This commit is contained in:
parent
d8af216730
commit
2e8078e461
@ -45,7 +45,7 @@ import Data.Text.Lazy.Builder (fromLazyText)
|
|||||||
import Data.Text.Lazy.Encoding (decodeUtf8)
|
import Data.Text.Lazy.Encoding (decodeUtf8)
|
||||||
import Data.Text.Lazy.Builder (toLazyText)
|
import Data.Text.Lazy.Builder (toLazyText)
|
||||||
import qualified Blaze.ByteString.Builder.Char.Utf8 as Blaze
|
import qualified Blaze.ByteString.Builder.Char.Utf8 as Blaze
|
||||||
import Data.Conduit (($$))
|
import Data.Conduit
|
||||||
import Network.Wai (requestBody, requestHeaders)
|
import Network.Wai (requestBody, requestHeaders)
|
||||||
import Network.Wai.Parse (parseHttpAccept)
|
import Network.Wai.Parse (parseHttpAccept)
|
||||||
import qualified Data.ByteString.Char8 as B8
|
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 :: J.FromJSON a => GHandler sub master (J.Result a)
|
||||||
parseJsonBody = do
|
parseJsonBody = do
|
||||||
req <- waiRequest
|
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
|
-- | Same as 'parseJsonBody', but return an invalid args response on a parse
|
||||||
-- error.
|
-- error.
|
||||||
|
|||||||
@ -7,7 +7,7 @@ maintainer: Michael Snoyman <michael@snoyman.com>
|
|||||||
synopsis: Generate content for Yesod using the aeson package.
|
synopsis: Generate content for Yesod using the aeson package.
|
||||||
category: Web, Yesod
|
category: Web, Yesod
|
||||||
stability: Stable
|
stability: Stable
|
||||||
cabal-version: >= 1.6
|
cabal-version: >= 1.8
|
||||||
build-type: Simple
|
build-type: Simple
|
||||||
homepage: http://www.yesodweb.com/
|
homepage: http://www.yesodweb.com/
|
||||||
description: Generate content for Yesod using the aeson package.
|
description: Generate content for Yesod using the aeson package.
|
||||||
@ -32,6 +32,18 @@ library
|
|||||||
exposed-modules: Yesod.Json
|
exposed-modules: Yesod.Json
|
||||||
ghc-options: -Wall
|
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
|
source-repository head
|
||||||
type: git
|
type: git
|
||||||
location: https://github.com/yesodweb/yesod
|
location: https://github.com/yesodweb/yesod
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user