Hi Jack, the default rejection handler is defined here <https://github.com/akka/akka/blob/ada636badb0f9ed9e48a75cf9e877fe7ae335cfc/akka-http/src/main/scala/akka/http/scaladsl/server/RejectionHandler.scala#L116>, it executes all those cases in the order they are given and returns the result for the first match. This should be documented, I opened an issue for that, thanks for the pointer! The easiest way to fix the behavior in your case is to seal the route (using Route.seal) below a certain URI path, disallowing rejection to bubble out of it. In this case the seal would need to be placed below the pathPrefix("user") (and you might want to match pathEnd in addition to `post` because otherwise the UserNew unmarshalling etc. applies also to "/user/foo/bar").
Regards, Roland On Wed, Aug 5, 2015 at 11:51 PM, Jack Daniels <[email protected]> wrote: > I have unauthenticated routes and authenticated routes in my Akka/Spray > DSL. Below is simplified version of the code > > > val nakedRoutes = pathPrefix("user") { > post { > entity(as[UserNew]) { user => > (validate(EmailValidator.getInstance().isValid(user.email), s"Invalid > email address ${user.email}") & validate(!user.password.isEmpty, "Password > should not be empty")) { > complete { > UserWire(new ObjectId(), user.firstName, user.lastName, > user.email, user.company, user.role, new ObjectId()) > } > } > } > }} > val myAuthorization: Directive[Tuple1[String]] = > (headerValueByName("X-API-Token") | > parameter("token")).tflatMap[Tuple1[String]]{ > case Tuple1(token) => if (!token.isEmpty) provide(token.reverse) else > complete(StatusCodes.Forbidden -> "API token is not provided") > } > val authenticatedRoutes = myAuthorization { user => > get { complete { "" } }} > val routes = (decodeRequest & encodeResponse) { > authenticatedRoutes ~ nakedRoutes > } > > The idea is that user makes POST /user request, gets token and uses it > for authenticated routes. > > The problem I have is that when validation of email fails I get incorrect > rejection > > Request is missing required HTTP header 'X-API-Token' > > > Why doesn't it throw last occurred rejection ? How does framework pick > rejection out of multiple rejections ? > > -- > >>>>>>>>>> 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 http://groups.google.com/group/akka-user. > For more options, visit https://groups.google.com/d/optout. > -- Akka Team Typesafe - Reactive apps on the JVM Blog: letitcrash.com Twitter: @akkateam -- >>>>>>>>>> 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 http://groups.google.com/group/akka-user. For more options, visit https://groups.google.com/d/optout.
