When executing JS throws an exception, capture the js stack in the C++ exception thrown

Reviewed By: davidaurelio

Differential Revision: D3428921

fbshipit-source-id: 0e8be84a2be92558ea3de0d32d1d4a53308d8270
This commit is contained in:
Marc Horowitz 2016-06-16 14:28:28 -07:00 committed by Facebook Github Bot 1
parent bc42861cbf
commit a60b74d974
2 changed files with 34 additions and 6 deletions

View File

@ -52,7 +52,8 @@ JSValueRef evaluateScript(JSContextRef context, JSStringRef script, JSStringRef
// file/resource, or was a constructed statement. The location
// info will include that source, if any.
std::string locationInfo = source != nullptr ? String::ref(source).str() : "";
auto line = exception.asObject().getProperty("line");
Object exObject = exception.asObject();
auto line = exObject.getProperty("line");
if (line != nullptr && line.isNumber()) {
if (locationInfo.empty() && line.asInteger() != 1) {
// If there is a non-trivial line number, but there was no
@ -71,7 +72,15 @@ JSValueRef evaluateScript(JSContextRef context, JSStringRef script, JSStringRef
}
LOG(ERROR) << "Got JS Exception: " << exceptionText;
throwJSExecutionException("%s", exceptionText.c_str());
Value jsStack = exObject.getProperty("stack");
if (jsStack.isNull() || !jsStack.isString()) {
throwJSExecutionException("%s", exceptionText.c_str());
} else {
LOG(ERROR) << "Got JS Stack: " << jsStack.toString().str();
throwJSExecutionExceptionWithStack(
exceptionText.c_str(), jsStack.toString().str().c_str());
}
}
return result;
}

View File

@ -17,12 +17,26 @@
namespace facebook {
namespace react {
struct JsException : std::runtime_error {
using std::runtime_error::runtime_error;
class JSException : public std::runtime_error {
public:
explicit JSException(const char* msg)
: std::runtime_error(msg)
, stack_("") {}
JSException(const char* msg, const char* stack)
: std::runtime_error(msg)
, stack_(stack) {}
const std::string& getStack() const {
return stack_;
}
private:
std::string stack_;
};
inline void throwJSExecutionException(const char* msg) {
throw JsException(msg);
throw JSException(msg);
}
template <typename... Args>
@ -31,7 +45,12 @@ inline void throwJSExecutionException(const char* fmt, Args... args) {
msgSize = std::min(512, msgSize + 1);
char *msg = (char*) alloca(msgSize);
snprintf(msg, msgSize, fmt, args...);
throw JsException(msg);
throw JSException(msg);
}
template <typename... Args>
inline void throwJSExecutionExceptionWithStack(const char* msg, const char* stack) {
throw JSException(msg, stack);
}
void installGlobalFunction(