This commit is contained in:
parent
f49708d8b7
commit
ab83603733
3 changed files with 79 additions and 2 deletions
|
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
* Copyright (C) 2016-2016 Lightbend Inc. <http://www.lightbend.com>
|
||||
*/
|
||||
package docs.http.javadsl.server.directives;
|
||||
|
||||
import akka.http.javadsl.model.HttpRequest;
|
||||
import akka.http.javadsl.model.StatusCodes;
|
||||
import akka.http.javadsl.server.ExceptionHandler;
|
||||
import akka.http.javadsl.server.PathMatchers;
|
||||
import akka.http.javadsl.server.RejectionHandler;
|
||||
import akka.http.javadsl.server.Rejections;
|
||||
import akka.http.javadsl.server.Route;
|
||||
import akka.http.javadsl.server.ValidationRejection;
|
||||
import akka.http.javadsl.testkit.JUnitRouteTest;
|
||||
import org.junit.Test;
|
||||
|
||||
import static akka.http.javadsl.server.PathMatchers.integerSegment;
|
||||
|
||||
public class ExecutionDirectivesExamplesTest extends JUnitRouteTest {
|
||||
|
||||
@Test
|
||||
public void testHandleExceptions() {
|
||||
//#handleExceptions
|
||||
final ExceptionHandler divByZeroHandler = ExceptionHandler.newBuilder()
|
||||
.match(ArithmeticException.class, x ->
|
||||
complete(StatusCodes.BAD_REQUEST, "You've got your arithmetic wrong, fool!"))
|
||||
.build();
|
||||
|
||||
final Route route =
|
||||
path(PathMatchers.segment("divide").slash(integerSegment()).slash(integerSegment()), (a, b) ->
|
||||
handleExceptions(divByZeroHandler, () -> complete("The result is " + (a / b)))
|
||||
);
|
||||
|
||||
// tests:
|
||||
testRoute(route).run(HttpRequest.GET("/divide/10/5"))
|
||||
.assertEntity("The result is 2");
|
||||
testRoute(route).run(HttpRequest.GET("/divide/10/0"))
|
||||
.assertStatusCode(StatusCodes.BAD_REQUEST)
|
||||
.assertEntity("You've got your arithmetic wrong, fool!");
|
||||
//#handleExceptions
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandleRejections() {
|
||||
//#handleRejections
|
||||
final RejectionHandler totallyMissingHandler = RejectionHandler.newBuilder()
|
||||
.handleNotFound(complete(StatusCodes.NOT_FOUND, "Oh man, what you are looking for is long gone."))
|
||||
.handle(ValidationRejection.class, r -> complete(StatusCodes.INTERNAL_SERVER_ERROR, r.message()))
|
||||
.build();
|
||||
|
||||
final Route route = pathPrefix("handled", () ->
|
||||
handleRejections(totallyMissingHandler, () ->
|
||||
route(
|
||||
path("existing", () -> complete("This path exists")),
|
||||
path("boom", () -> reject(Rejections.validationRejection("This didn't work.")))
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
// tests:
|
||||
testRoute(route).run(HttpRequest.GET("/handled/existing"))
|
||||
.assertEntity("This path exists");
|
||||
// applies default handler
|
||||
testRoute(route).run(HttpRequest.GET("/missing"))
|
||||
.assertStatusCode(StatusCodes.NOT_FOUND)
|
||||
.assertEntity("The requested resource could not be found.");
|
||||
testRoute(route).run(HttpRequest.GET("/handled/missing"))
|
||||
.assertStatusCode(StatusCodes.NOT_FOUND)
|
||||
.assertEntity("Oh man, what you are looking for is long gone.");
|
||||
testRoute(route).run(HttpRequest.GET("/handled/boom"))
|
||||
.assertStatusCode(StatusCodes.INTERNAL_SERVER_ERROR)
|
||||
.assertEntity("This didn't work.");
|
||||
//#handleRejections
|
||||
}
|
||||
}
|
||||
|
|
@ -14,4 +14,5 @@ See :ref:`exception-handling-java` for general information about options for han
|
|||
|
||||
Example
|
||||
-------
|
||||
TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 <https://github.com/akka/akka/issues/20466>`_.
|
||||
|
||||
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/ExecutionDirectivesExamplesTest.java#handleExceptions
|
||||
|
|
|
|||
|
|
@ -13,4 +13,5 @@ See :ref:`rejections-java` for general information about options for handling re
|
|||
|
||||
Example
|
||||
-------
|
||||
TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 <https://github.com/akka/akka/issues/20466>`_.
|
||||
|
||||
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/ExecutionDirectivesExamplesTest.java#handleRejections
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue