2015-05-11 23:05:18 +02:00
|
|
|
/*
|
2016-02-23 12:58:39 +01:00
|
|
|
* Copyright (C) 2009-2016 Lightbend Inc. <http://www.lightbend.com>
|
2015-05-11 23:05:18 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package docs.http.scaladsl.server
|
|
|
|
|
|
|
|
|
|
// format: OFF
|
|
|
|
|
|
|
|
|
|
object MyExplicitExceptionHandler {
|
|
|
|
|
|
|
|
|
|
//#explicit-handler-example
|
|
|
|
|
import akka.actor.ActorSystem
|
2015-06-23 18:28:53 +02:00
|
|
|
import akka.stream.ActorMaterializer
|
2015-05-11 23:05:18 +02:00
|
|
|
import akka.http.scaladsl.Http
|
|
|
|
|
import akka.http.scaladsl.model._
|
|
|
|
|
import akka.http.scaladsl.server._
|
|
|
|
|
import StatusCodes._
|
|
|
|
|
import Directives._
|
|
|
|
|
|
|
|
|
|
val myExceptionHandler = ExceptionHandler {
|
|
|
|
|
case _: ArithmeticException =>
|
|
|
|
|
extractUri { uri =>
|
|
|
|
|
println(s"Request to $uri could not be handled normally")
|
|
|
|
|
complete(HttpResponse(InternalServerError, entity = "Bad numbers, bad result!!!"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object MyApp extends App {
|
|
|
|
|
implicit val system = ActorSystem()
|
2015-06-23 18:28:53 +02:00
|
|
|
implicit val materializer = ActorMaterializer()
|
2015-05-11 23:05:18 +02:00
|
|
|
|
|
|
|
|
val route: Route =
|
|
|
|
|
handleExceptions(myExceptionHandler) {
|
|
|
|
|
// ... some route structure
|
|
|
|
|
null // hide
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Http().bindAndHandle(route, "localhost", 8080)
|
|
|
|
|
}
|
|
|
|
|
//#
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object MyImplicitExceptionHandler {
|
|
|
|
|
|
|
|
|
|
//#implicit-handler-example
|
|
|
|
|
import akka.actor.ActorSystem
|
2015-06-23 18:28:53 +02:00
|
|
|
import akka.stream.ActorMaterializer
|
2015-05-11 23:05:18 +02:00
|
|
|
import akka.http.scaladsl.Http
|
|
|
|
|
import akka.http.scaladsl.model._
|
|
|
|
|
import akka.http.scaladsl.server._
|
|
|
|
|
import StatusCodes._
|
|
|
|
|
import Directives._
|
|
|
|
|
|
|
|
|
|
implicit def myExceptionHandler: ExceptionHandler =
|
|
|
|
|
ExceptionHandler {
|
|
|
|
|
case _: ArithmeticException =>
|
|
|
|
|
extractUri { uri =>
|
|
|
|
|
println(s"Request to $uri could not be handled normally")
|
|
|
|
|
complete(HttpResponse(InternalServerError, entity = "Bad numbers, bad result!!!"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object MyApp extends App {
|
|
|
|
|
implicit val system = ActorSystem()
|
2015-06-23 18:28:53 +02:00
|
|
|
implicit val materializer = ActorMaterializer()
|
2015-05-11 23:05:18 +02:00
|
|
|
|
|
|
|
|
val route: Route =
|
|
|
|
|
// ... some route structure
|
|
|
|
|
null // hide
|
|
|
|
|
|
|
|
|
|
Http().bindAndHandle(route, "localhost", 8080)
|
|
|
|
|
}
|
|
|
|
|
//#
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ExceptionHandlerExamplesSpec extends RoutingSpec {
|
|
|
|
|
|
|
|
|
|
"test explicit example" in {
|
2015-10-09 15:19:36 +02:00
|
|
|
// tests:
|
2015-05-11 23:05:18 +02:00
|
|
|
Get() ~> handleExceptions(MyExplicitExceptionHandler.myExceptionHandler) {
|
|
|
|
|
_.complete((1 / 0).toString)
|
|
|
|
|
} ~> check {
|
|
|
|
|
responseAs[String] === "Bad numbers, bad result!!!"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"test implicit example" in {
|
|
|
|
|
import akka.http.scaladsl.server._
|
|
|
|
|
import MyImplicitExceptionHandler.myExceptionHandler
|
2015-10-09 15:19:36 +02:00
|
|
|
// tests:
|
2015-05-11 23:05:18 +02:00
|
|
|
Get() ~> Route.seal(ctx => ctx.complete((1 / 0).toString)) ~> check {
|
|
|
|
|
responseAs[String] === "Bad numbers, bad result!!!"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|