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 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. ifdef::web[]
  33. [[application.errors.systemmessages]]
  34. == Customizing System Messages
  35. System messages are notifications that indicate a major invalid state that
  36. usually requires restarting the application. Session timeout is perhaps the most
  37. typical such state.
  38. System messages are strings managed in the [classname]#SystemMessages# class.
  39. sessionExpired:: ((("session",
  40. "expiration")))
  41. ((("session",
  42. "timeout")))
  43. The Vaadin session expired. A session expires if no server requests are made
  44. during the session timeout period. The session timeout can be configured with
  45. the [parameter]#session-timeout# parameter in [filename]#web.xml#, as described
  46. in
  47. <<dummy/../../../framework/application/application-environment#application.environment.web-xml,"Using
  48. a web.xml Deployment Descriptor">>.
  49. communicationError:: An unspecified communication problem between the Vaadin Client-Side Engine and
  50. the application server. The server may be unavailable or there is some other
  51. problem.
  52. authenticationError:: This error occurs if 401 (Unauthorized) response to a request is received from
  53. the server.
  54. internalError:: A serious internal problem, possibly indicating a bug in Vaadin Client-Side
  55. Engine or in some custom client-side code.
  56. cookiesDisabled:: Informs the user that cookies are disabled in the browser and the application
  57. does not work without them.
  58. Each message has four properties: a short caption, the actual message, a URL to
  59. which to redirect after displaying the message, and property indicating whether
  60. the notification is enabled.
  61. Additional details may be written (in English) to the debug console window
  62. described in
  63. <<dummy/../../../framework/advanced/advanced-debug#advanced.debug,"Debug Mode
  64. and Window">>.
  65. You can override the default system messages by setting the
  66. [interfacename]#SystemMessagesProvider# in the [classname]#VaadinService#. You
  67. need to implement the [methodname]#getSystemMessages()# method, which should
  68. return a [classname]#SystemMessages# object. The easiest way to customize the
  69. messages is to use a [classname]#CustomizedSystemMessages# object.
  70. You can set the system message provider in the
  71. [methodname]#servletInitialized()# method of a custom servlet class, for example
  72. as follows:
  73. [source, java]
  74. ----
  75. getService().setSystemMessagesProvider(
  76. new SystemMessagesProvider() {
  77. @Override
  78. public SystemMessages getSystemMessages(
  79. SystemMessagesInfo systemMessagesInfo) {
  80. CustomizedSystemMessages messages =
  81. new CustomizedSystemMessages();
  82. messages.setCommunicationErrorCaption("Comm Err");
  83. messages.setCommunicationErrorMessage("This is bad.");
  84. messages.setCommunicationErrorNotificationEnabled(true);
  85. messages.setCommunicationErrorURL("http://vaadin.com/");
  86. return messages;
  87. }
  88. });
  89. ----
  90. See
  91. <<dummy/../../../framework/application/application-lifecycle#application.lifecycle.servlet-service,"Vaadin
  92. Servlet, Portlet, and Service">> for information about customizing Vaadin
  93. servlets.
  94. endif::web[]
  95. ifdef::web[]
  96. [[application.errors.unchecked-exceptions]]
  97. == Handling Uncaught Exceptions
  98. Handling events can result in exceptions either in the application logic or in
  99. the framework itself, but some of them may not be caught properly by the
  100. application. Any such exceptions are eventually caught by the framework. It
  101. delegates the exceptions to the [classname]#DefaultErrorHandler#, which displays
  102. the error as a component error, that is, with a small red "!" -sign (depending
  103. on the theme). If the user hovers the mouse pointer over it, the entire
  104. backtrace of the exception is shown in a large tooltip box, as illustrated in
  105. <<figure.application.errors.unchecked-exceptions>>.
  106. [[figure.application.errors.unchecked-exceptions]]
  107. .Uncaught Exception in Component Error Indicator
  108. image::img/errorindicator-exception.png[]
  109. You can customize the default error handling by implementing a custom
  110. [interfacename]#ErrorHandler# and enabling it with
  111. [methodname]#setErrorHandler()# in any of the components in the component
  112. hierarchy, including the [classname]#UI#, or in the [classname]#VaadinSession#
  113. object. You can either implement the [interfacename]#ErrorHandler# or extend the
  114. [classname]#DefaultErrorHandler#. In the following example, we modify the
  115. behavior of the default handler.
  116. [source, java]
  117. ----
  118. // Here's some code that produces an uncaught exception
  119. final VerticalLayout layout = new VerticalLayout();
  120. final Button button = new Button("Click Me!",
  121. new Button.ClickListener() {
  122. public void buttonClick(ClickEvent event) {
  123. ((String)null).length(); // Null-pointer exception
  124. }
  125. });
  126. layout.addComponent(button);
  127. // Configure the error handler for the UI
  128. UI.getCurrent().setErrorHandler(new DefaultErrorHandler() {
  129. @Override
  130. public void error(com.vaadin.server.ErrorEvent event) {
  131. // Find the final cause
  132. String cause = "<b>The click failed because:</b><br/>";
  133. for (Throwable t = event.getThrowable(); t != null;
  134. t = t.getCause())
  135. if (t.getCause() == null) // We're at final cause
  136. cause += t.getClass().getName() + "<br/>";
  137. // Display the error message in a custom fashion
  138. layout.addComponent(new Label(cause, ContentMode.HTML));
  139. // Do the default error handling (optional)
  140. doDefault(event);
  141. }
  142. });
  143. ----
  144. The above example also demonstrates how to dig up the final cause from the cause
  145. stack.
  146. When extending [classname]#DefaultErrorHandler#, you can call
  147. [methodname]#doDefault()# as was done above to run the default error handling,
  148. such as set the component error for the component where the exception was
  149. thrown. See the source code of the implementation for more details. You can call
  150. [methodname]#findAbstractComponent(event)# to find the component that caused the
  151. error. If the error is not associated with a component, it returns null.
  152. endif::web[]