forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Propagate Load Errors through JSExecutor API
Reviewed By: mhorowitz Differential Revision: D4315204 fbshipit-source-id: ae282e0a0398a602dd790de3a2b1adb5fdc1f50c
- Loading branch information
1 parent
9f3ef48
commit 6ca6b49
Showing
5 changed files
with
131 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
#pragma once | ||
|
||
#include <exception> | ||
#include <functional> | ||
#include <string> | ||
|
||
namespace facebook { | ||
namespace react { | ||
namespace detail { | ||
|
||
/** | ||
* RecoverableError | ||
* | ||
* An exception that it is expected we should be able to recover from. | ||
*/ | ||
struct RecoverableError : public std::exception { | ||
|
||
explicit RecoverableError(const std::string &what_) | ||
: m_what {what_} | ||
{} | ||
|
||
virtual const char* what() const throw() override { return m_what.c_str(); } | ||
|
||
/** | ||
* runRethrowingAsRecoverable | ||
* | ||
* Helper function that converts any exception of type `E`, thrown within the | ||
* `act` routine into a recoverable error with the same message. | ||
*/ | ||
template <typename E> | ||
inline static void runRethrowingAsRecoverable(std::function<void()> act) { | ||
try { | ||
act(); | ||
} catch(const E &err) { | ||
throw RecoverableError(err.what()); | ||
} | ||
} | ||
|
||
private: | ||
std::string m_what; | ||
}; | ||
|
||
} // namespace detail | ||
} // namespace react | ||
} // namespace facebook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <exception> | ||
#include <stdexcept> | ||
|
||
#include <cxxreact/RecoverableError.h> | ||
|
||
using namespace facebook::react::detail; | ||
|
||
TEST(RecoverableError, RunRethrowingAsRecoverableRecoverTest) { | ||
try { | ||
RecoverableError::runRethrowingAsRecoverable<std::runtime_error>([]() { | ||
throw std::runtime_error("catch me"); | ||
}); | ||
FAIL() << "Unthrown exception"; | ||
} catch (const RecoverableError &err) { | ||
ASSERT_STREQ(err.what(), "catch me"); | ||
} catch (...) { | ||
FAIL() << "Uncaught exception"; | ||
} | ||
} | ||
|
||
TEST(RecoverableError, RunRethrowingAsRecoverableFallthroughTest) { | ||
try { | ||
RecoverableError::runRethrowingAsRecoverable<std::runtime_error>([]() { | ||
throw std::logic_error("catch me"); | ||
}); | ||
FAIL() << "Unthrown exception"; | ||
} catch (const RecoverableError &err) { | ||
FAIL() << "Recovered exception that should have fallen through"; | ||
} catch (const std::exception &err) { | ||
ASSERT_STREQ(err.what(), "catch me"); | ||
} | ||
} |