Skip to content

Commit

Permalink
docs: update article to describe new methods in ErrorEvent (vaadin#2948)
Browse files Browse the repository at this point in the history
* docs: update article to describe new methods in ErrorEvent

Adds documentation for two new methods in ErrorEvent: getComponent() and getElement().

Related-to: vaadin/flow#13044

* Update articles/advanced/custom-error-handler.asciidoc

Co-authored-by: caalador <[email protected]>

* Update articles/advanced/custom-error-handler.asciidoc

Co-authored-by: caalador <[email protected]>

* First pass at editing.

* Second pass at editing.

---------

Co-authored-by: caalador <[email protected]>
Co-authored-by: Russell JT Dyer <[email protected]>
Co-authored-by: Mikhail Shabarov <[email protected]>
  • Loading branch information
4 people authored Nov 13, 2023
1 parent a85003c commit e36751e
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions articles/advanced/custom-error-handler.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ order: 145

You can customize the error views related to navigation between different routes, as described in the <<{articles}/routing/exceptions#, Router Exception Handling>> documentation page.

For other unhandled runtime exceptions, an [classname]`ErrorHandler` class can be used to let users know that something went wrong. To do so, first create a custom [classname]`ErrorHandler` class, then use that class to override the default error handler.
For other unhandled runtime exceptions, an [classname]`ErrorHandler` class can be used to let users know that something went wrong. To do so, first create a custom [classname]`ErrorHandler` class. Then use that class to override the default error handler.

For example, the following [classname]`CustomErrorHandler` class logs the error and displays a notification to the user:

Expand Down Expand Up @@ -41,9 +41,21 @@ VaadinSession.getCurrent().setErrorHandler(new CustomErrorHandler());

To apply the custom error handler to all user sessions, you can use a [classname]`SessionInitListener`, which receives an event each time a new [classname]`VaadinSession` is initialized. See the <<session-and-ui-init-listener#,Session and UI Listeners>> documentation page to learn how to create a [classname]`SessionInitListener`.


[role="since:com.vaadin:[email protected]"]
== Showing Error Parameter Views for Non-Navigation Exceptions

[classname]`ErrorEvent` has method [methodname]`getComponent()` to get the handled [classname]`Component`, and the [methodname]`getElement()` method to get the handled [classname]`Element` when the error is thrown if available.

For example, the following button click listener would have [classname]`ErrorEvent` [methodname]`getComponent()` return the clicked button:

[source,java]
----
Button button = new Button("Click me", event -> {
throw new IllegalArgumentException("No clicking");
});
----


== Show Error Parameter Views for Non-Navigation Exceptions

In the [classname]`DefaultErrorHandler`, it's possible to enable transitioning to an [interfacename]`HasErrorParameter<T extends Exception>` error view on exceptions. This is done by setting the `enableErrorHandlerRedirect` parameter to `true`. See <<{articles}/routing/exceptions#, Error Resolving>> for more information on the [interfacename]`HasErrorParameter`.

Expand Down

0 comments on commit e36751e

Please sign in to comment.