respondWithHeader and rejections

22 views
Skip to first unread message

Jelmer Kuperus

unread,
Oct 19, 2017, 6:15:28 PM10/19/17
to Akka User List
In akka http I am using the respondWithHeader directive to add a correlation id to the response like so

final case class `X-Request-Id`(token: String) extends ModeledCustomHeader[`X-Request-Id`] {
  override def renderInRequests = true
  override def renderInResponses = true
  override val companion = `X-Request-Id`
  override def value: String = token
}
object `X-Request-Id` extends ModeledCustomHeaderCompanion[`X-Request-Id`] {
  override val name = "X-Request-Id"
  override def parse(value: String) = Try(new `X-Request-Id`(value))
}

respondWithHeader(`X-Request-Id`(requestId))

However it seems that on rejections this header is not added to the response. Is this a bug or am i missing something ?

Jelmer Kuperus

unread,
Oct 20, 2017, 4:42:54 AM10/20/17
to Akka User List
It seems that it works if instead of using the default  rejection handler i use an explicit rejection handler as an inner directive. 

respondWithHeader(`X-Request-Id`(requestId)) {
  handleRejections(RejectionHandler.default) {
    ...

But this is rather inconvenient because some headers are set deeply nested within the routes. Is there an better way?
Reply all
Reply to author
Forward
0 new messages