Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nothing is translated to null #914

Closed
paweln1986 opened this issue Mar 3, 2018 · 2 comments
Closed

Nothing is translated to null #914

paweln1986 opened this issue Mar 3, 2018 · 2 comments

Comments

@paweln1986
Copy link

When I have API defined like:

type Api = "user" :> Capture "name" Text :> Get '[JSON] (Maybe User)

servant don't treat Nothing as 404 but return null

curl -H 'Content-type: application/json' localhost:3000/user/Alice

< HTTP/1.1 200 OK
< Transfer-Encoding: chunked
< Date: Sat, 03 Mar 2018 16:02:32 GMT
< Server: Warp/3.2.13
< Content-Type: application/json;charset=utf-8
< 
* Connection #0 to host localhost left intact
null%                                                   

Is it possible to create general handlers for Maybe, now I have to manually throw ServantErrors.
It would be good if servant translate Nothing into 404.

@alpmestan
Copy link
Contributor

alpmestan commented Mar 4, 2018

I see the point you're making, but the response type advertised after the verb (Maybe User in your example) is supposed to be whatever the handler will return in the successful case, that is if we don't encounter any error. The way to achieve what you want with servant is to indeed throw an error. Usually I do this by using a custom monad which contains ExceptT ApplicationError in the stack, where ApplicationError is something like:

data ApplicationError
  = UserNotFound UserId
  | ProductNotFound Product
  | ...

and I then convert values of that type to HTTP error responses (ServantErr values) using hoistServer.

Another way to achieve what you want would be to use servant-checked-exceptions. In both cases though, the library requires you to say Get '[JSON] User and drop the Maybe.

Note that this would all become a little more explicit and symmetric once we get around to executing #841.

@jkarni
Copy link
Member

jkarni commented Mar 6, 2018

Closing, please re-open if you think this was a mistake.

@jkarni jkarni closed this as completed Mar 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants