haskell-servant / haskell-servant/servant

Capture errors are not returned in the response

Open
#1,191 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
2k
Forks
427
Avg merge
2d 23h
Merged PRs (30d)
5

Description

I have a very simple server, the full code of which is below:

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

module Main where

import Network.Wai.Handler.Warp
import Servant
import Servant.API

data OneOrZero = One | Zero deriving (Eq, Show)

instance FromHttpApiData OneOrZero where
parseUrlPiece t
| t == "1" = Right One
| t == "0" = Right Zero
| otherwise = Left "field must be 1 or 0"

type API = Capture "v" OneOrZero :> Get '[PlainText] String

app :: Application
app = serve (Proxy :: Proxy API) getEndpoint

getEndpoint :: OneOrZero -> Handler String
getEndpoint _ = pure "Done"

main :: IO ()
main = run 4000 app
```

If I call the server with the path `/0` or `/1`, I get `"Done"` in the response body, as expected. If I use any other path, I get a 400 Bad Request with an empty body—I'm expecting the body to be `field must be 1 or 0`.

What am I doing wrong?

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.