From bc616eb9201e2ddc48ddb15ef37032976aa95c0b Mon Sep 17 00:00:00 2001 From: mrkkrp Date: Sun, 3 Jan 2016 23:25:58 +0600 Subject: [PATCH] =?UTF-8?q?Improve=20error=20messages=20in=20=E2=80=98From?= =?UTF-8?q?JSON=E2=80=99=20instance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 ++++ Web/Slug.hs | 12 +++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c45f390..2128b65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## Slug 0.1.2 + +* Improved error messages in `parseJSON`. + ## Slug 0.1.1 * Add `Read` instance of `Slug`. diff --git a/Web/Slug.hs b/Web/Slug.hs index 822f371..ed20fc2 100644 --- a/Web/Slug.hs +++ b/Web/Slug.hs @@ -22,7 +22,7 @@ module Web.Slug where import Control.Exception (Exception) -import Control.Monad (mzero, (>=>), liftM) +import Control.Monad ((>=>), liftM) import Control.Monad.Catch (MonadThrow (..)) import Data.Aeson.Types (ToJSON (..), FromJSON (..)) import Data.Char (isAlphaNum) @@ -32,8 +32,8 @@ import Database.Persist.Class (PersistField (..)) import Database.Persist.Sql (PersistFieldSql (..)) import Database.Persist.Types (SqlType (..)) import Web.PathPieces -import qualified Data.Aeson.Types as A -import qualified Data.Text as T +import qualified Data.Aeson as A +import qualified Data.Text as T -- | This exception is thrown by 'mkSlug' when its input cannot be converted -- into proper 'Slug'. @@ -133,8 +133,10 @@ instance ToJSON Slug where toJSON = toJSON . unSlug instance FromJSON Slug where - parseJSON (A.String v) = maybe mzero return (parseSlug v) - parseJSON _ = mzero + parseJSON = A.withText "Slug" $ \txt -> + case parseSlug txt of + Left err -> fail (show err) + Right val -> return val instance PersistField Slug where toPersistValue = toPersistValue . unSlug