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.

ErrorEvent.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.server;
  17. import java.io.Serializable;
  18. import com.vaadin.shared.Connector;
  19. import com.vaadin.ui.UI;
  20. /**
  21. * An error thrown by the framework and handled by an {@link ErrorHandler}.
  22. * Typically handled by {@link VaadinSession#getErrorHandler()} but can also be
  23. * handled by a {@link Connector} specific handler, set using
  24. * {@link ClientConnector#setErrorHandler(ErrorHandler)}.
  25. *
  26. */
  27. public class ErrorEvent implements Serializable {
  28. private Throwable throwable;
  29. public ErrorEvent(Throwable t) {
  30. setThrowable(t);
  31. }
  32. /**
  33. * Gets the contained throwable, the cause of the error.
  34. *
  35. * @return
  36. */
  37. public Throwable getThrowable() {
  38. return throwable;
  39. }
  40. public void setThrowable(Throwable throwable) {
  41. this.throwable = throwable;
  42. }
  43. /**
  44. * Method for finding the error handler for the given connector. Uses
  45. * connector hierarchy to find a connector with an error handler. Falls back
  46. * to the VaadinSession error handler if no connector has specified an error
  47. * handler.
  48. * <p>
  49. * Returns a {@link DefaultErrorHandler} if no error handler was found
  50. * </p>
  51. *
  52. * @param connector
  53. * The target connector
  54. * @return An ErrorHandler for the connector
  55. */
  56. public static ErrorHandler findErrorHandler(ClientConnector connector) {
  57. if (connector != null) {
  58. ErrorHandler errorHandler = connector.getErrorHandler();
  59. if (errorHandler != null) {
  60. return errorHandler;
  61. }
  62. ClientConnector parent = connector.getParent();
  63. if (parent != null) {
  64. return findErrorHandler(parent);
  65. }
  66. /*
  67. * Reached UI and found no error handler. Try session which
  68. * typically has one.
  69. */
  70. UI ui = connector.getUI();
  71. if (ui != null) {
  72. errorHandler = findErrorHandler(ui.getSession());
  73. if (errorHandler != null) {
  74. return errorHandler;
  75. }
  76. }
  77. }
  78. /*
  79. * No connector known or the connector is not attached to a session. Try
  80. * the current session
  81. */
  82. if (VaadinSession.getCurrent() != null) {
  83. ErrorHandler errorHandler = VaadinSession.getCurrent()
  84. .getErrorHandler();
  85. if (errorHandler != null) {
  86. return errorHandler;
  87. }
  88. }
  89. /*
  90. * We should never really get here as at least the session should have
  91. * an error handler. If for some reason it does not we use the default
  92. * error handler.
  93. */
  94. return new DefaultErrorHandler();
  95. }
  96. /**
  97. * Method for finding the error handler for the given session.
  98. *
  99. * @param connector
  100. * The target connector
  101. *
  102. * @return An ErrorHandler for the session or null if none was found
  103. */
  104. public static ErrorHandler findErrorHandler(VaadinSession session) {
  105. if (session == null) {
  106. return null;
  107. }
  108. return session.getErrorHandler();
  109. }
  110. }