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

Returning a header regardless of error #1319

Open
kirelagin opened this issue Jul 9, 2020 · 2 comments
Open

Returning a header regardless of error #1319

kirelagin opened this issue Jul 9, 2020 · 2 comments

Comments

@kirelagin
Copy link

kirelagin commented Jul 9, 2020

I would like to have an endpoint that sets a header both when it returns successfully and when it fails with an error. As far as I can tell, currently there is no way to do this easily.

In particular:

  1. Headers are attached to the endpoint’s return value type at type-level, so there is no way to assert that an endpoint sets a header on error.
  2. Headers are attached to the return value at value-level, so there is just no way to set a header when failing.

I tried to come up with some kind of combinator that would do this, but I couldn’t: it all boils down to the fact that combinators are polymorphic over the underlying monad, so there is no way for them to do anything with the error in the error case.

What I was thinking instead is a custom handler monad, that will essentially be ExceptT (headers, ServerError) IO (headers, a) and then hoist it to Handler a by prepending the headers to the error or... and then I’m not sure how to add the headers to the normal response. And, also, this way the headers set will need to be the same for all endpoints, which is not great.

Ultimately, I would be ok with sacrificing 1 and not indicating in the type that a header is set, but 2, together with my previous paragraph, seems to be a big roadblock.

So, I guess, I’m looking for ideas of how this can be achieved and, ideally, eventually upstreamed for the future users.

@fisx
Copy link
Member

fisx commented Jul 10, 2020

We are going to release error renderers next, and then a way to represent all errors in the types after that.

Do you think this will solve your problem?

@ch1bo
Copy link

ch1bo commented Nov 25, 2020

I just documented that feature in the servant-exceptions example: https://github.com/ch1bo/servant-exceptions/blob/3eb8a1091e16180f2a7d4641a17e67edfb6df12b/example/Main.hs#L65

With this package one splits error response encoding from the actual handler and the HasServer Throws instance uses ToServantErr to figure out what response headers shall be added for a given error value.

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