RecoverableError.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #pragma once
  8. #include <exception>
  9. #include <functional>
  10. #include <string>
  11. namespace facebook {
  12. namespace react {
  13. /**
  14. * RecoverableError
  15. *
  16. * An exception that it is expected we should be able to recover from.
  17. */
  18. struct RecoverableError : public std::exception {
  19. explicit RecoverableError(const std::string &what_)
  20. : m_what{"facebook::react::Recoverable: " + what_} {}
  21. virtual const char *what() const noexcept override {
  22. return m_what.c_str();
  23. }
  24. /**
  25. * runRethrowingAsRecoverable
  26. *
  27. * Helper function that converts any exception of type `E`, thrown within the
  28. * `act` routine into a recoverable error with the same message.
  29. */
  30. template <typename E>
  31. inline static void runRethrowingAsRecoverable(std::function<void()> act) {
  32. try {
  33. act();
  34. } catch (const E &err) {
  35. throw RecoverableError(err.what());
  36. }
  37. }
  38. private:
  39. std::string m_what;
  40. };
  41. } // namespace react
  42. } // namespace facebook