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.

ErrorHandlingRunnable.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.io.Serializable;
  18. import java.util.Objects;
  19. /**
  20. * Defines the interface to handle exceptions thrown during the execution of a
  21. * FutureAccess.
  22. *
  23. * @since 7.1.8
  24. * @author Vaadin Ltd
  25. */
  26. public interface ErrorHandlingRunnable extends Runnable, Serializable {
  27. /**
  28. * Handles exceptions thrown during the execution of a FutureAccess.
  29. * Exceptions thrown by this method are handled by the default error
  30. * handler.
  31. *
  32. * @since 7.1.8
  33. * @param exception
  34. * the thrown exception.
  35. */
  36. public void handleError(Exception exception);
  37. /**
  38. * Process the given exception in the context of the given runnable. If the
  39. * runnable extends {@link ErrorHandlingRunnable}, then the exception is
  40. * passed to {@link #handleError(Exception)} and null is returned. If
  41. * {@link #handleError(Exception)} throws an exception, that exception is
  42. * returned. If the runnable does not extend {@link ErrorHandlingRunnable},
  43. * then the original exception is returned.
  44. *
  45. * @since 8.7
  46. * @param runnable
  47. * the runnable for which the exception should be processed, not
  48. * <code>null</code>
  49. * @param exception
  50. * the exception to process, not <code>null</code>
  51. * @return the resulting exception, or <code>null</code> if the exception is
  52. * fully processed
  53. */
  54. public static Exception processException(Runnable runnable,
  55. Exception exception) {
  56. Objects.requireNonNull(runnable, "The runnable cannot be null.");
  57. if (runnable instanceof ErrorHandlingRunnable) {
  58. ErrorHandlingRunnable errorHandlingRunnable = (ErrorHandlingRunnable) runnable;
  59. try {
  60. errorHandlingRunnable.handleError(exception);
  61. return null;
  62. } catch (Exception exceptionFromHandler) {
  63. return exceptionFromHandler;
  64. }
  65. }
  66. return exception;
  67. }
  68. }