Created
July 17, 2015 08:24
-
-
Save magnusart/a3b9321d6e391889d59a to your computer and use it in GitHub Desktop.
Route DSL type errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[error] found : akka.http.scaladsl.server.RequestContext => scala.concurrent.Future[akka.http.scaladsl.server.RouteResult] | |
[error] required: scala.concurrent.Future[akka.http.scaladsl.server.RouteResult] | |
[error] eitherRejectionAsync( upload( ctx.request ) )( executionContext ) { response: HttpResponse ⇒ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def eitherRejection[T]( result: Either[Rejection, T] ): Directive1[T] = result match { | |
case Left( rej ) ⇒ reject( rej ) | |
case Right( t ) ⇒ provide( t ) | |
} | |
def eitherRejectionAsync[T]( result: Future[Either[Rejection, T]] )( | |
implicit | |
executionContext: ExecutionContext | |
): Directive1[T] = onSuccess( result ).flatMap( eitherRejection ) | |
def upload( req: HttpRequest ): Future[Either[Rejection,HttpResponse]] = | |
Source.single( Right( req ) ). | |
via( storageClient.upload( uploadFlow ) ). | |
runWith( Sink.head ) | |
val route:Route = path( "upload" ) { | |
( get & authenticateJwt( validator ) ) { userContext ⇒ ctx ⇒ | |
eitherRejectionAsync( upload( ctx.request ) )( executionContext ) { response: HttpResponse ⇒ | |
complete( response ) | |
} | |
} | |
} |
Aha, thanks! I forgot I could use extractRequest so I figured I'd access it directly. It worked now and thanks for the background as well, it helps a lot.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's how you could rewrite it (stripped the irrelevant parts):
Some best practices:
RequestContext
manually by writing aRoute
manually, it's easy to get it wrong. This is what happened here: By usingctx =>
you signalled that you want to write a route manually (remember thattype Route = RequestContext => Future[RouteResult]
), so you would have to return aFuture[RouteResult]
but you tried to return aRoute
(the result ofeitherRejectionAsync(...).apply(res => complete(response))
). You could fix it by actually executing the route withctx => eitherRejectionAsync(...).apply(res => complete(response)).apply(ctx)
but if you find yourself doing that, you can just removectx => ....apply(ctx)
.RequestContext
manually, use extraction directives to access parts of the request context you are interested inExecutionContext
when working with routes: if necessary (which it seems wasn't here), you can always extract one from theRequestContext
. Use theextractExecutionContext {implicit ec => }
directive. (Same for the materializer.)HTH