When using servant, I'd like to return all errors as JSON. Currently, if a request fails to parse, I see an error message like this, returned as plain text
Failed reading: not a valid json value
Instead I would like to return this as application/json
{"error":"Failed reading: not a valid json value"}
How can I do this? The docs say ServantErr is the default error type, and I can certainly respond with custom errors inside my handlers, but if parsing fails I don't see how I can return a custom error.
First, some language extensions
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ViewPatterns #-}
Now then
Unfortunately this is more difficult than it should be. Servant, while well-designed and the composition of small logical parts, is very opinionated about how HTTP services should operate. The default implementation of ReqBody, which you are probably using, is hard-coded to spit out a text string.
However, we can switch out ReqBody for our own data type:
module Body where
import Control.Monad.Trans (liftIO)
import Data.Proxy (Proxy(..))
import Network.Wai (lazyRequestBody)
import Data.Aeson
import Servant.API
import Servant.Server
import Servant.Server.Internal
data Body a
instance (FromJSON a, HasServer api context) => HasServer (Body a :> api) context where
type ServerT (Body a :> api) m = a -> ServerT api m
route Proxy context subserver =
route (Proxy :: Proxy api) context (addBodyCheck subserver (withRequest bodyCheck))
where
bodyCheck request = do
body <- liftIO (lazyRequestBody request)
case eitherDecode body of
Left (BodyError -> e) ->
delayedFailFatal err400 { errBody = encode e }
Right v ->
return v
In this very brief amount of code a lot is happening:
We are teaching the servant-server package on how to handle our new datatype when it appears in the type resolution for serve (Proxy :: Proxy (Body foo :> bar)) server.
We have ripped most of the code from the v0.8.1 release of ReqBody.
We are adding a function to the pipeline that processes request bodies.
In it, we attempt to decode to the a parameter of Body. On failure, we spit out a JSON blob and an HTTP 400.
We are entirely ignoring content-type headers here, for brevity.
Here is the type of the JSON blob:
newtype BodyError = BodyError String
instance ToJSON BodyError where
toJSON (BodyError b) = object ["error" .= b]
Most of this machinery is internal to servant-server and underdocumented and rather fragile. For example, already I see that the code diverges on master branch and the arity of my addBodyCheck has changed.
Though the Servant project is still quite young and remarkably ambitious, I have to say that the aesthetics and robustness of this solution are definitely underwhelming.
To test this
We will need a Main module:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
module Main where
import Data.Proxy (Proxy(..))
import Network.Wai.Handler.Warp (run)
import Servant.API
import Servant.Server
import Body
type API = Body [Int] :> Post '[JSON] [Int]
server :: Server API
server = pure
main :: IO ()
main = do
putStrLn "running on port 8000"
run 8000 (serve (Proxy :: Proxy API) server)
And a shell:
~ ❯❯❯ curl -i -XPOST 'http://localhost:8000/'
HTTP/1.1 400 Bad Request
Transfer-Encoding: chunked
Date: Fri, 20 Jan 2017 01:18:57 GMT
Server: Warp/3.2.9
{"error":"Error in $: not enough input"}%
~ ❯❯❯ curl -id 'hey' -XPOST 'http://localhost:8000/'
HTTP/1.1 400 Bad Request
Transfer-Encoding: chunked
Date: Fri, 20 Jan 2017 01:19:02 GMT
Server: Warp/3.2.9
{"error":"Error in $: Failed reading: not a valid json value"}%
~ ❯❯❯ curl -id '[1,2,3]' -XPOST 'http://localhost:8000/'
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Date: Fri, 20 Jan 2017 01:19:07 GMT
Server: Warp/3.2.9
Content-Type: application/json
[1,2,3]%
Ta-da!
You should be able to run all this code on LTS-7.16.
What did we learn
(1) Servant and Haskell are fun.
(2) The typeclass machinery of Servant allows for a kind of plug-and-play when it comes to the types you specify in your API. We can take out ReqBody and replace it with our own; on a project I did at work we even replaced the Servant verbs (GET, POST, ...) with our own. We wrote new content types and we even did something similar with ReqBody like you saw here.
(3) It is the remarkable ability of the GHC compiler that we can destructure types during compile-time to influence runtime behavior in a safe and logically sound way. That we can express a tree of API routes at the type-level and then walk over them using typeclass instances, accumulating a server type using type families, is a wonderfully elegant way to build a well-typed web service.
Currently right now I just handle this in middleware. I do something like the following:
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts #-}
module Lib.ErrorResponse where
import Data.Text.Lazy.Encoding (decodeUtf8)
import Data.ByteString.Lazy (toStrict)
import Blaze.ByteString.Builder (toLazyByteString)
import Blaze.ByteString.Builder.ByteString (fromByteString)
import Network.Wai
import Network.Wai.Internal
import Network.HTTP.Types
import Data.Text
import Data.Aeson
import qualified Data.Text.Lazy as TL
customError :: Application -> Application
customError = modifyResponse responseModifier
responseModifier :: Response -> Response
responseModifier r
| responseStatus r == status400 && not (isCustomMessage r "Bad Request") =
buildResponse status400 "Bad Request" (customErrorBody r "BadRequest") 400
| responseStatus r == status403 =
buildResponse status403 "Forbidden" "Forbidden" 400
| responseStatus r == status404 =
buildResponse status404 "Not Found" "Not Found" 404
| responseStatus r == status405 =
buildResponse status405 "Method Not Allowed" "Method Not Allowed" 405
| otherwise = r
customErrorBody :: Response -> Text -> Text
customErrorBody (ResponseBuilder _ _ b) _ = TL.toStrict $ decodeUtf8 $ toLazyByteString b
customErrorBody (ResponseRaw _ res) e = customErrorBody res e
customErrorBody _ e = e
isCustomMessage :: Response -> Text -> Bool
isCustomMessage r m = "{\"error\":" `isInfixOf` customErrorBody r m
buildResponse :: Status -> Text -> Text -> Int -> Response
buildResponse st err msg cde = responseBuilder st
[("Content-Type", "application/json")]
(fromByteString . toStrict . encode $ object
[ "error" .= err
, "message" .= msg
, "statusCode" .= cde
]
)
And then I can use just like any other middleware:
run 8000 . customError $ serve api server
Taking inspiration from #codedmart I also use a middleware, but it does not construct the json, it only changes the content type of the response when there is an error, and keep the original error message.
startApp :: IO ()
startApp = run 8081 . (modifyResponse errorHeadersToJson) $ serve api server
errorHeadersToJson :: Response -> Response
errorHeadersToJson r
| responseStatus r == status200 = r
| otherwise = mapResponseHeaders text2json r
text2json :: ResponseHeaders -> ResponseHeaders
text2json h = Map.assocs (Map.fromList [("Content-Type", "application/json")] `Map.union` Map.fromList h)
The json is built beforehand with a function overriding the Servant throwError function.
data ServerError = ServerError
{ statusCode :: Int
, error :: String
, message :: String
} deriving (Eq, Show)
$(deriveJSON defaultOptions ''ServerError)
throwJsonError :: ServantErr -> String -> Servant.Handler b
throwJsonError err "" = throwError $ err { errBody = encode $ ServerError (errHTTPCode err) ("Server error"::String) (show $ errBody err) }
throwJsonError err message = throwError $ err { errBody = encode $ ServerError (errHTTPCode err) ("Server error"::String) message }
then I can throw any error with a custom message, it will be served as a json with the correct content-type :
throwJsonError err500 "Oh no !"
Related
I'm building a reinforcement learning library where I'd like to pass certain instance information into the executables via a piped JSON.
Using aeson's Simplest.hs, I'm able to get the following basic example working as intended. Note that the parameters are sitting in Main.hs as a String params as a placeholder.
I tried to modify Main.hs so I would pipe the Nim game parameters in from a JSON file via getContents, but am running into the expected [Char] vs. IO String issue. I've tried to read up as much as possible about IO, but can't figure out how to lift my JSON parsing method to deal with IO.
How would I modify the below so that I can work with piped-in JSON?
Main.hs
module Main where
import qualified System.Random as Random
import qualified Data.ByteString.Lazy.Char8 as BL
import qualified Games.Engine as Engine
import qualified Games.IO.Nim as NimIO
import qualified Games.Rules.Nim as Nim
import qualified Games.Learn.ValueIteration as VI
main :: IO ()
main = do
let params = "{\"players\":[\"Bob\", \"Alice\", \"Charlie\"], \"initialPiles\": [3, 4, 5], \"isMisere\": false}"
let result = NimIO.decode $ BL.pack params :: Maybe NimIO.NimGame
case result of
Nothing -> putStrLn "Parameter errors."
Just game -> do
putStrLn "Let's play some Nim! Remainder of code omitted"
Games.IO.Nim.hs
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Games.IO.Nim
( decode
, NimGame
, players
, initialPiles
, isMisere
) where
import Control.Applicative (empty)
import qualified Data.ByteString.Lazy.Char8 as BL
import Data.Aeson
( pairs,
(.:),
object,
FromJSON(parseJSON),
Value(Object),
KeyValue((.=)),
ToJSON(toJSON, toEncoding),
decode)
data NimGame = NimGame
{ players :: [String]
, initialPiles :: [Int]
, isMisere :: Bool
} deriving (Show)
instance ToJSON NimGame where
toJSON (NimGame playersV initialPilesV isMisereV) = object [ "players" .= playersV,
"initialPiles" .= initialPilesV,
"isMisere" .= isMisereV]
toEncoding NimGame{..} = pairs $
"players" .= players <>
"initialPiles" .= initialPiles <>
"isMisere" .= isMisere
instance FromJSON NimGame where
parseJSON (Object v) = NimGame <$>
v .: "players" <*>
v .: "initialPiles" <*>
v .: "isMisere"
parseJSON _ = empty
Alternative Main.hs that generates compile error
module Main where
import qualified System.Random as Random
import qualified Data.ByteString.Lazy.Char8 as BL
import qualified Games.Engine as Engine
import qualified Games.IO.Nim as NimIO
import qualified Games.Rules.Nim as Nim
import qualified Games.Learn.ValueIteration as VI
main :: IO ()
main = do
--let params = "{\"players\":[\"Bob\", \"Alice\", \"Charlie\"], \"initialPiles\": [3, 4, 5], \"isMisere\": false}"
let params = getContents
let result = NimIO.decode $ BL.pack params :: Maybe NimIO.NimGame
case result of
Nothing -> putStrLn "Parameter errors."
Just game -> do
putStrLn "Let's play some Nim!"
Compile Error
(base) randm#pearljam ~/Projects/gameshs $ stack build
gameshs-0.1.0.0: unregistering (local file changes: app/Nim.hs)
gameshs> configure (lib + exe)
Configuring gameshs-0.1.0.0...
gameshs> build (lib + exe)
Preprocessing library for gameshs-0.1.0.0..
Building library for gameshs-0.1.0.0..
Preprocessing executable 'nim-exe' for gameshs-0.1.0.0..
Building executable 'nim-exe' for gameshs-0.1.0.0..
[2 of 2] Compiling Main
/home/randm/Projects/gameshs/app/Nim.hs:17:41: error:
• Couldn't match expected type ‘[Char]’
with actual type ‘IO String’
• In the first argument of ‘BL.pack’, namely ‘params’
In the second argument of ‘($)’, namely ‘BL.pack params’
In the expression:
NimIO.decode $ BL.pack params :: Maybe NimIO.NimGame
|
17 | let result = NimIO.decode $ BL.pack params :: Maybe NimIO.NimGame
| ^^^^^^
-- While building package gameshs-0.1.0.0 (scroll up to its section to see the error) using:
/home/randm/.stack/setup-exe-cache/x86_64-linux-tinfo6/Cabal-simple_mPHDZzAJ_3.2.1.0_ghc-8.10.4 --builddir=.stack-work/dist/x86_64-linux-tinfo6/Cabal-3.2.1.0 build lib:gameshs exe:nim-exe --ghc-options " -fdiagnostics-color=always"
Process exited with code: ExitFailure 1
getContents returns not a String as you apparently expect, but IO String, which is a "program", which, when executed, will produce a String. So when you're trying to parse this program with decode, of course that doesn't work: decode parses a String, it cannot parse a program.
So how do you execute this program to obtain the String? There are two ways: either you make it part of another program or you call it main and it becomes your entry point.
In your case, the sensible thing to do would be to make getContent part of your main program. To do that, use the left arrow <-, like this:
main = do
params <- getContents
let result = NimIO.decode $ BL.pack params :: Maybe NimIO.NimGame
...
Problem: I am attempting to POST some JSON to an HTTP endpoint that only accepts Base64 encoding.
Code Sample: Here is a code sample which successfully posts without Base64 encoding:
{-# LANGUAGE OverloadedStrings #-}
module Lib where
import Data.Aeson (encode, object, (.=))
import qualified Data.ByteString.Lazy.Char8 as L8
import Network.HTTP.Client
import Network.HTTP.Client.TLS
import Network.HTTP.Types.Status (statusCode)
import qualified Data.ByteString.Base64 as B64
postJSON :: IO ()
postJSON = do
manager <- newManager tlsManagerSettings
-- Nested JSON object to POST:
let requestObject = object
[ "event" .= ("App launched." :: String)
, "properties" .= object [ "distinct_id" .= ("user" :: String)
, "token" .= ("f793bae9548d8e123cef251fd81df487" :: String)
]
]
initialRequest <- parseRequest "http://api.mixpanel.com/track"
let request = initialRequest
{ method = "POST"
, requestBody = RequestBodyLBS $ encode requestObject
, requestHeaders =
[ ("Content-Type", "application/json; charset=utf-8")
]
}
response <- httpLbs request manager
putStrLn $ "The status code was: "
++ show (statusCode $ responseStatus response)
L8.putStrLn $ responseBody response
Attempt: In order to send the JSON as Base64 encoded, I tried replacing requestBody = RequestBodyLBS $ encode requestObject with requestBody = RequestBodyLBS $ Data.Bytestring.Base64.encode (encode requestObject), but I get a type error. So how do I encode the JSON as Base64 for this HTTP POST?
B64.encode here is a function from strict ByteString to strict ByteString (you'll need to hover over the type name in the haddocks to see this if you're just browsing), while Aeson.encode returns a lazy bytestring (from the Data.ByteString.Lazy module). These are two distinct types although they have the same name.
You probably have to do something like:
...
requestBody = RequestBodyLBS $ L8.fromStrict $ Data.Bytestring.Base64.encode (L8.toStrict $ encode requestObject)
I have two things to add to jberryman's answer.
First, if (as it now appears) you're going to be putting this in a query string, you need to make sure you don't just use a base64 encoded bytestring, but instead use a base64 url-encoded bytestring. So don't use Data.Bytestring.Base64 (as jberryman linked to), but rather Data.Bytestring.Base64.URL (here).
Second, while he pointed you in the right direction on the Base64 encoding part, it seems you're still hung up on setting the querystring. For that, you should check out the setQueryString function in the http-client library you're already using (link here).
That function has the signature:
setQueryString :: [(ByteString, Maybe ByteString)] -> Request -> Request
So if you're base64 encoded bytestring is built like this
let urlEncodedBytestring = Data.Bytestring.Base64.URL.encode . L8.toStrict $ encode requestObject
and if you're attemtping to set the data key in the querystring of your request, then you'll probably want:
let requestWithQueryStringSet = setQueryString [("data", (Just urlEncodedBytestring))] request
I'm learning to use wreq this weekend and I've run into some strange behavior.
I have a module AuthRequest
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
module AuthRequest where
import Data.Aeson
import GHC.Generics
import Data.Monoid
data AuthRequest = AuthRequest {
client_id :: String
, client_secret :: String
, grant_type :: String
} deriving (Generic, Show)
instance ToJSON AuthRequest where
toJSON (AuthRequest id_ secret grant) =
object [ "client_id" .= id_
, "client_secret" .= secret
, "grant_type" .= grant
]
toEncoding(AuthRequest id_ secret grant) =
pairs ("client_id" .= id_ <> "client_secret" .= secret <> "grant_type" .= grant)
and a module HttpDemo
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
module HttpDemo where
import Control.Lens
import Network.Wreq
import AuthRequest
import Data.Aeson
clientId = "some_id"
clientSecret = "some_secret"
url = "http://localhost:5000"
opts :: Options
opts = defaults
& header "Content-Type" .~ ["application/x-www-form-urlencoded"]
req :: AuthRequest
req = AuthRequest clientId clientSecret "credentials"
postIt = postWith opts url (toJSON req)
On the other end, I have a simple python flask server that listens to this request with a breakpoint so I can see the value that comes through.
When I look at the request.form on the server side, I see this: ImmutableMultiDict([('{"client_secret":"some_secret","client_id":"some_id","grant_type":"whatever"}', '')])
The key is what should be my post body!
But if I make a similar request using the requests python library
requests.post('http://localhost:5000', data={'client_id': clientId, 'client_secret': clientSecret, 'grant_type': grant_type}, headers={'content-type': 'application/x-www-form-urlencoded'})
I see what I expect: ImmutableMultiDict([('grant_type', 'whatever'), ('client_id', 'some_id'), ('client_secret', 'some_secret')])
What I think I want is to send this request as x-www-form-urlencoded. I see there are some docs here around this, but not clear on how to proceed. Maybe I need a FormValue instance? An example would be helpful.
As per your discussion with #Alexis, your client seems to be sending JSON, while the server is expected urlencoded. The documentation of Post shows how to send urlencoded data, using the := constructor. In this case this would be
postIt = post url ["client_id" := clientId, "client_secret" := clientSecret, "grant_type" := grantType]
I've given the example using post rather than postWith since the default appears to be that it uses application/x-www-form-urlencoded.
There seems to be a slight complication with OverloadedStrings. To make a compilable program, I had to remove the AuthRequest module and explicitly give the types of the constants as below
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Network.Wreq
import Data.ByteString as B
clientId = "some_id" :: ByteString
clientSecret = "some_secret" :: ByteString
grantType = "credentials" :: ByteString
url = "http://localhost:8080"
postIt = post url ["client_id" := clientId, "client_secret" := clientSecret, "grant_type" := grantType]
main = postIt
The full example is here:
{-# LANGUAGE OverloadedStrings #-}
module Test2 where
import Data.Aeson
import Text.Mustache
main :: IO ()
main = do
let example = Data.Aeson.object [ "key" .= (5 :: Integer), "somethingElse" .= (2 :: Integer) ]
print . encode $ example
print ("Start" :: String)
case compileTemplate "" "{{{jsonData}}}" of
Right x -> do
print $ substituteValue x (Text.Mustache.object ["jsonData" ~= example])
Left e -> error . show $ e
The above produces the following output:
"{\"somethingElse\":2,\"key\":5}"
"Start"
"fromList [(\"somethingElse\",2.0),(\"key\",5.0)]"
My expectation is it would produce:
"{\"somethingElse\":2,\"key\":5}"
"Start"
"{\"somethingElse\":2,\"key\":5}"
Mustache doesn't seem to support straight substitution of JSON objects. Setting up a similar example here, I receive
[object Object]
as output. Not identical to yours, but it indicates that the issue is not necessarily with the Haskell implementation.
In other words, I believe the issue is with your template {{{jsonData}}}.
If you change it to {{{jsonData.somethingElse}}}, it works fine (I know this is not what you want).
Alternatively, encode the JSON data as text prior to passing it to the substitution function as suggested here. Basically something like this:
substituteValue x (Text.Mustache.object ["jsonData" ~= (encodeToLazyText jsonData)])
This results in your desired output. encodeToLazyText is found in Data.Aeson.Text.
Working code:
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Data.Aeson ((.=))
import qualified Data.Aeson as A
import Data.Aeson.Text (encodeToLazyText)
import Text.Mustache ((~=))
import qualified Text.Mustache as M
import qualified Text.Mustache.Types as M
main :: IO ()
main = do
print . A.encode $ jsonData
putStrLn "Start"
case M.compileTemplate "" "in mustache: {{{jsonData}}}" of
Right template ->
print (M.substituteValue template mustacheVals)
Left e ->
error . show $ e
jsonData :: A.Value
jsonData =
A.object
[ "key" .= (5 :: Integer)
, "somethingElse" .= (2 :: Integer)
]
mustacheVals :: M.Value
mustacheVals =
M.object
[ "jsonData" ~= encodeToLazyText jsonData
]
I'm using Data.Aeson to parse some JSON into a Record type. From time to time data is added to the JSON and this breaks my code as Aeson complains something to the effect of:
expected Object with 21
name/value pairs but got 23 name/value
I'd really prefer to parse the JSON in a fault tolerant way -- I don't care if more fields are added to the JSON at a later date, just parse whatever you can! Is there a way to achieve this fault tolerance? Here's my code:
myRecordFromJSONString :: BS.ByteString -> Maybe MyRecord
myRecordFromJSONString s = case Data.Attoparsec.parse json s of
Done _rest res -> Data.Aeson.Types.parseMaybe parseJSON res
_ -> Nothing
I should add that I'm using deriveJSON from Data.Aeson.TH to generate the parsing code. If I write the FromJSON code manually it's fault tolerant but I'd like to not have to do that...
If you are using GHC 7.2 or 7.4, the new generics support in aeson doesn't check for extra fields. I'm not sure if this is by design or not but we use it for the same reason.
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
import Data.Aeson
import qualified Data.Aeson.Types
import Data.Attoparsec
import qualified Data.ByteString as BS
import Data.ByteString.Char8 ()
import GHC.Generics
data MyRecord = MyRecord
{ field1 :: Int
} deriving (Generic, Show)
instance FromJSON MyRecord
myRecordFromJSONString :: BS.ByteString -> Maybe MyRecord
myRecordFromJSONString s = case Data.Attoparsec.parse json s of
Done _rest res -> Data.Aeson.Types.parseMaybe parseJSON res
_ -> Nothing
main :: IO ()
main = do
let parsed = myRecordFromJSONString "{ \"field1\": 1, \"field2\": 2 }"
print parsed
Running this would fail with the TH derived instance due to 'field2' not existing in the record. The Generic instance returns the desired result:
Just (MyRecord {field1 = 1})