1- {-# LANGUAGE DeriveGeneric #-}
1+ {-# LANGUAGE DeriveGeneric #-}
22
33module Web.Template.Except
4- ( Except (.. ), JsonWebError (.. )
4+ (
5+ Except (.. )
6+ , JsonWebError (.. )
7+ , ScottyError (.. )
58 , handleEx
69 ) where
710
8- import Data.Aeson (ToJSON (.. ))
11+ import Data.Aeson (FromJSON ( .. ), ToJSON (.. ))
912import Data.String (fromString )
1013import GHC.Generics (Generic )
11- import Network.HTTP.Types.Status (status403 , status404 ,
12- status500 )
14+ import Network.HTTP.Types.Status (status403 , status404 , status500 )
1315import Web.Scotty.Trans (ActionT , ScottyError (.. ), json ,
1416 status )
1517
1618
1719instance ScottyError Except where
18- stringError = StringEx
20+ stringError = JsonException
1921 showError = fromString . show
2022
21- data Except = Forbidden | NotFound Int | StringEx String
23+ data Except = Forbidden | NotFound Int | StringException String | JsonException String
2224 deriving (Show , Eq )
2325
2426newtype JsonWebError = JsonWebError { error :: String }
2527 deriving (Generic )
2628
2729instance ToJSON JsonWebError
30+ instance FromJSON JsonWebError
2831
2932handleEx :: Monad m => Except -> ActionT Except m ()
3033handleEx Forbidden = do
@@ -33,6 +36,9 @@ handleEx Forbidden = do
3336handleEx (NotFound i) = do
3437 status status404
3538 json . JsonWebError $ " Can't find " ++ show i
36- handleEx (StringEx str) = do
39+ handleEx (StringException str) = do
3740 status status500
3841 json . JsonWebError $ " Server problem: " ++ str
42+ handleEx (JsonException str) = do
43+ status status500
44+ json . JsonWebError $ str
0 commit comments