您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DefaultErrorHandler.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2000-2018 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.lang.reflect.InvocationTargetException;
  18. import java.net.SocketException;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import com.vaadin.event.ListenerMethod.MethodException;
  22. import com.vaadin.server.ClientConnector.ConnectorErrorEvent;
  23. import com.vaadin.server.ServerRpcManager.RpcInvocationException;
  24. import com.vaadin.shared.Connector;
  25. import com.vaadin.ui.AbstractComponent;
  26. import com.vaadin.ui.Component;
  27. public class DefaultErrorHandler implements ErrorHandler {
  28. @Override
  29. public void error(ErrorEvent event) {
  30. doDefault(event);
  31. }
  32. public static void doDefault(ErrorEvent event) {
  33. Throwable t = event.getThrowable();
  34. if (t instanceof SocketException) {
  35. // Most likely client browser closed socket
  36. getLogger().info("SocketException in CommunicationManager."
  37. + " Most likely client (browser) closed socket.");
  38. return;
  39. }
  40. t = findRelevantThrowable(t);
  41. // Finds the original source of the error/exception
  42. AbstractComponent component = findAbstractComponent(event);
  43. if (component != null) {
  44. // Shows the error in AbstractComponent
  45. ErrorMessage errorMessage = AbstractErrorMessage
  46. .getErrorMessageForException(t);
  47. component.setComponentError(errorMessage);
  48. }
  49. // also print the error on console
  50. getLogger().log(Level.SEVERE, "", t);
  51. }
  52. /**
  53. * Vaadin wraps exceptions in its own and due to reflection usage there
  54. * might be also other irrelevant exceptions that make no sense for Vaadin
  55. * users (~developers using Vaadin). This method tries to choose the
  56. * relevant one to be reported.
  57. *
  58. * @since 7.2
  59. * @param t
  60. * a throwable passed to ErrorHandler
  61. * @return the throwable that is relevant for Vaadin users
  62. */
  63. public static Throwable findRelevantThrowable(Throwable t) {
  64. try {
  65. if ((t instanceof RpcInvocationException)
  66. && (t.getCause() instanceof InvocationTargetException)) {
  67. /*
  68. * RpcInvocationException (that always wraps irrelevant
  69. * java.lang.reflect.InvocationTargetException) might only be
  70. * relevant for core Vaadin developers.
  71. */
  72. return findRelevantThrowable(t.getCause().getCause());
  73. } else if (t instanceof MethodException) {
  74. /*
  75. * Method exception might only be relevant for core Vaadin
  76. * developers.
  77. */
  78. return t.getCause();
  79. }
  80. } catch (Exception e) {
  81. // NOP, just return the original one
  82. }
  83. return t;
  84. }
  85. private static Logger getLogger() {
  86. return Logger.getLogger(DefaultErrorHandler.class.getName());
  87. }
  88. /**
  89. * Returns the AbstractComponent associated with the given error if such can
  90. * be found.
  91. *
  92. * @param event
  93. * The error to investigate
  94. * @return The {@link AbstractComponent} to error relates to or null if
  95. * could not be determined or if the error does not relate to any
  96. * AbstractComponent.
  97. */
  98. public static AbstractComponent findAbstractComponent(
  99. com.vaadin.server.ErrorEvent event) {
  100. if (event instanceof ConnectorErrorEvent) {
  101. Component c = findComponent(
  102. ((ConnectorErrorEvent) event).getConnector());
  103. if (c instanceof AbstractComponent) {
  104. return (AbstractComponent) c;
  105. }
  106. }
  107. return null;
  108. }
  109. /**
  110. * Finds the nearest component by traversing upwards in the hierarchy. If
  111. * connector is a Component, that Component is returned. Otherwise, looks
  112. * upwards in the hierarchy until it finds a {@link Component}.
  113. *
  114. * @return A Component or null if no component was found
  115. */
  116. public static Component findComponent(Connector connector) {
  117. if (connector instanceof Component) {
  118. return (Component) connector;
  119. }
  120. if (connector.getParent() != null) {
  121. return findComponent(connector.getParent());
  122. }
  123. return null;
  124. }
  125. }