+htj #16436 add ExecutionDirectives.handleExceptions
This commit is contained in:
parent
30e4acacfa
commit
45a410b02a
5 changed files with 30 additions and 2 deletions
|
|
@ -7,6 +7,8 @@ package akka.http.javadsl.server.directives;
|
|||
import org.junit.Test;
|
||||
import scala.Option;
|
||||
import scala.concurrent.Future;
|
||||
|
||||
import akka.http.javadsl.server.*;
|
||||
import akka.http.javadsl.model.HttpRequest;
|
||||
import akka.http.javadsl.model.headers.Authorization;
|
||||
import akka.http.javadsl.testkit.*;
|
||||
|
|
|
|||
|
|
@ -119,6 +119,12 @@ private[http] object RouteImplementation extends Directives with server.RouteCon
|
|||
case Conditional(eTag, lastModified, children) ⇒
|
||||
conditional(eTag.asScala, lastModified.asScala).apply(apply(RouteAlternatives(children)))
|
||||
|
||||
case HandleExceptions(handler, children) ⇒
|
||||
val pf: akka.http.scaladsl.server.ExceptionHandler = akka.http.scaladsl.server.ExceptionHandler {
|
||||
case e: RuntimeException ⇒ apply(handler.handle(e))
|
||||
}
|
||||
handleExceptions(pf).apply(apply(RouteAlternatives(children)))
|
||||
|
||||
case o: OpaqueRoute ⇒
|
||||
(ctx ⇒ o.handle(new RequestContextImpl(ctx)).asInstanceOf[RouteResultImpl].underlying)
|
||||
|
||||
|
|
|
|||
|
|
@ -40,6 +40,8 @@ private[http] object RouteStructure {
|
|||
|
||||
case class Conditional(entityTag: EntityTag, lastModified: DateTime, children: immutable.Seq[Route]) extends DirectiveRoute
|
||||
|
||||
case class HandleExceptions(handler: ExceptionHandler, children: immutable.Seq[Route]) extends DirectiveRoute
|
||||
|
||||
abstract class OpaqueRoute(extractions: RequestVal[_]*) extends Route {
|
||||
def handle(ctx: RequestContext): RouteResult
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,8 +4,10 @@
|
|||
|
||||
package akka.http.javadsl.server.directives
|
||||
|
||||
import java.lang.reflect.Method
|
||||
import scala.annotation.varargs
|
||||
import java.lang.reflect.Method
|
||||
|
||||
import akka.http.javadsl.model.{ StatusCode, HttpResponse }
|
||||
import akka.http.javadsl.server._
|
||||
import akka.http.impl.server.RouteStructure._
|
||||
import akka.http.impl.server._
|
||||
|
|
@ -26,6 +28,22 @@ abstract class BasicDirectives {
|
|||
def handle(ctx: RequestContext): RouteResult = ctx.complete(text)
|
||||
}
|
||||
|
||||
/**
|
||||
* A route that completes the request with a static text
|
||||
*/
|
||||
def complete(response: HttpResponse): Route =
|
||||
new OpaqueRoute() {
|
||||
def handle(ctx: RequestContext): RouteResult = ctx.complete(response)
|
||||
}
|
||||
|
||||
/**
|
||||
* A route that completes the request with a status code.
|
||||
*/
|
||||
def completeWithStatus(code: StatusCode): Route =
|
||||
new OpaqueRoute() {
|
||||
def handle(ctx: RequestContext): RouteResult = ctx.completeWithStatus(code)
|
||||
}
|
||||
|
||||
/**
|
||||
* A route that completes the request using the given marshaller and value.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -72,7 +72,7 @@ object FileAndResourceRoute {
|
|||
}
|
||||
}
|
||||
|
||||
abstract class FileAndResourceDirectives extends CodingDirectives {
|
||||
abstract class FileAndResourceDirectives extends ExecutionDirectives {
|
||||
/**
|
||||
* Completes GET requests with the content of the given resource loaded from the default ClassLoader.
|
||||
* If the resource cannot be found or read the Route rejects the request.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue