So I am experimenting with Akka-HTTP and I cant figure out how to pass data
to the rejections handler. Consider this snippet:
object ServerNode extends App {
lazy val services = new ProductionNodeServices
import services._
implicit lazy val log: LoggingAdapter = Logging(actorSystem, getClass)
val https: HttpsConnectionContext =
ConnectionContext.https(services.cryptoUtil.sslContext)
Http().setDefaultServerHttpContext(https)
Http().bindAndHandle(route, "127.0.0.1", 9090)
StdIn.readLine("Hit ENTER to exit")
Await.ready(actorSystem.terminate(), Duration.Inf)
def route(implicit mat: Materializer, log: LoggingAdapter): Route = {
provide(UUID.randomUUID()) { requestId =>
log.warning(requestId.toString)
extractRequestContext { context =>
pathSingleSlash {
post {
entity(as[Foo]) { foo =>
completeWithResponse(EndpointResponse.success(requestId,
Json.toJson(foo)))
}
}
}
}
}
}
def completeWithResponse(response: EndpointResponse) =
complete((response.status, response))
object Foo {
implicit val fooFormat: Format[Foo] = Json.format[Foo]
}
case class Foo(bar: String)
def createUnmarshallingRejectionHandler: PartialFunction[Rejection, Route] = {
case JsonUnmarshallingRejection(error) =>
provide(UUID.randomUUID()) { requestId =>
extractRequestContext {
context => {
if (log.isDebugEnabled) {
val requestContent = Json.prettyPrint(JsError.toJson(error))
val uri = context.request.uri
log.debug(s"Bad Request while Accessing $uri with content:
$requestContent")
}
completeWithResponse(EndpointResponse.badRequest(requestId, JsNull))
}
}
}
}
def createMissingCookieRejectionHandler: PartialFunction[Rejection, Route] = {
case _: MissingCookieRejection =>
extractRequestContext {
_ =>
complete(HttpResponse(BadRequest, entity = "No cookies, no
service!!!"))
}
}
def createAuthorizationFailedRejectionHandler: PartialFunction[Rejection,
Route] = {
case AuthorizationFailedRejection =>
complete((Forbidden, "You're out of your depth!"))
}
def createValidationRejectionHandler: PartialFunction[Rejection, Route] = {
case ValidationRejection(msg, _) =>
complete((InternalServerError, "That wasn't valid! " + msg))
}
def createMethodRejectionHandler = (methodRejections: Seq[MethodRejection])
=> {
val names = methodRejections.map(_.supported.name)
complete((MethodNotAllowed, s"Can't do that! Supported: ${names mkString "
or "}!"))
}
def handleNotFound = complete(NotFound, "URI not found")
private implicit def createRejectionHandler: RejectionHandler =
RejectionHandler.newBuilder()
.handle(createUnmarshallingRejectionHandler)
.handle(createMissingCookieRejectionHandler)
.handle(createAuthorizationFailedRejectionHandler)
.handle(createValidationRejectionHandler)
.handleAll[MethodRejection](createMethodRejectionHandler)
.handleNotFound(handleNotFound)
.result()
}
The requestId is available to the routes but if there is a rejection i want
to use the requestId in createUnmarshallingRejectionHandler rather than
generating a new one but I cant figure out how to get the data from the
route to the rejection handler. Any suggestions?
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ:
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.