Skip to content

Commit

Permalink
Disambiguate error messages in MethodCall
Browse files Browse the repository at this point in the history
Reviewed By: fromcelticpark

Differential Revision: D5706581

fbshipit-source-id: 06343e2a41d08a1594eb35bb96cc6dc7bf9e29c6
  • Loading branch information
javache authored and facebook-github-bot committed Aug 29, 2017
1 parent b8d347d commit 46f0a3b
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions ReactCommon/cxxreact/MethodCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ namespace react {
#define REQUEST_PARAMSS 2
#define REQUEST_CALLID 3

static const char *errorPrefix = "Malformed calls from JS: ";

std::vector<MethodCall> parseMethodCalls(folly::dynamic&& jsonData) throw(std::invalid_argument) {
if (jsonData.isNull()) {
return {};
}

if (!jsonData.isArray()) {
throw std::invalid_argument(
folly::to<std::string>("Did not get valid calls back from JS: ", jsonData.typeName()));
folly::to<std::string>(errorPrefix, "input isn't array but ", jsonData.typeName()));
}

if (jsonData.size() < REQUEST_PARAMSS + 1) {
throw std::invalid_argument(
folly::to<std::string>("Did not get valid calls back from JS: size == ", jsonData.size()));
folly::to<std::string>(errorPrefix, "size == ", jsonData.size()));
}

auto& moduleIds = jsonData[REQUEST_MODULE_IDS];
Expand All @@ -35,18 +37,18 @@ std::vector<MethodCall> parseMethodCalls(folly::dynamic&& jsonData) throw(std::i

if (!moduleIds.isArray() || !methodIds.isArray() || !params.isArray()) {
throw std::invalid_argument(
folly::to<std::string>("Did not get valid calls back from JS: ", folly::toJson(jsonData)));
folly::to<std::string>(errorPrefix, "not all fields are arrays.\n\n", folly::toJson(jsonData)));
}

if (moduleIds.size() != methodIds.size() || moduleIds.size() != params.size()) {
throw std::invalid_argument(
folly::to<std::string>("Did not get valid calls back from JS: ", folly::toJson(jsonData)));
folly::to<std::string>(errorPrefix, "field sizes are different.\n\n", folly::toJson(jsonData)));
}

if (jsonData.size() > REQUEST_CALLID) {
if (!jsonData[REQUEST_CALLID].isNumber()) {
throw std::invalid_argument(
folly::to<std::string>("Did not get valid calls back from JS: %s", folly::toJson(jsonData)));
folly::to<std::string>(errorPrefix, "invalid callId", jsonData[REQUEST_CALLID].typeName()));
}
callId = jsonData[REQUEST_CALLID].asInt();
}
Expand All @@ -55,7 +57,7 @@ std::vector<MethodCall> parseMethodCalls(folly::dynamic&& jsonData) throw(std::i
for (size_t i = 0; i < moduleIds.size(); i++) {
if (!params[i].isArray()) {
throw std::invalid_argument(
folly::to<std::string>("Call argument isn't an array"));
folly::to<std::string>(errorPrefix, "method arguments isn't array but ", params[i].typeName()));
}

methodCalls.emplace_back(
Expand Down

0 comments on commit 46f0a3b

Please sign in to comment.