You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

application-errors.asciidoc 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. ---
  2. title: Handling Errors
  3. order: 6
  4. layout: page
  5. ---
  6. [[application.errors]]
  7. = Handling Errors
  8. [[application.errors.error-indicator]]
  9. == Error Indicator and Message
  10. All components have a built-in error indicator that is turned on if validating
  11. the component fails, and can be set explicitly with
  12. [methodname]#setComponentError()#. Usually, the error indicator is placed right
  13. of the component caption. The error indicator is part of the component caption,
  14. so its placement is usually managed by the layout in which the component is
  15. contained, but some components handle it themselves. Hovering the mouse pointer
  16. over the field displays the error message.
  17. [source, java]
  18. ----
  19. textfield.setComponentError(new UserError("Bad value"));
  20. button.setComponentError(new UserError("Bad click"));
  21. ----
  22. The result is shown in <<figure.application.errors.error-indicator>>.
  23. [[figure.application.errors.error-indicator]]
  24. .Error Indicator Active
  25. image::img/errorindicator-example2.png[scaledwidth=40%]
  26. [[application.errors.reconnect]]
  27. == Connection Fault
  28. If the connection to the server is lost, Vaadin application shows a "lost connection" notification
  29. and tries to restore the connection. After several retries, an error message is shown.
  30. You can customize the messages, timeouts, and the number of reconnect attempts in the [classname]#ReconnectDialogConfiguration#
  31. object, which you can access from your [classname]#UI# with [methodname]#getReconnectDialogConfiguration()#.
  32. [[application.errors.systemmessages]]
  33. == Customizing System Messages
  34. System messages are notifications that indicate a major invalid state that
  35. usually requires restarting the application. Session timeout is perhaps the most
  36. typical such state.
  37. System messages are strings managed in the [classname]#SystemMessages# class.
  38. Each message has four properties: a short caption, the actual message, a URL to
  39. which to redirect after displaying the message, and property indicating whether
  40. the notification is enabled.
  41. You can override the default system messages by setting the
  42. [interfacename]#SystemMessagesProvider# in the [classname]#VaadinService#. You
  43. need to implement the [methodname]#getSystemMessages()# method, which should
  44. return a [classname]#SystemMessages# object. The easiest way to customize the
  45. messages is to use a [classname]#CustomizedSystemMessages# object.
  46. You can set the system message provider in the
  47. [methodname]#servletInitialized()# method of a custom servlet class, for example
  48. as follows:
  49. [source, java]
  50. ----
  51. getService().setSystemMessagesProvider(
  52. new SystemMessagesProvider() {
  53. @Override
  54. public SystemMessages getSystemMessages(
  55. SystemMessagesInfo systemMessagesInfo) {
  56. CustomizedSystemMessages messages =
  57. new CustomizedSystemMessages();
  58. messages.setCommunicationErrorCaption("Comm Err");
  59. messages.setCommunicationErrorMessage("This is bad.");
  60. messages.setCommunicationErrorNotificationEnabled(true);
  61. messages.setCommunicationErrorURL("http://vaadin.com/");
  62. return messages;
  63. }
  64. });
  65. ----
  66. See
  67. <<application-lifecycle#application.lifecycle.servlet-service,"Vaadin
  68. Servlet, Portlet, and Service">> for information about customizing Vaadin
  69. servlets.
  70. [[application.errors.unchecked-exceptions]]
  71. == Handling Uncaught Exceptions
  72. Handling events can result in exceptions either in the application logic or in
  73. the framework itself, but some of them may not be caught properly by the
  74. application. Any such exceptions are eventually caught by the framework. It
  75. delegates the exceptions to the [classname]#DefaultErrorHandler#, which displays
  76. the error as a component error, that is, with a small red "!" -sign (depending
  77. on the theme). If the user hovers the mouse pointer over it, the entire
  78. backtrace of the exception is shown in a large tooltip box.
  79. You can customize the default error handling by implementing a custom
  80. [interfacename]#ErrorHandler# and enabling it with
  81. [methodname]#setErrorHandler()# in any of the components in the component
  82. hierarchy, including the [classname]#UI#, or in the [classname]#VaadinSession#
  83. object. You can either implement the [interfacename]#ErrorHandler# or extend the
  84. [classname]#DefaultErrorHandler#. In the following example, we modify the
  85. behavior of the default handler.
  86. [source, java]
  87. ----
  88. // Here's some code that produces an uncaught exception
  89. final VerticalLayout layout = new VerticalLayout();
  90. final Button button = new Button("Click Me!", event ->
  91. ((String)null).length()); // Null-pointer exception
  92. layout.addComponent(button);
  93. // Configure the error handler for the UI
  94. UI.getCurrent().setErrorHandler(new DefaultErrorHandler() {
  95. @Override
  96. public void error(com.vaadin.server.ErrorEvent event) {
  97. // Find the final cause
  98. String cause = "<b>The click failed because:</b><br/>";
  99. for (Throwable t = event.getThrowable(); t != null;
  100. t = t.getCause())
  101. if (t.getCause() == null) // We're at final cause
  102. cause += t.getClass().getName() + "<br/>";
  103. // Display the error message in a custom fashion
  104. layout.addComponent(new Label(cause, ContentMode.HTML));
  105. // Do the default error handling (optional)
  106. doDefault(event);
  107. }
  108. });
  109. ----
  110. The above example also demonstrates how to dig up the final cause from the cause
  111. stack.
  112. When extending [classname]#DefaultErrorHandler#, you can call
  113. [methodname]#doDefault()# as was done above to run the default error handling,
  114. such as set the component error for the component where the exception was
  115. thrown. See the source code of the implementation for more details. You can call
  116. [methodname]#findAbstractComponent(event)# to find the component that caused the
  117. error. If the error is not associated with a component, it returns null.