Use c++ exceptions in react/

Reviewed By: astreet

Differential Revision: D2905193

fb-gh-sync-id: ddb4c615ede606e99c92a09a96a15141b912ef72
shipit-source-id: ddb4c615ede606e99c92a09a96a15141b912ef72
This commit is contained in:
Chris Hopman 2016-02-09 12:59:36 -08:00 committed by facebook-github-bot-5
parent 229c8c35c9
commit 7cda49d516
5 changed files with 31 additions and 21 deletions

View File

@ -9,7 +9,6 @@
#include <fb/log.h>
#include <folly/json.h>
#include <folly/String.h>
#include <jni/fbjni/Exceptions.h>
#include <sys/time.h>
#include "Value.h"
#include "jni/OnLoad.h"
@ -42,8 +41,6 @@ using fbsystrace::FbSystraceSection;
static const int64_t NANOSECONDS_IN_SECOND = 1000000000LL;
static const int64_t NANOSECONDS_IN_MILLISECOND = 1000000LL;
using namespace facebook::jni;
namespace facebook {
namespace react {

View File

@ -4,7 +4,6 @@
#include <JavaScriptCore/JSStringRef.h>
#include <glog/logging.h>
#include <jni/fbjni/Exceptions.h>
#include "Value.h"

View File

@ -6,11 +6,29 @@
#include <JavaScriptCore/JSObjectRef.h>
#include <JavaScriptCore/JSValueRef.h>
#define throwJSExecutionException(...) jni::throwNewJavaException("com/facebook/react/bridge/JSExecutionException", __VA_ARGS__)
#include <stdexcept>
#include <algorithm>
namespace facebook {
namespace react {
struct JsException : std::runtime_error {
using std::runtime_error::runtime_error;
};
inline void throwJSExecutionException(const char* msg) {
throw JsException(msg);
}
template <typename... Args>
inline void throwJSExecutionException(const char* fmt, Args... args) {
int msgSize = snprintf(nullptr, 0, fmt, args...);
msgSize = std::min(512, msgSize + 1);
char *msg = (char*) alloca(msgSize);
snprintf(msg, msgSize, fmt, args...);
throw JsException(msg);
}
void installGlobalFunction(
JSGlobalContextRef ctx,
const char* name,

View File

@ -9,8 +9,6 @@
#include <glog/logging.h>
#include <folly/Memory.h>
#include <jni/fbjni/Exceptions.h>
#include <jni/LocalReference.h>
#include "JSCHelpers.h"
#include "jni/JSLoader.h"
@ -43,6 +41,7 @@ JSCWebWorker::JSCWebWorker(int id, JSCWebWorkerOwner *owner, std::string scriptS
});
}
JSCWebWorker::~JSCWebWorker() {
CHECK(isTerminated()) << "Didn't terminate the web worker before releasing it!";;
}

View File

@ -2,9 +2,8 @@
#include "MethodCall.h"
#include <jni/fbjni.h>
#include <folly/json.h>
#include <stdexcept>
namespace facebook {
namespace react {
@ -22,13 +21,13 @@ std::vector<MethodCall> parseMethodCalls(const std::string& json) {
}
if (!jsonData.isArray()) {
jni::throwNewJavaException(jni::gJavaLangIllegalArgumentException,
"Did not get valid calls back from JS: %s", jsonData.typeName());
throw std::invalid_argument(
folly::to<std::string>("Did not get valid calls back from JS: ", jsonData.typeName()));
}
if (jsonData.size() < REQUEST_PARAMSS + 1) {
jni::throwNewJavaException(jni::gJavaLangIllegalArgumentException,
"Did not get valid calls back from JS: size == %d", jsonData.size());
throw std::invalid_argument(
folly::to<std::string>("Did not get valid calls back from JS: size == ", jsonData.size()));
}
auto moduleIds = jsonData[REQUEST_MODULE_IDS];
@ -37,16 +36,14 @@ std::vector<MethodCall> parseMethodCalls(const std::string& json) {
int callId = -1;
if (!moduleIds.isArray() || !methodIds.isArray() || !params.isArray()) {
jni::throwNewJavaException(jni::gJavaLangIllegalArgumentException,
"Did not get valid calls back from JS: %s",
json.c_str());
throw std::invalid_argument(
folly::to<std::string>("Did not get valid calls back from JS: ", json.c_str()));
}
if (jsonData.size() > REQUEST_CALLID) {
if (!jsonData[REQUEST_CALLID].isInt()) {
jni::throwNewJavaException(jni::gJavaLangIllegalArgumentException,
"Did not get valid calls back from JS: %s",
json.c_str());
throw std::invalid_argument(
folly::to<std::string>("Did not get valid calls back from JS: %s", json.c_str()));
} else {
callId = jsonData[REQUEST_CALLID].getInt();
}
@ -56,8 +53,8 @@ std::vector<MethodCall> parseMethodCalls(const std::string& json) {
for (size_t i = 0; i < moduleIds.size(); i++) {
auto paramsValue = params[i];
if (!paramsValue.isArray()) {
jni::throwNewJavaException(jni::gJavaLangIllegalArgumentException,
"Call argument isn't an array");
throw std::invalid_argument(
folly::to<std::string>("Call argument isn't an array"));
}
methodCalls.emplace_back(