This is an automated email from the ASF dual-hosted git repository. jamesnetherton pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
The following commit(s) were added to refs/heads/main by this push: new df441da427 Add error handling section to platform-http extension docs df441da427 is described below commit df441da42773c26da44c7fad8839f6cb5d7bb587 Author: James Netherton <jamesnether...@gmail.com> AuthorDate: Mon Jul 8 13:04:41 2024 +0100 Add error handling section to platform-http extension docs --- .../pages/reference/extensions/platform-http.adoc | 42 ++++++++++++++++++++++ .../platform-http/runtime/src/main/doc/usage.adoc | 41 +++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/docs/modules/ROOT/pages/reference/extensions/platform-http.adoc b/docs/modules/ROOT/pages/reference/extensions/platform-http.adoc index 46082f6818..85a1c8b8aa 100644 --- a/docs/modules/ROOT/pages/reference/extensions/platform-http.adoc +++ b/docs/modules/ROOT/pages/reference/extensions/platform-http.adoc @@ -150,6 +150,48 @@ from("platform-http:proxy") + "${headers." + Exchange.HTTP_HOST + "}"); ---- +[id="extensions-platform-http-usage-error-handling"] +=== Error handling + +If you need to customize the reponse returned to the client when exceptions are thrown from your routes, then you can use Camel error handling constucts like `doTry`, `doCatch` and `onException`. + +For example, to configure a global exception handler in response to a specific Exception type being thrown. + +[source,java] +---- +onException(InvalidOrderTotalException.class) + .handled(true) + .setHeader(Exchange.HTTP_RESPONSE_CODE).constant(500) + .setHeader(Exchange.CONTENT_TYPE).constant("text/plain") + .setBody().constant("The order total was not greater than 100"); + +from("platform-http:/orders") + .choice().when().xpath("//order/total > 100") + .to("direct:processOrder") + .otherwise() + .throwException(new InvalidOrderTotalException()); +---- + +You can implement more fine-grained error handling by hooking into the Vert.x Web router initialization with a CDI observer. + +[source,java] +---- +void initRouter(@Observes Router router) { + // Custom 404 handler + router.errorHandler(404, new Handler<RoutingContext>() { + @Override + public void handle(RoutingContext event) { + event.response() + .setStatusCode(404) + .putHeader("Content-Type", "text/plain") + .end("Sorry - resource not found"); + } + }); +} +---- + +Note that care should be taken when modifying the router configuration when extensions such as RestEASY are present, since they may register their own error handling logic. + [id="extensions-platform-http-additional-camel-quarkus-configuration"] == Additional Camel Quarkus configuration diff --git a/extensions/platform-http/runtime/src/main/doc/usage.adoc b/extensions/platform-http/runtime/src/main/doc/usage.adoc index 019884a1d8..a06ac383fd 100644 --- a/extensions/platform-http/runtime/src/main/doc/usage.adoc +++ b/extensions/platform-http/runtime/src/main/doc/usage.adoc @@ -92,3 +92,44 @@ from("platform-http:proxy") .toD("http://" + "${headers." + Exchange.HTTP_HOST + "}"); ---- + +=== Error handling + +If you need to customize the reponse returned to the client when exceptions are thrown from your routes, then you can use Camel error handling constucts like `doTry`, `doCatch` and `onException`. + +For example, to configure a global exception handler in response to a specific Exception type being thrown. + +[source,java] +---- +onException(InvalidOrderTotalException.class) + .handled(true) + .setHeader(Exchange.HTTP_RESPONSE_CODE).constant(500) + .setHeader(Exchange.CONTENT_TYPE).constant("text/plain") + .setBody().constant("The order total was not greater than 100"); + +from("platform-http:/orders") + .choice().when().xpath("//order/total > 100") + .to("direct:processOrder") + .otherwise() + .throwException(new InvalidOrderTotalException()); +---- + +You can implement more fine-grained error handling by hooking into the Vert.x Web router initialization with a CDI observer. + +[source,java] +---- +void initRouter(@Observes Router router) { + // Custom 404 handler + router.errorHandler(404, new Handler<RoutingContext>() { + @Override + public void handle(RoutingContext event) { + event.response() + .setStatusCode(404) + .putHeader("Content-Type", "text/plain") + .end("Sorry - resource not found"); + } + }); +} +---- + +Note that care should be taken when modifying the router configuration when extensions such as RestEASY are present, since they may register their own error handling logic.