haskell-servant / haskell-servant/servant

Incompatibility between servant-server and Vanilla JS generator on server-side errors

Open
#296 9 comments 0 reactions 0 assignees View on GitHub
bug servant-js servant-server
Dominant language
Haskell
Stars
2k
Forks
427
Avg merge
2d 23h
Merged PRs (30d)
5

Description

Hi.

When any endpoint on servant-server returns a `ServantErr`, it just puts the `errBody` as is. But the JS client generated with `servant-js` expects a JSON object and tries to decode it; then it fails with a syntax error.

I think either the server should encode the errors conforming with `Content-type`, or generated JavaScript client must not convert it from JSON and just give the response body to error callback.

Example:

```
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}

module Main where

--------------------------------------------------------------------------------
import Control.Monad.Except
import Control.Monad.Trans.Class
import Data.Proxy
import Data.Text
import Network.Wai.Handler.Warp
import Servant
import Servant.JS
--------------------------------------------------------------------------------
type API = "stuff" :> Get '[JSON] ()

api :: Proxy API
api = Proxy

server :: Server API
server = throwError $ err500 { errBody="not possible" }

main :: IO ()
main = do
putStrLn . unpack $ jsForAPI api vanillaJS
run 2222 $ serve api server
```

When calling the API with generated JavaScript, instead of calling the error handler, it fails with syntax error.

![2015-12-16-144902_635x380_scrot](https://cloud.githubusercontent.com/assets/928084/11841177/2a945a0c-a404-11e5-9d36-2eca79af0e82.png)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.