给定以下仆人服务器:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators #-}
module ServantSample (main) where
import Data.Aeson
import Data.Aeson.TH
import Network.Wai
import Network.Wai.Handler.Warp
import Servant
data Spec = Spec
{ schema :: Object
} deriving (Eq, Show)
$(deriveJSON defaultOptions ''Spec)
type Api = ReqBody '[JSON] Spec :> Post '[JSON] NoContent
server :: Server Api
server = postDoc
postDoc :: Spec -> Handler NoContent
postDoc _ = return NoContent
api :: Proxy Api
api = Proxy
app :: Application
app = serve api server
main :: IO ()
main = run 8080 app
...以及以下 curl 到上述服务器的运行实例:
curl localhost:8080 -H 'Content-Type: application/json' --data '{"schema": "I am not an object but I should be!"}'
我回来了:
Error in $.schema: expected HashMap ~Text v, encountered String
有没有办法拦截 Aeson 错误并将其替换为不会将实现细节泄漏给客户端的东西?据我所知,这一切都发生在 Servant 机器的幕后,我找不到任何关于如何连接它的文档。
例如,我很想返回类似的东西:
Expected a JSON Object under the key "schema", but got the String "I am not an object but I should be!"
谢谢!