Skip to content
This repository has been archived by the owner on Jun 22, 2018. It is now read-only.

Commit

Permalink
Improve error messages in ‘FromJSON’ instance
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkkrp committed Jan 3, 2016
1 parent 39708a5 commit bc616eb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Slug 0.1.2

* Improved error messages in `parseJSON`.

## Slug 0.1.1

* Add `Read` instance of `Slug`.
Expand Down
12 changes: 7 additions & 5 deletions Web/Slug.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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'.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bc616eb

Please sign in to comment.