Skip to content

Returning a header regardless of error #1319

@kirelagin

Description

@kirelagin

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions