=doc add custom ValidationRejection handling to samples
This commit is contained in:
parent
dcfa56e547
commit
565b480b2e
2 changed files with 10 additions and 1 deletions
|
|
@ -25,6 +25,9 @@ object MyRejectionHandler {
|
||||||
.handle { case AuthorizationFailedRejection ⇒
|
.handle { case AuthorizationFailedRejection ⇒
|
||||||
complete((Forbidden, "You're out of your depth!"))
|
complete((Forbidden, "You're out of your depth!"))
|
||||||
}
|
}
|
||||||
|
.handle { case ValidationRejection(msg, _) ⇒
|
||||||
|
complete((InternalServerError, "That wasn't valid! " + msg))
|
||||||
|
}
|
||||||
.handleAll[MethodRejection] { methodRejections ⇒
|
.handleAll[MethodRejection] { methodRejections ⇒
|
||||||
val names = methodRejections.map(_.supported.name)
|
val names = methodRejections.map(_.supported.name)
|
||||||
complete((MethodNotAllowed, s"Can't do that! Supported: ${names mkString " or "}!"))
|
complete((MethodNotAllowed, s"Can't do that! Supported: ${names mkString " or "}!"))
|
||||||
|
|
|
||||||
|
|
@ -32,11 +32,13 @@ class ExecutionDirectivesExamplesSpec extends RoutingSpec {
|
||||||
"handleRejections" in {
|
"handleRejections" in {
|
||||||
val totallyMissingHandler = RejectionHandler.newBuilder()
|
val totallyMissingHandler = RejectionHandler.newBuilder()
|
||||||
.handleNotFound { complete((StatusCodes.NotFound, "Oh man, what you are looking for is long gone.")) }
|
.handleNotFound { complete((StatusCodes.NotFound, "Oh man, what you are looking for is long gone.")) }
|
||||||
|
.handle { case ValidationRejection(msg, _) => complete((StatusCodes.InternalServerError, msg)) }
|
||||||
.result()
|
.result()
|
||||||
val route =
|
val route =
|
||||||
pathPrefix("handled") {
|
pathPrefix("handled") {
|
||||||
handleRejections(totallyMissingHandler) {
|
handleRejections(totallyMissingHandler) {
|
||||||
path("existing")(complete("This path exists"))
|
path("existing")(complete("This path exists")) ~
|
||||||
|
path("boom")(reject(new ValidationRejection("This didn't work.")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -52,5 +54,9 @@ class ExecutionDirectivesExamplesSpec extends RoutingSpec {
|
||||||
status shouldEqual StatusCodes.NotFound
|
status shouldEqual StatusCodes.NotFound
|
||||||
responseAs[String] shouldEqual "Oh man, what you are looking for is long gone."
|
responseAs[String] shouldEqual "Oh man, what you are looking for is long gone."
|
||||||
}
|
}
|
||||||
|
Get("/handled/boom") ~> route ~> check {
|
||||||
|
status shouldEqual StatusCodes.InternalServerError
|
||||||
|
responseAs[String] shouldEqual "This didn't work."
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue