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

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