From 2e8078e4616f7904f0dbff0361f199bf87e15c13 Mon Sep 17 00:00:00 2001 From: Michael Snoyman Date: Sun, 10 Mar 2013 09:27:06 +0200 Subject: [PATCH] Deal with JSON parse errors #511 --- yesod-json/Yesod/Json.hs | 10 ++++++++-- yesod-json/yesod-json.cabal | 14 +++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/yesod-json/Yesod/Json.hs b/yesod-json/Yesod/Json.hs index eb46612a..dcc4dce8 100644 --- a/yesod-json/Yesod/Json.hs +++ b/yesod-json/Yesod/Json.hs @@ -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. diff --git a/yesod-json/yesod-json.cabal b/yesod-json/yesod-json.cabal index 1048b3a4..3a29c9e9 100644 --- a/yesod-json/yesod-json.cabal +++ b/yesod-json/yesod-json.cabal @@ -7,7 +7,7 @@ maintainer: Michael Snoyman 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