48 lines
1.2 KiB
Scala
48 lines
1.2 KiB
Scala
/*
|
|
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
|
*/
|
|
|
|
package docs.http.server
|
|
|
|
import akka.actor.ActorSystem
|
|
import akka.http.server.Route
|
|
import akka.stream.FlowMaterializer
|
|
|
|
object MyHandler {
|
|
//# example-1
|
|
import akka.http.model.HttpResponse
|
|
import akka.http.model.StatusCodes._
|
|
import akka.http.server._
|
|
import Directives._
|
|
|
|
implicit def myExceptionHandler =
|
|
ExceptionHandler {
|
|
case e: ArithmeticException =>
|
|
extractUri { uri =>
|
|
logWarning(s"Request to $uri could not be handled normally")
|
|
complete(HttpResponse(InternalServerError, entity = "Bad numbers, bad result!!!"))
|
|
}
|
|
}
|
|
|
|
object MyApp {
|
|
implicit val system = ActorSystem()
|
|
import system.dispatcher
|
|
implicit val materializer = FlowMaterializer()
|
|
|
|
def handler = Route.handlerFlow(`<my-route-definition>`)
|
|
}
|
|
//#
|
|
|
|
def `<my-route-definition>`: Route = null
|
|
def logWarning(str: String): Unit = {}
|
|
}
|
|
|
|
class ExceptionHandlerExamplesSpec extends RoutingSpec {
|
|
import MyHandler._
|
|
|
|
"example" in {
|
|
Get() ~> Route.seal(ctx => ctx.complete((1 / 0).toString)) ~> check {
|
|
responseAs[String] === "Bad numbers, bad result!!!"
|
|
}
|
|
}
|
|
}
|